Copy link to clipboard
Copied
This slightly edited version of indesigns standart example script AddGuides.jsx does nothing different, but it remembers the dialogs setting into an activeDocuments script label and next time opens the AddGuide dialog with the last used settings. Nice, if you use it often.
Copy and Paste the below code into AddGuides.jsx as replacement or save as new script AddGuides2.jsx.
Enjoy.
Stephan
main();
function main(){
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
var _ObjectList = new Array;
if (app.documents.length != 0){
if (app.selection.length != 0){
for(var _Counter = 0;_Counter < app.selection.length; _Counter++){
switch (app.selection[_Counter].constructor.name){
case "Rectangle":
case "Oval":
case "Polygon":
case "TextFrame":
case "Group":
case "Button":
case "GraphicLine":
_ObjectList.push(app.selection[_Counter]);
break;
}
}
if (_ObjectList.length != 0){
var Prefs = {
data: { /* fallback default preferences */
checkTop: true,
checkLeft: true,
checkBottom: true,
checkRight: true,
checkXCenter: true,
checkYCenter: true,
checkXPoint:false,
checkYPoint:false,
checkEach: 0,
checkGB: 0,
XOffset:0,
YOffset:0
},
load: function(_doc) {
try {
var label = eval(_doc.extractLabel("addGuidePrefs"));
if (label) {
this.data = label;
}
} catch (e) {}
},
save: function(_doc) {
function hasGuideLabel() {
try {
var label = eval(_doc.extractLabel("addGuidePrefs"));
if (label) return true;
else return false;
} catch (e) {}
}
function removeGuideLabel() {
try {
_doc.insertLabel("addGuidePrefs", "");
} catch(e) {
alert('Remove Error: ' + e);
}
}
if (hasGuideLabel()) removeGuideLabel();
try {
_doc.insertLabel("addGuidePrefs", this.data.toSource());
} catch(e) {
alert('Add Error: ' + e);
}
},
setData: function(newData) {
for (var key in newData) {
if (newData.hasOwnProperty(key) && this.data.hasOwnProperty(key)) {
this.data[key] = newData[key];
}
}
}
}
Prefs.load(app.activeDocument);
var _OldXUnits = app.documents.item(0).viewPreferences.horizontalMeasurementUnits;
var _OldYUnits = app.documents.item(0).viewPreferences.verticalMeasurementUnits;
app.documents.item(0).viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
app.documents.item(0).viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
_DisplayDialog(_ObjectList,Prefs);
//Comment out the previous line and uncomment the following line to use a ScriptUI dialog.
//_DisplayScriptUIDialog(_ObjectList);
app.documents.item(0).viewPreferences.horizontalMeasurementUnits = _OldXUnits;
app.documents.item(0).viewPreferences.verticalMeasurementUnits = _OldYUnits;
}
else{
alert ("Please select a page item and try again.");
}
}
else{
alert ("Please select an object and try again.");
}
}
else{
alert ("Please open a document, select an object, and try again.");
}
}
function _DisplayDialog(_ObjectList,Prefs){
var _RangeButtons, _BasedOnButtons;
var _LabelWidth = 100;
var _w = app.dialogs.add({name:"AddGuides"});
with(_w){
with(dialogColumns.add()){
with(borderPanels.add()){
staticTexts.add({staticLabel:"Add Guides At:"});
with(dialogColumns.add()){
var _TopCB = checkboxControls.add({staticLabel:"&Top", checkedState:Prefs.data.checkTop});
var _LeftCB = checkboxControls.add({staticLabel:"&Left", checkedState:Prefs.data.checkLeft});
var _BottomCB = checkboxControls.add({staticLabel:"&Bottom", checkedState:Prefs.data.checkBottom});
var _RightCB = checkboxControls.add({staticLabel:"&Right", checkedState:Prefs.data.checkRight});
var _XCenterCB = checkboxControls.add({staticLabel:"&Horizontal Center", checkedState:Prefs.data.checkXCenter});
var _YCenterCB = checkboxControls.add({staticLabel:"&Vertical Center", checkedState:Prefs.data.checkYCenter});
var _XPointCB = checkboxControls.add({staticLabel:"Path Point Hori&zontal Anchor", checkedState:Prefs.data.checkXPoint});
var _YPointCB = checkboxControls.add({staticLabel:"Path Point Verti&cal Anchor", checkedState:Prefs.data.checkYPoint});
}
}
with(borderPanels.add()){
staticTexts.add({staticLabel:"Add Guides Around:"});
with(_RangeButtons = radiobuttonGroups.add()){
radiobuttonControls.add({staticLabel:"Each &Object", checkedState:true, minWidth:156});
radiobuttonControls.add({staticLabel:"Entire &Selection"});
selectedButton = Prefs.data.checkEach;
}
}
with(borderPanels.add()){
with(dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Guides Based On:"});
}
with(dialogColumns.add()){
with(_BasedOnButtons = radiobuttonGroups.add()){
radiobuttonControls.add({staticLabel:"&Geometric Bounds", checkedState:true, minWidth:160});
radiobuttonControls.add({staticLabel:"V&isible Bounds"});
selectedButton = Prefs.data.checkGB;
}
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Horizontal Offset:", minWidth:_LabelWidth});
}
with(dialogColumns.add()){
var _XOffsetField = measurementEditboxes.add({editValue:Prefs.data.XOffset, editUnits:MeasurementUnits.points});
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Vertical Offset:", minWidth:_LabelWidth});
}
with(dialogColumns.add()){
var _YOffsetField = measurementEditboxes.add({editValue:Prefs.data.YOffset, editUnits:MeasurementUnits.points});
}
}
}
}
}
}
_Return = _w.show();
if (_Return == true){
//Get control values from the dialog box.
Prefs.setData({
checkTop: _TopCB.checkedState,
checkLeft: _LeftCB.checkedState,
checkBottom: _BottomCB.checkedState,
checkRight: _RightCB.checkedState,
checkXCenter: _XCenterCB.checkedState,
checkYCenter: _YCenterCB.checkedState,
checkXPoint: _XPointCB.checkedState,
checkYPoint: _YPointCB.checkedState,
checkGB: _BasedOnButtons.selectedButton,
checkEach: _RangeButtons.selectedButton,
XOffset: _XOffsetField.editValue,
YOffset: _YOffsetField.editValue
});
//Remove the dialog box from memory.
_w.destroy();
if(Prefs.data.checkTop+Prefs.data.checkLeft+Prefs.data.checkBottom+Prefs.data.checkRight+Prefs.data.checkXCenter+Prefs.data.checkYCenter+Prefs.data.checkXPoint+Prefs.data.checkYPoint !=0){
_AddGuides(Prefs, _ObjectList);
}
else{
alert("No guides were specified.");
}
Prefs.save(app.activeDocument);
}
else{
//Remove the dialog box from memory.
_w.destroy();
}
}
//ScriptUI version of the above dialog box. Not edited to save prefs yet.
/*
function _DisplayScriptUIDialog(_ObjectList){
var _BasedOn, _Range;
var _w = new Window('dialog', 'AddGuides');
with(_w){
alignChildren = 'fill';
var _GuideTypesPanel = add('panel', undefined, "Add Guides At:");
with(_GuideTypesPanel){
orientation = 'column';
alignChildren = 'left';
margins = [12, 16, 12, 6];
spacing = 4;
_w._TopCB = add('checkbox', undefined, "Top");
_w._TopCB.value = true;
_w._LeftCB = add('checkbox', undefined, "Left");
_w._LeftCB.value = true;
_w._BottomCB = add('checkbox', undefined, "Bottom");
_w._BottomCB.value = true;
_w._RightCB = add('checkbox', undefined, "Right");
_w._RightCB.value = true;
_w._XCenterCB = add('checkbox', undefined, "Horizontal Center");
_w._XCenterCB.value = true;
_w._YCenterCB = add('checkbox', undefined, "Vertical Center");
_w._YCenterCB.value = true;
_w._XPointCB = add('checkbox', undefined, "Path Point Horizontal Anchor");
_w._XPointCB.value = true;
_w._YPointCB = add('checkbox', undefined, "Path Point Vertical Anchor");
_w._YPointCB.value = true;
}
var _GuideLocationPanel = add('panel', undefined, "Add Guides Around:");
with(_GuideLocationPanel){
orientation = 'column';
alignChildren = 'left';
margins = [12, 14, 12, 6];
spacing = 4;
_w._EachObjectRadioButton = add('radiobutton', undefined, "Each Object");
_w._EachObjectRadioButton.value = true;
_w._EntireSelectionRadioButton = add('radiobutton', undefined, "Entire Selection");
}
var _GuidesBasedOnPanel = add('panel', undefined, "Add Guides Based On:");
with(_GuidesBasedOnPanel){
orientation = 'column';
alignChildren = 'left';
margins = [12, 14, 12, 6];
spacing = 4;
_w._GeometricBoundsRadioButton = add('radiobutton', undefined, "Geometric Bounds");
_w._GeometricBoundsRadioButton.value = true;
_w._VisibleBoundsRadioButton = add('radiobutton', undefined, "Visible Bounds");
with(add('group')){
orientation = 'row';
alignChildren = 'center';
spacing = 2;
with(add('group')){
orientation = 'column';
alignChildren = 'right';
var _XOffsetLabel = add('statictext', undefined, "Horizontal Offset:");
var _YOffsetLabel = add('statictext', undefined, "Vertical Offset:");
}
with(add('group')){
orientation = 'column';
alignChildren = 'right';
_w._XOffsetField = add('edittext', undefined, "0", {enterKeySignalsOnChange:false});
_w._XOffsetField.characters = 7;
_w._XOffsetField.justify = "right";
_XOffsetField.onChange = function(){
if(_ValidateNumber(_XOffsetField.text)==false){
alert("Invalid numeric entry!");
_w._XOffsetField.text = "0";
}
}
_w._YOffsetField = add('edittext', undefined, "0");
_w._YOffsetField.justify = "right";
_w._YOffsetField.characters = 7;
_YOffsetField.onChange = function(){
if(_ValidateNumber(_YOffsetField.text)==false){
alert("Invalid numeric entry!");
_w._YOffsetField.text = "0";
}
}
}
with(add('group')){
orientation = 'column';
alignChildren = 'left';
add('statictext', undefined, "points");
add('statictext', undefined, "points");
}
}
}
with(add('group')){
orientation = 'row';
alignment = 'right';
_w._CloseButton = add('button', undefined, "Close", {name:'cancel'});
_w._CloseButton.onClick = function(){_w.close()};
_w._OKButton = add('button', undefined, "OK", {name:'ok'});
}
}
var _Return = _w.show();
if (_Return == true){
//Get control values from the dialog box.
with(_w){
var _Top = _TopCB.value;
var _Left = _LeftCB.value;
var _Bottom = _BottomCB.value;
var _Right = _RightCB.value;
var _XCenter = _XCenterCB.value;
var _YCenter = _YCenterCB.value;
var _XPoint = _XPointCB.value;
var _YPoint = _YPointCB.value;
var _XOffset = parseFloat(_XOffsetField.text);
var _YOffset = parseFloat(_YOffsetField.text);
if(_GeometricBoundsRadioButton.value == true){
_BasedOn = 0;
}
else{
_BasedOn = 1;
}
if(_EachObjectRadioButton.value == true){
_Range = 0;
}
else{
_Range = 1;
}
}
_w.close();
if(_Top+_Left+_Bottom+_Right+_XCenter+_YCenter+_XPoint+_YPoint !=0){
_AddGuides(Prefs, _ObjectList);
}
else{
alert("No guides were specified.");
}
}
else{
_w.close();
}
}
*/
function _AddGuides(p, _ObjectList){
var _Top, _Left, _Bottom, _Right, _XCenter, _YCenter, _Range, _BasedOn, _XOffset, _YOffset, _XPoint, _YPoint;
_Top = p.data.checkTop;
_Left = p.data.checkLeft;
_Bottom = p.data.checkBottom;
_Right = p.data.checkRight;
_XCenter = p.data.checkXCenter;
_YCenter = p.data.checkYCenter;
_Range = p.data.checkEach;
_BasedOn = p.data.checkGB;
_XOffset = p.data.XOffset;
_YOffset = p.data.YOffset;
_XPoint = p.data.checkXPoint;
_YPoint = p.data.checkYPoint;
var _Layer, _Counter;
var _OldRulerOrigin = app.activeDocument.viewPreferences.rulerOrigin;
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.spineOrigin;
//Create a layer to hold the printers marks (if it does not already exist).
_Layer = app.activeDocument.layers.item("Guides");
try{
_LayerName = _Layer.name;
}
catch (_Error){
_Layer = app.activeDocument.layers.add({name:"Guides"});
}
//Process the objects in the selection.
for(_Counter = 0; _Counter < _ObjectList.length; _Counter ++){
var _Object = _ObjectList[_Counter];
if (_BasedOn == 0){
_Bounds = _Object.geometricBounds;
}
else{
_Bounds = _Object.visibleBounds;
}
//Draw guides at path point locations, if necessary.
if ((_XPoint == true)||(_YPoint == true)){
_DrawGuidesAtPathPoints(_Object, _XPoint, _YPoint);
}
//Set up some initial bounding box values.
if ((_Range != 0)&&(_Counter==0)){
_X1 = _Bounds[1];
_Y1 = _Bounds[0];
_X2 = _Bounds[3];
_Y2 = _Bounds[2];
}
if(_Range == 0){
_DrawGuides (_Bounds[1], _Bounds[0], _Bounds[3], _Bounds[2], _Top, _Left, _Bottom, _Right, _XCenter, _YCenter, _Layer, _XOffset, _YOffset);
}
else{
//Compare the bounds values to the stored bounds.
//If a given bounds value is less than (for x1 and y1) or
//greater than (for x2 and y2) the stored value,
//then replace the stored value with the bounds value.
if (_Bounds[0] < _Y1){
_Y1 = _Bounds[0];
}
if (_Bounds[1] < _X1){
_X1 = _Bounds[1];
}
if (_Bounds[2] > _Y2){
_Y2 = _Bounds[2];
}
if (_Bounds[3] > _X2){
_X2 = _Bounds[3];
}
}
}
if(_Range != 0){
_DrawGuides (_X1, _Y1, _X2, _Y2, _Top, _Left, _Bottom, _Right, _XCenter, _YCenter, _Layer, _XOffset, _YOffset);
}
app.activeDocument.viewPreferences.rulerOrigin = _OldRulerOrigin;
}
function _DrawGuidesAtPathPoints(_Object, _XPoint, _YPoint){
for(var _PathCounter = 0; _PathCounter < _Object.paths.length; _PathCounter++){
for(var _PointCounter = 0; _PointCounter < _Object.paths.item(_PathCounter).pathPoints.length; _PointCounter ++){
if(_XPoint==true){
_DrawGuide(_Object.paths.item(_PathCounter).pathPoints.item(_PointCounter).anchor[0], 1);
}
if(_YPoint==true){
_DrawGuide(_Object.paths.item(_PathCounter).pathPoints.item(_PointCounter).anchor[1], 0);
}
}
}
}
function _DrawGuides(_X1, _Y1, _X2, _Y2, _Top, _Left, _Bottom, _Right, _XCenter, _YCenter, _Layer, _XOffset, _YOffset){
if (_Top == true){
_DrawGuide(_Y1 - _YOffset, 0);
}
if (_Left == true){
_DrawGuide(_X1 - _XOffset, 1);
}
if (_Bottom == true){
_DrawGuide(_Y2 + _YOffset, 0);
}
if (_Right == true){
_DrawGuide(_X2 + _XOffset, 1);
}
if (_XCenter == true){
_DrawGuide(_X1+((_X2-_X1)/2), 1);
}
if (_YCenter == true){
_DrawGuide(_Y1+((_Y2-_Y1)/2), 0);
}
}
function _DrawGuide(_GuideLocation, _GuideOrientation){
var _Layer = app.activeDocument.layers.item("Guides");
with(app.activeWindow.activeSpread){
if(_GuideOrientation == 0){
with (guides.add(_Layer, undefined, undefined)){
orientation=HorizontalOrVertical.horizontal;
location=_GuideLocation;
}
}
else{
with (guides.add(_Layer, undefined, undefined)){
orientation=HorizontalOrVertical.vertical;
location=_GuideLocation;
}
}
}
}
function _ValidateNumber(_String){
var _RegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
return _RegExp.test(_String);
}
Copy link to clipboard
Copied
Thanks for posting this.
What was the need or reasoning behind it?
Copy link to clipboard
Copied
Sorry i missed you post. The need reasoning was: if you need this script more often than occasionally than it's a wonderfull convenience not having to take care of all the checkmarks again and again. Mostly you'll use this script similarly each time.
Copy link to clipboard
Copied
If you save your label with app - instead of ActiveDocument - should make it more versatile.