Skip to main content
M.Hasanin
Inspiring
April 29, 2022
Answered

Trying to Adjust Items in One Side by Item Type Selected

  • April 29, 2022
  • 1 reply
  • 906 views

Dear Experts, i wrote simple scripts to move items (six types of them) 3mm in vertical direction , it work fine with no conditions in page or spread but when i decide to move the items in even or odd pages by selection it ignore the logical operator (OR) and move all the items in the all pages (even or odd) !, this is strange or maybe i did somethig wrong or miss something, because it shouid only move the same selected item type only not all items!, i used (OR) ! , here is the code :

 

//MySelection Items Adjuster -- Odd or Even Pages
function MySelectionAdjuster() {
    var mySelection = app.selection;
    var allDocPages = app.documents[0].pages.everyItem().getElements();
    // Loop all pages:
    for (var n = 0; n < allDocPages.length; n++) {
        // Test Left or Right Hand side of page:
        //if (allDocPages[n].side == PageSideOptions.LEFT_HAND) { //Left (Odd)
        if (allDocPages[n].side == PageSideOptions.RIGHT_HAND) { //Right (Even)
            // Get all items on the current page:
            var mySelection = allDocPages[n].allPageItems;
            for (var i = 0; i < mySelection.length; i++) {
                if (mySelection[i].constructor.name == "TextFrame" ||
                mySelection[i].constructor.name == "Rectangle" || 
                mySelection[i].constructor.name == "Oval" || 
                mySelection[i].constructor.name == "Polygon" || 
                mySelection[i].constructor.name == "Group" || 
                mySelection[i].constructor.name == "GraphicLine"
                ) {
                    var mySelected = mySelection[i];
                    mySelected.move(undefined , [0 ,"3mm"]);
                }
            }
        }
    }
                    alert("Amount of "+mySelection.length+" items Moved!","Finished" );
}
app.doScript(MySelectionAdjuster, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Move Selected Items");

 

 so please help to identify the script logic problem, and thanks in advance

This topic has been closed for replies.
Correct answer m1b

Hi @m1b  , I have another idea, its faster in execution but it only let me select one item type in a time!,can the following script adjusted to let me select any item types in the same time and move those item types selected together in the same time instead of repeating selecting for each item ? , can i contacentae all those arrays into one? and then the script will move all the items at once? not each item type? and thanks

//Start Moving Each Items Type Seperatly by Selecting One Item Type
function AdjustSelectedItems(){
   try {
            var mySelection = app.selection[0];
            switch (mySelection.constructor.name) {
            case 'TextFrame':
              var allFrames = app.documents[0].pages.everyItem().textFrames.everyItem();
              break;
            case 'Rectangle':
              var allFrames = app.documents[0].pages.everyItem().rectangles.everyItem(); 
              break;
            case 'Oval':
              var allFrames = app.documents[0].pages.everyItem().ovals.everyItem();        
              break;
            case 'Polygon':
              var allFrames = app.documents[0].pages.everyItem().polygons.everyItem();      
              break;
            case 'GraphicLine':
              var allFrames = app.documents[0].pages.everyItem().graphicLines.everyItem();        
              break;
            case 'Group':
              var allFrames = app.documents[0].pages.everyItem().groups.everyItem();
              break;
        }
        //Start Moving Each Items Type Seperatly
        allFrames.move(undefined , [0 ,"10mm"]); 
        myCollected = allFrames.getElements();
        alert("Amount of "+myCollected.length+" items Moved!","Finished" );
        } catch(myerror) { 
        alert (myerror,"OOps!");
        exit();
    }
}

AdjustSelectedItems();

 


This makes things a bit more complex. See the following script and see if you can follow the logic. Here we collect the selected item types at the start, and later we check each page item's type against that array. We only move the item if the page item's type matches *any* item in the selected types array.

- Mark

 

//MySelection Items Adjuster -- Odd or Even Pages
function MySelectionAdjuster() {
    if (app.selection.length == 0) {
        alert('Please select an item to set type.');
        return;
    }
    // collect all selected item types
    var mySelectionTypes = [];
    selectionLoop:
    for (var i = 0; i < app.selection.length; i++) {
        var type = app.selection[i].constructor.name;
        // add type only if it hasn't been added already
        for (var j = 0; j < mySelectionTypes.length; j++)
            if (mySelectionTypes[j] == type)
                continue selectionLoop;
        mySelectionTypes.push(type);
    }
    var allDocPages = app.documents[0].pages.everyItem().getElements();
    var counters = [];
    // Loop all pages:
    for (var n = 0; n < allDocPages.length; n++) {
        // Test Left or Right Hand side of page:
        // if (allDocPages[n].side == PageSideOptions.LEFT_HAND) { //Left (Odd)
        if (allDocPages[n].side == PageSideOptions.RIGHT_HAND) { //Right (Even)
            // Get all items on the current page:
            var mySelection = allDocPages[n].allPageItems;
            pageItemsLoop:
            for (var i = 0; i < mySelection.length; i++) {
                var mySelected = mySelection[i];
                // check against each selection type
                for (var j = 0; j < mySelectionTypes.length; j++) {
                    if (mySelectionTypes[j] == mySelected.constructor.name) {
                        // move it
                        mySelected.move(undefined, [0, "30mm"]);
                        counters[j] = counters[j]==undefined ? 1 : counters[j] + 1;
                        continue pageItemsLoop;
                    }
                }
            }
        }
    }
    var message = "Moved these items:\n"
    for (var j = 0; j < mySelectionTypes.length; j++) {
        message += mySelectionTypes[j] + ": " + counters[j]+'\n';
    }
    alert(message, "Finished");
}

app.doScript(MySelectionAdjuster, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Move Right-hand Page Items");

 

1 reply

m1b
Community Expert
Community Expert
April 29, 2022

Hi @M.Hasanin, actually when I run your script it works as I would expect—it only moves items on the right-hand pages. Perhaps you could try on a simple test document with facing pages to make sure it isn't something about your document that is different to my test?

- Mark

 

(By the way, a minor issue was that your way of counting doesn't work—it shows zero every time. See script slightly adjusted below).

//MySelection Items Adjuster -- Odd or Even Pages
function MySelectionAdjuster() {
    var mySelection = app.selection;
    var allDocPages = app.documents[0].pages.everyItem().getElements();
    var counter = 0;
    // Loop all pages:
    for (var n = 0; n < allDocPages.length; n++) {
        // Test Left or Right Hand side of page:
        // if (allDocPages[n].side == PageSideOptions.LEFT_HAND) { //Left (Odd)
            if (allDocPages[n].side == PageSideOptions.RIGHT_HAND) { //Right (Even)
            // Get all items on the current page:
            var mySelection = allDocPages[n].allPageItems;
            for (var i = 0; i < mySelection.length; i++) {
                var mySelected = mySelection[i];
                if (mySelected.constructor.name == "TextFrame"
                    || mySelected.constructor.name == "Rectangle"
                    || mySelected.constructor.name == "Oval"
                    || mySelected.constructor.name == "Polygon"
                    || mySelected.constructor.name == "Group"
                    || mySelected.constructor.name == "GraphicLine"
                ) {
                    mySelected.move(undefined, [0, "3mm"]);
                    counter++
                }
            }
        }
    }
    alert("Amount of " + counter + " items Moved!", "Finished");
}

app.doScript(MySelectionAdjuster, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Move Right-hand Page Items");

 

M.Hasanin
M.HasaninAuthor
Inspiring
April 29, 2022

Thanks alot @m1b 

I know the Script is working good but the problem i expect it will only move the selected item type, as example if i select the one rectangle i expected that it will find and move the all rectangles same type in all even pages, but what i get is its moving all items, because of that i mention i used (or) logical operator but it still move all six types!, thats my question, you can put various types like text frames and rectangles and ovals and you will noticed that it move all items at atime even if you select any type of them, thats not was my expecting, i expect it will move only same type, i hope you get the idea, and thanks for fixing the counter, in the below version it move all selected types even if one selected but what i expect in the above version that only item type selected will be moved :

//Document Items Shifter
function DocSelectedShifter() {
    var mySelection = app.selection;
    var counter = 0;
            for (var i = 0; i < mySelection.length; i++) {
                if (mySelection[i].constructor.name == "TextFrame" ||
                mySelection[i].constructor.name == "Rectangle" || 
                mySelection[i].constructor.name == "Oval" || 
                mySelection[i].constructor.name == "Polygon" || 
                mySelection[i].constructor.name == "Group" || 
                mySelection[i].constructor.name == "GraphicLine"
                ) {
                    var mySelected = mySelection[i];
                    mySelected.move(undefined , [0 ,"3mm"]);
                    counter++
        }
    }
    alert("Amount of " + counter + " items Moved!", "Finished");
}
app.doScript(DocSelectedShifter, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Move Selected Items");
Mohammad Hasanin
m1b
Community Expert
Community Expert
April 30, 2022

Your code says:

if (the page item is Rectangle or Oval or Polygon or Group or GraphicLine) then move it.

 

So yes, it will move all of those items.

 

What behaviour do you want?

- Mark