2009-09-14

Revised Return Document Reference

I was using this function in a script and noticed when it was passed a collections such as you get with .pages, it didn't return a document reference. This version does this. The version posted back on June 27, 2009 has been removed.

function returnDocumentReference ( anObject ) {
//-------------------------------------------------------------------------
//-- R E T U R N D O C U M E N T R E F E R E N C E
//-------------------------------------------------------------------------
//-- Generic: Yes for most versions of Adobe InCopy or Adobe InDesign.
//-------------------------------------------------------------------------
//-- Purpose: To return a document reference for the passed object.
//-------------------------------------------------------------------------
//-- Returns: A reference to the document holding the passed object
//-- providing the object is within 32 levels of the Document
//-- or returns null if the docucment cannot be found or if an
//-- error occurs.
//-------------------------------------------------------------------------
//-- Sample Use:
//-- var docRef = returnDocumentReference ( app.selection[0] )
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- This function is not recursive, but instead loops through the
//-- object's parents, grandparents, etc. until reaching something
//-- whose constructor's name includes 'Document'. What is
//-- interesting about the function is that it is generic enough
//-- that you could easily swap the word 'Document' for another
//-- type of object. However, in Adobe InDesign and Adobe InCopy
//-- not all objects below pages have constructors.
//-------------------------------------------------------------------------
//-- Written: by Jon S. Winters
//-- Unsure of original date, but edited on 2009.09.14 in New Orleans, LA
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------

// Provide an excape hatch incase something goes astray
var loopLimit = 32 ;

// Create a Regular Expression pattern
// for a case insensitive word 'Document'
var docPattern = new RegExp ( 'Document' , 'i' ) ;

// Start with the object itself incase it is the document.
var possibleDocument = anObject ;

// in case something really odd is sent, wrap in error handler
try {
// Continue looping up and chacking the parents until
// a matching document is found.
while ( ( ! docPattern.test( possibleDocument.constructor.name ) ) && ( loopLimit-- > 0 ) ){
//-- 2009.09.14 Below generates false error when a collection is passed
//-- such as .pages
try {
possibleDocument = possibleDocument.parent ;
}
catch (err ) {
//-- if the error deals with a collection of objects
//-- then try to get the parent of the first item.
if ( possibleDocument.length > 1 ) {
possibleDocument = possibleDocument[0].parent ;
}
else { return null ; } ;
}
}
// Check the loop limit and either return the document or null
if ( loopLimit > 0 ) {
return possibleDocument ; }
else { return null ; }
}
catch (err) { return null ; }
}// end of function
//

No comments:

Post a Comment