2009-07-17

Move an Object Any Direction in Any Measurement System

Works regardless of the document's measurement system

One problem often encountered when manipulating items on Adobe InDesign pages is knowing what the current page's or document's measurement system. If you asked something to move '1', you don't know if you are moving 1 pica, 1 inch, or 1 centimeter. To make matters worse, a horizontal movement might happen in one measurement system and vertical adjustments in another measurement system.

This function solves that by allowing you to specify the distance in any measurement system. Just supply the 'unit' parameter a string such as 'picas', 'inches', 'points', 'cm', etc.

The function is long because it has loads of error checking. But it works, and that is all that matters.

//
function moveObjectBy ( o , down , right , unit ) {
//-------------------------------------------------------------------------
//-- M O V E O B J E C T B Y
//-------------------------------------------------------------------------
//-- Generic: Yes for Adobe InDesign CS3
//-------------------------------------------------------------------------
//-- Purpse: To move the passed object down and to the right by the
//-- specified amount in the passed unit values.
//-- The significant thing here is that you do not need to worry about
//-- the current measurment system in use on the Adobe InDesign page.
//-- Another benifit of this routine is that some special objects
//-- can be constructed via plug-ins which prevent them from having
//-- thier bounds manipulated. Ads on MediaSpan jazbox pages are
//-- one of these types of objects.
//-------------------------------------------------------------------------
//-- Parameters: 4
//-- o: The Adobe InDesign object to move. There are many things
//-- that can be moved. Any page item ( text frame, graphic
//-- frame, unassigned frames, graphic line, group, etc. If
//-- it can be modified using controls in the Object menu in
//-- Adobe InDesign, likely it can me moved by this function.
//-- down: The amount to move the object down. If you want to move
//-- the object up, then use a negative value.
//-- right: The amount to move the object to the right. Again, use
//-- a negative value to move the item to the left.
//-- unit: A string. Can be any of a large number of choices.
//-- For example: 'pica', 'point', 'mm', 'centimeter', 'inches'
//-- or 'pc', 'pt', 'millimeters', 'cm', 'i'
//-------------------------------------------------------------------------
//-- Returns: True if successful. False if something prevented the item
//-- from moving. For example, locked objects cannot be moved.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ var s = app.selection[0]
//~ moveObjectBy ( s , 0 , 2.54 , 'cm' ) // right 1 inch
//~ moveObjectBy ( s , 3 , undefined , 'picas' ) // down a half
//~ moveObjectBy ( s , 0 , -1 , 'in' ) // back to the left 1 inch
//~ moveObjectBy ( s , -36 ) // return to the starting place
//-------------------------------------------------------------------------
//-- Written: 2009.07.17 from scratch during flight US377 from EWR to CLT
//-- Written by: Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Verify there are good numberic values for down and right or set to 0
if ( isNaN ( down ) ) { down = 0 } ;
if ( isNaN ( right ) ) { right = 0 } ;
//-- Assume points (my favorite for scripts) if the unit isn't passed.
if ( unit == undefined ) { var unit = 'points' ; }
//-- Make sure that we passed a lowercase value.
unit = String ( unit ).toLowerCase() ;
try {
//-- Convert to ExtendScript unit values. This is unique to
//-- ExtendScript and is not part of JavaScript or ECMAScript.
//-- Note, there appears to be a bug in how this works. Picas
//-- converts to 'pc' and move won't take that, so back to
//-- picas the string will be converted.
var downUnitsToMove = String ( new UnitValue ( down , unit ) ).replace ( 'pc', 'picas') ;
var rightUnitsToMove = String ( new UnitValue ( right , unit ) ).replace ( 'pc', 'picas') ;
//-- Most objects support a .move method. Try it then return true.
o.move( undefined , [ rightUnitsToMove , downUnitsToMove ] ) ;
return true ;
}
catch (err) {
//-- if any of the conversions to unit values failed or if
//-- the passed object cannot be moved this way, then
//-- return an error.
return false ;
}
}
//

No comments:

Post a Comment