Duplicating an Item

When I try to script duplicating page items in Quark, it duplicates the first item 9 times rather than duplicate 9 items one time each. How do I get it to duplicate the first item just one time and then move on to the second item?
Any help would be greatly appreciated!

on open of droppedFiles --> This is the line of code that begins a droplet. 
   with timeout of 900 seconds -->The script will try for 15 minutes before timing out. Default is 2 minutes, which is way too little.
       
       
       
       tell application "QuarkXPress"
           activate
           
           repeat with afile in droppedFiles
               open afile as alias
               
               
               
               tell document 1
                   
                   set pictureBoxes to every picture box
                   
                   repeat with i from 1 to number of pictureBoxes
                       set pictureBox to item i of pictureBoxes
                       set duplicateBox to duplicate pictureBox to after pictureBox
                   end repeat
                   
                   close saving yes
                   
                   
               end tell --end of tell document 1 block
           end repeat --end of first repeat loop
       end tell --end of tell Quark block
   end timeout
end open --> This is the line of code that ends a droplet.

I tried telling the script to


set locked of pictureBox to true
set locked of duplicateBox to true

I was hoping this would prevent if from continuing to duplicate the first picture box over and over again.
It locked it alright but continued duplicating the first item and ignoring the 2nd, 3rd, 4th, etc. despite the first item being locked. I also considered grouping the picture box to the newly duplicated box, but I’m having trouble figuring out how to group items via applescript in Quark. Any help would be greatly appreciated. Quark is driving me crazy! :slight_smile:

on open of droppedFiles --> This is the line of code that begins a droplet. 
with timeout of 900 seconds -->The script will try for 15 minutes before timing out. Default is 2 minutes, which is way too little.



tell application "QuarkXPress"
activate

repeat with afile in droppedFiles
open afile as alias



tell document 1

set pictureBoxes to every picture box

repeat with i from 1 to number of pictureBoxes
set pictureBox to item i of pictureBoxes
set duplicateBox to duplicate pictureBox to after pictureBox

set locked of pictureBox to true
set locked of duplicateBox to true

end repeat

close saving yes


end tell --end of tell document 1 block
end repeat --end of first repeat loop
end tell --end of tell Quark block
end timeout
end open --> This is the line of code that ends a droplet.

I don’t have Quark so I may not be very useful here, but if it’s anything like Illustrator, I have an idea what might be happening. Code the same as yours in Illustrator would result in 9 copies of the first item.

Here’s what I’d guess is happening: the “items” in your list pictureBoxes are relative references by object type/position, not some kind of absolute reference/identifier, so every time you duplicate one, you’re shifting which object these references refer to.

set pictureBoxes to every picture box

repeat with i from 1 to number of pictureBoxes
set pictureBox to item i of pictureBoxes
set duplicateBox to duplicate pictureBox to after pictureBox

If you were to dig into what “pictureBoxes” contains, based on my experience with Illustrator, it would be the equivalent of a list of elements like “Picture Box 1 of Document 1, Picture Box 2 of Document 1, etc.”

So your code gets the first item, “Picture Box 1 of Document 1” and duplicates it to just after itself. Now the second picture box, “Picture Box 2 of document 1,” is the copy of the first picture box, not the original “Picture Box 2.” So the next iteration through the repeat copies the duplicate you just made. That duplicate, the second copy, is now Picture Box 3 of Document 1, and gets copied, and on down the list, with it copying each copy over and over until it’s done.

So, ways around this. I tend to just work backwards:

set pictureBoxes to every picture box

repeat with i from number of pictureBoxes to 1 by -1
set pictureBox to item i of pictureBoxes
set duplicateBox to duplicate pictureBox to after pictureBox

So first it duplicates Picture Box 9 to after itself, so that’s Picture Box 10 now. That item doesn’t exist in your list, so it won’t cause any problems. Next it does 8. The copy of 8 would now be Picture Box 9, but it doesn’t matter that that (now inaccurate) reference is already in your list, because you already processed it. And onward, etc.

If I’m right about the problem, but the working backwards solution doesn’t work for you, another option is to look for a unique identifier to reference. Do the objects have unique names you can use? Does Quark assign unique object ID’s you can access and reference them by?

Anyway, best of luck, let me know if this helps.

@pcLoadLetter

May you take care to write the name of the target application in the title of your question ?
When a title appears as : “Duplicating an Item”, nobody may guess which is the related application and we must enter the thread to discover what it is about.

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) mardi 27 septembre 2016 17:09:05

Hi pcLoadLetter,

I answered your question on Publi-Script yesterday (well I suppose it was you :-)). And my answer was the same as t.spoon.
Like Yvan says, it’s better to name the app in the Subject field otherwise one cannot know what you’re talking about from there (I personnaly thought you were working in Finder).