2009-07-15

Unlock an Adobe InDesign Object

Short code, could be shorter...

Here is a really simple function to unlock an Adobe InDesign object via script. It is a case where the comments far outweigh the code. The code itself could loose two line by dropping the else clause. And if you collapse the if block it too could be on one line. But it is easier to read as it is. The key here is not unlocking the object directly (because it won't work), but first trying to see if the parent object is also locked. When the locked parents are unlocked, the object itself will be unlocked. That is the way it works in Adobe InDesign -- A departure from QuarkXPress thinking.


function unlock(o) {
//-------------------------------------------------------------------------
//-- U N L O C K
//-------------------------------------------------------------------------
//-- Generic: Yes for Most versions of Adobe InDesign
//-------------------------------------------------------------------------
//-- Purpose: To unlock position of a locked object. If successful the
//-- function returns false to indicate the locked status of the
//-- object. The function is recursive, and calls itself on the
//-- parent of the object because in Adobe InDesign, if the an
//-- object of group is locked, the group is locked. Thus, you have
//-- to unlock the group that contains the object to unlock the
//-- object itself.
//-------------------------------------------------------------------------
//-- Parameters: o: The object to unlock. Very generic as to what that
//-- object actually is.
//-------------------------------------------------------------------------
//-- Returns:
//-- False if the object is now unlocked.
//-- True if there was an error and the item remains locked or cannot
//-- be unlocked because there is no locked or unlocked option.
//-------------------------------------------------------------------------
//-- Calls: Itself. The function is recursive.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var A = app.selection[0] ;
//~ unlock(A) ;
//-------------------------------------------------------------------------
//-- Written: 2009.06.13 -- HA
//-- Written by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------

//-- If an Adobe InDesign's _parent_ object can be locked it will have a
//-- property called locked. If it has that property, then call this
//-- same function recursively on the _parent_ of the object.
if ( o.parent.hasOwnProperty ('locked') ) {
return ( unlock ( o.parent ) ) ;
}
else {
//-- The else really isn't necessary in this case, but it makes
//-- the function easier to read...
//-- If here, then try to unlock the object itself and return
//-- false, otherwsise it could generate an error becuase it
//-- can't be unlocked and thus return true.
try { return ( o.locked = false ) }
catch ( err ) { return true }
}
}
//

3 comments: