Copy link to clipboard
Copied
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 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.');
r
...
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi @m1b
The behaviour i want is when selecting as example (Rectangle) one or two only the rectangles items will moved not all the pageitems, maybe my problem in this line :
var mySelection = allDocPages[n].allPageItems;
so instead of moving all page items it shouid only move the selected item type but i dont know how to do it, so if i select only rectanlge or rectangles it will move all rectangles, i think the problem in this line but how to fix it to only move the item type? and thank you again
Copy link to clipboard
Copied
Oh I see now. This is your script but modified to handle only one type of page item—the type that is selected when the script is run. See what you think.
- Mark
//MySelection Items Adjuster -- Odd or Even Pages
function MySelectionAdjuster() {
if (app.selection.length == 0) {
alert('Please select an item to set type.');
return;
}
var mySelectionType = app.selection[0].constructor.name;
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 == mySelectionType) {
mySelected.move(undefined, [0, "30mm"]);
counter++
}
}
}
}
alert("Amount of " + counter + " " + mySelectionType + "s Moved!", "Finished");
}
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
Thanks a lot @m1b thats what i mean but please can you explain what this lines means :
selectionLoop:
and
pageItemsLoop:
the other logic i can understand, thanks alot again, i learned a lot today
Copy link to clipboard
Copied
They are labels for the loops. The labels can be anything you like. You don't normally need to give loops labels, but in this case I wanted to specifically "continue" a particular loop. For example the "continue" statement was inside the var j loop but I wanted it to continue the outer var i loop. See Using Continue With A Label.
- Mark
Copy link to clipboard
Copied
Thanks a lot @m1b , I wish you a great day, cheers
Copy link to clipboard
Copied
You're very welcome. 🙂
- Mark