2010-04-19

Set Text Variables

Part of a routine to set folios
I used to set folios by leaving misspelled tags on pages such as 'mMonth' and then scripting their replacement for folios. The advantage of this is that if a user fails to run the script that does the swap, or if the document doesn't conform to the site's naming convention, the misspelled tags remain and hopefully they would be found during a spell check of the document. Alas, users don't seem to follow the rules.

So, I've changed to using Adobe InDesign text variables for folios. The template would be set to have no values and then at key moments (like opening, and saving as) the values would get set.

The function below makes it easy to set values as it adds the text variable if it doesn't exist.

//
function setCustomTextVariable (docRef, name, value) {
//-------------------------------------------------------------------------
//-- S E T C U S T O M T E X T V A R I A B L E
//-------------------------------------------------------------------------
//-- Generic: Yes for InDesign CS4 and newer.
//-------------------------------------------------------------------------
//-- Purpose: To set a Custom Text Variable value. These can be used for
//-- a multitude of items, but they are commonly used for folios.
//-------------------------------------------------------------------------
//-- Arguments:
//-- docRef: The reference to the document to alter
//-- name: The name of the CustomText Variable
//-- value: The text that the CustomText Variable should be set to.
//-------------------------------------------------------------------------
//-- Calls: Nothing.
//-------------------------------------------------------------------------
//-- Returns: true unless an error occurs, which returns false.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ setCustomTextVariable (app.documents[0], 'Publication', 'Star-Ledger')
//-------------------------------------------------------------------------
//-- Notes: The Text Variable does not have to exist, this will create it.
//-- But if the text variable is not a Custom Text Variable, its value
//-- won't be able to be set, and this function will fail silently. See
//-- returns above.
//-------------------------------------------------------------------------
//-- Written: 2010.04.19 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
try {
var atv = docRef.textVariables.everyItem() ;

if (atv.hasOwnProperty (name)) {
//-- Note, not testing to see if the value can be set. If the TV
//-- isn't a 'Custom' TV, this will fail.
atv [name].variableOptions.contents = value ;
}
else {
//-- Create the TV and set the value.
var newTV = docRef.textVariables.add({name:name, variableType:VariableTypes.CUSTOM_TEXT_TYPE}) ;
newTV.variableOptions.contents = value ;
}
return true ;
}
catch (failSilently) {
var localError = failSilently ;
return false ;
}
}
//

No comments:

Post a Comment