Showing posts with label Document. Show all posts
Showing posts with label Document. Show all posts

2010-04-16

Get Document that Triggered Event

The correct document isn't always the one in front...

Event handlers are ways to run scripts when certain key events happen in Adobe InDesign or Adobe InCopy. Most things can be caught if there is a menu version of that function. Adobe's documentation does a decent job of explaining the common events, but not what to do with the 'event' itself, and is downright poor at the rest of the event handler issues.

But below is a very simple function that you can call inside the event handlers for 'open' and for 'close' event. Imagine you had multiple documents open. Now imagine that the windows are stacked so that you can see the close button for all the windows. Now assume you have an event handler that will do something useful (like look for overset text, preflight, adjust folios, for log a version comment). Well your event handler needs to know which document you just tried to close. That is where this function comes into play.

Event handlers always reference a function. But the function is a specified only in name. You can't pass it anything. Well your event handler will always receive a single argument -- an event. And the event has a property called .target and that .target property has another property .name. And the name of the target of the event is the name of the document.


//
function docRefFromEvent (event) {
//-------------------------------------------------------------------------
//-- D O C R E F F R O M E V E N T
//-------------------------------------------------------------------------
//-- Generic: Yes for ExtendScript CS3, CS4, etc.
//-------------------------------------------------------------------------
//-- Purpose: To return the document triggering the event handler or the
//-- default of the frontmost document.
//-------------------------------------------------------------------------
//-- Arguments: event: the event which triggered some other function which
//-- in turn called this function. Likely it would be an open or a close
//-- function so that you can catch the correct document. For example,
//-- you need this for open handlers becasue if you have two documents
//-- open, documents[0] or .activeDocument will point to the first doc
//-- and not the document you just opened. Likewise with closing files,
//-- your windows could be tiled and you can close a file other than the
//-- active or the documents[0].
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: Either a reference to the front document or the document that
//-- triggered the passed event.
//-------------------------------------------------------------------------
//-- Written: 2010.04.06 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Created for version 3.52
if ((event != undefined) && event.hasOwnProperty ('target') && event.target.hasOwnProperty ('name')) {
var testDocRef = app.documents.itemByName (event.target.name) ;
if (testDocRef != null) {
return testDocRef ;
}
}
return app.documents[0] ;
}
//

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