Showing posts with label Reference. Show all posts
Showing posts with label Reference. Show all posts

2009-10-22

Get Page of Window

What is the user looking at?

I've found this function very handy lately. If you are opening files without showing the window ( as strange as that might seem, it can be very useful ), you will need a reference to what the user is viewing. This function returns the page the the Adobe InDesign thinks the user is looking at.

From that page reference you can easily get the document reference (remember it might not be app.activeDocument or app.documents[0]. See some prior posts to know why, but opening documents without showing the window is a really good reason!

Now, for some ideas. If you know the page that the user is looking at, you have ways to control printing of just that page, exporting just that page, adding items to just that page, applying master pages to just that page, etc...

//
function pageOfWindow () {
//-------------------------------------------------------------------------
//-- P A G E O F W I N D O W
//-------------------------------------------------------------------------
//-- Generic: Yes for Adobe InDesign, but not InCopy
//-------------------------------------------------------------------------
//-- Purpose: To return a reference to a the page that Adobe InDesign
//-- considers the active page.
//-------------------------------------------------------------------------
//-- Arguments: None
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: A page reference to the page that Adobe InDesign considers
//-- the active page. If no pages can be found it returns null ;
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var currentPage = pageOfWindow() ;
//-------------------------------------------------------------------------
//-- Notes: The page returned can be a Master Page
//-------------------------------------------------------------------------
//-- Written: 2009.09.22 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- 2009.09.24 added error handling

try {
//-- close any StoryWindows
while ( new RegExp ('Story','i').test( app.activeWindow.reflect.name ) ) {
app.activeWindow.close() ;
}
} catch (err) {
var errorObject = err ;
return null ;
}
//-- Get the page reference for the window displayed in the
//-- active Layout Window
return app.activeWindow.activePage ;
}
//

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
//