Recently I was reminded of a problem in writing a script for Adobe InDesign that needs discussion. It has to do with existing measurement and margin settings.

You may recall in creating a small document that you need to make sure margin settings are not larger than the size of the document. If not, here is a little script to demonstrate.

AppleScript:

tell application "Adobe InDesign CS6"
	set horizontal measurement units of view preferences to picas
	if not (exists document preset "TestThis") then
		make document preset with properties {name:"TestThis", width:"2 in", height:"2 in", top:36, left:36, bottom:36, right:36}
	end if
end tell

ExtendScript:

app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PICAS;
var thisTest = app.documentPresets.item("TestThis").isValid);
if (thisTest == false) {
app.documentPresets.add ({name:"TestThis", width:"2 in", height:"2 in", top:36, left:36, bottom:36, right:36});
 }

The problem here is obvious because horizontal measurement units is being set as part of the script. When the default measurement units are set to a unit other than anticipated, the reason for an out of range error will not be so obvious. If the margin widths (72 picas total in the example) is wider than the width of the document), we get the error. This problem can be avoided by indicating the measurement units for the document preset margins using a string value.

AppleScript:

tell application "Adobe InDesign CS6"
	if not (exists document preset "TestThis") then
		make document preset with properties {name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"}
	end if
end tell

ExtendScript

app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PICAS;
var thisTest = app.documentPresets.item("TestThis").isValid);
if (thisTest == false) {
app.documentPresets.add ({name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"});
 }

What you might not expect is the problem that can occur with margins set previously at the application level. If the left and right margins have been set previously to 72 picas, the following script will error out with a “Data out of range” error.

AppleScript:

tell application "Adobe InDesign CS6"
	if not (exists document preset "TestThis") then
		make document preset with properties {name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"}
	end if
end tell

ExtendScript:

var thisTest =  (app.documentPresets.item("TestThis").isValid);
if (thisTest == false) {
    alert ("preset not found");
app.documentPresets.add ({name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"});
 } else {
     alert ("preset found");
 }

The problem, this time, is not that measurement units are improperly set or that you failed to indicate the measurement unit for margins in creating the document preset. The problem is that the width of the left and right margins for the application exceed the width of the intended document preset.

The way to prevent this problem from raising its ugly head when you create a document or a document preset is to set the margin preferences for the application to zero. You may also want to preserve the original settings, as in the following:

AppleScript

set presetCreated to false
tell application "Adobe InDesign CS6"
	//place original values in variable
	set oldPrefs to margin preferences
	--set margins to 0
	tell margin preferences
		set left to 0
		set right to 0
		set top to 0
		set bottom to 0
	end tell
	--create preset if it does not exist
	if not (exists document preset "TestThis") then
		make document preset with properties {name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"}
		set presetCreated to true
	end if
	--reset to default settings
	tell margin preferences
		set left to left of oldPrefs
		set right to right of oldPrefs
		set top to top of oldPrefs
		set bottom to bottom of oldPrefs
	end tell
end tell
presetCreated

ExtendScript

var presetCreated = false;
var oldPrefs = app.marginPreferences;
//place original values in variable
var marginPrefs = app.marginPreferences;
//set margins to 0
marginPrefs.left = 0;
marginPrefs.right = 0;
marginPrefs.top = 0;
marginPrefs.bottom = 0;
var thisTest =  (app.documentPresets.item("TestThis").isValid);
if (thisTest == false) {
   presetCreated = true;
app.documentPresets.add ({name:"TestThis", width:"2 in", height:"2 in", top:"36 pt", left:"36 pt", bottom:"36 pt", right:"36 pt"});
 }
//reset margins to original values
marginPrefs.left = oldPrefs.left;
marginPrefs.right = oldPrefs.right;
marginPrefs.bottom = oldPrefs.bottom;
marginPrefs.top = oldPrefs.top;
presetCreated;

It might be a good idea to get into the habit of setting and resetting default margin and view preferences in all scripts that have to do with creating documents or document presets. You never know when a user might set these values to something other than anticipated.