Skip to main content
M.Hasanin
Inspiring
August 18, 2022
Answered

Resize All items Size with Selecting Anchor Point Option

  • August 18, 2022
  • 1 reply
  • 451 views

Hi Experts,,

I made simple script to resize page items and i tried to make variable for Anchor point option in this script

but it not work and give me this error (predicate expression) ..etc as shown here :

and here is the script that causing this error :

 

//------------------------------------------
//Resize All items Size with Selecting Anchor Point
//------------------------------------------

// ========
// DIALOG
// ========
var dialog = new Window("dialog");
dialog.text = "Resize and Fit with Anchor Positions";
dialog.preferredSize.width = 300;
dialog.orientation = "column";
dialog.alignChildren = ["center", "center"];
dialog.spacing = 10;
dialog.margins = 16;

//Add DropDown
var myAnchoreTitle = dialog.add ("statictext", undefined, "Select Your Anchor Option :");
var AnchorPointsa = ["bottom center anchor", "bottom left anchor", "bottom right anchor", "center anchor", "left center anchor", "right center anchor", "top center anchor", "top left anchor", "top right anchor"];
var AnchorPointsaList = dialog.add("dropdownlist", undefined, undefined, { name: "AnchorPointsaList", items: AnchorPointsa });
AnchorPointsaList.selection = 0;
AnchorPointsaList.alignment = ["center", "center"];

//Buttons
var buttons = dialog.add ("group")
buttons.add ("button", undefined, "OK");
buttons.add ("button", undefined, "Cancel");

//dialog.show();
var a = dialog.show()
if(a == 2){
  dialog.close();
  exit(0);
}

//Error Catcher for Function
function CatchMyError(){
    try {
        DoAllFrameSize();
    //Catch Error
    } catch (myError) {
    alert (myError,"ERROR!");
    exit();
    }
}
    
function DoAllFrameSize(){
    
    var myheight ="5.5 cm";
    var mywidth = "5.5 cm";
    
    //One Selected Anchor Point
    var SelectedAnchor = AnchorPointsaList.selection.index;
    switch (SelectedAnchor) {
            case 0:
                var myAnchorString = "BOTTOM_CENTER_ANCHOR";
                break;
            case 1:
                var myAnchorString = "BOTTOM_LEFT_ANCHOR";
                break;
            case 2:
                var myAnchorString = "BOTTOM_RIGHT_ANCHOR";
                break;
            case 3:
                var myAnchorString = "CENTER_ANCHOR";
                break;
            case 4:
                var myAnchorString = "LEFT_CENTER_ANCHOR";
                break;
            case 5:
                var myAnchorString = "RIGHT_CENTER_ANCHOR";
                break;
            case 6:
                var myAnchorString = "TOP_CENTER_ANCHOR";
                break;
            case 7:
                var myAnchorString = "TOP_LEFT_ANCHOR";
                break;
            case 8:
                var myAnchorString = "TOP_RIGHT_ANCHOR";
                break;
            default:
                break;
}
    
var allDocItems = app.activeDocument.pageItems.everyItem();
allDocItems.resize(  
    [CoordinateSpaces.INNER_COORDINATES, BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS],  
    AnchorPoint.(myAnchorString), 
    //AnchorPoint.TOP_CENTER_ANCHOR,  
    ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, 
    [UnitValue(mywidth).as('pt'), UnitValue(myheight).as('pt')]
    );
}

//Run with UNDO Enabled
app.doScript(CatchMyError, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change All items Size with Selected Anchor");

//Anchor Point
//~ AnchorPoint.BOTTOM_CENTER_ANCHOR
//~ AnchorPoint.BOTTOM_LEFT_ANCHOR
//~ AnchorPoint.BOTTOM_RIGHT_ANCHOR
//~ AnchorPoint.CENTER_ANCHOR
//~ AnchorPoint.LEFT_CENTER_ANCHOR
//~ AnchorPoint.RIGHT_CENTER_ANCHOR
//~ AnchorPoint.TOP_CENTER_ANCHOR
//~ AnchorPoint.TOP_LEFT_ANCHOR
//~ AnchorPoint.TOP_RIGHT_ANCHOR

 

Actually i dont know if this is possible to work, so please advice me what to do? and thanks in advance

This topic has been closed for replies.
Correct answer brian_p_dts

Why set your case statements to strings? Just set them to the enum value for the anchor point, ie. 

case 1:
     var myAnchorString = AnchorPoint.BOTTOM_LEFT_ANCHOR;
     break;

And then just: 

allDocItems.resize(  
    [CoordinateSpaces.INNER_COORDINATES, BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS],  
    myAnchorString, 
    ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, 
    [UnitValue(mywidth).as('pt'), UnitValue(myheight).as('pt')]
    );

You're trying to use a string to set the enum. Enums are not strings. They have associated integer values that you could pass instead, which are available in the documentation: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#AnchorPoint.html#d1e102866

 

This illuminating thread on enums, their names and values could prove useful: https://community.adobe.com/t5/indesign-discussions/js-a-way-to-get-enumeration-names/td-p/2059966

1 reply

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
August 18, 2022

Why set your case statements to strings? Just set them to the enum value for the anchor point, ie. 

case 1:
     var myAnchorString = AnchorPoint.BOTTOM_LEFT_ANCHOR;
     break;

And then just: 

allDocItems.resize(  
    [CoordinateSpaces.INNER_COORDINATES, BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS],  
    myAnchorString, 
    ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, 
    [UnitValue(mywidth).as('pt'), UnitValue(myheight).as('pt')]
    );

You're trying to use a string to set the enum. Enums are not strings. They have associated integer values that you could pass instead, which are available in the documentation: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#AnchorPoint.html#d1e102866

 

This illuminating thread on enums, their names and values could prove useful: https://community.adobe.com/t5/indesign-discussions/js-a-way-to-get-enumeration-names/td-p/2059966

M.Hasanin
M.HasaninAuthor
Inspiring
August 18, 2022

@brian_p_dts 

oh! i forget about that!, i thought it strings because its text!, thanks a lot for your help, i know about the values also but because the values is integers i thought that Enums is Strings!, thats my fault, thank you again, have a great day

Mohammad Hasanin