I have an InDesign file with 459 picture frames. I have written a script to populate those frames with images from the frontmost Finder folder. I want it to populate the images in random order. So, my plan was: write a script which chooses a random item from the folder and then deletes that item from the possible images to be placed so I don’t place an image twice. The script is below. It should work”so I think. But what I’m getting is of the 459 frames pulling from a folder of 366 images only 253 images get placed.
Am I missing something obvious here? If anyone would like more info, I can DropBox or YouSendIt the ID file and the source folder of images. Thanks for any help.
The script:
tell application "Finder"
set source_folder to folder of the front window
set theFiles to every file of source_folder
end tell
set theUsedList to {}
tell application "Adobe InDesign CS5.5"
activate
set myDocument to active document
tell myDocument
set List_1 to (object reference of every item of all page items of page 1 whose label is "poo")
repeat with t from 1 to count of items in List_1
set theFrame to item t of List_1
if not (exists graphic 1 of theFrame) then
set hh to random number from 1 to (count of items in theFiles)
if hh is not in theUsedList then
set thePath to item hh of theFiles as alias
tell theFrame
place thePath
copy hh to end of theUsedList
end tell
end if
end if
end repeat
end tell
end tell
use AS-Routine, Shanes’ ASObjC Runner.app or satimage.osax to remove used items from the filelist …
otherwise you’ll have to compare, compare, compare … which you’re just doing once at the moment → won’t work!
I would create a second empty list and once an item has been placed I would add it to this second list. Then ask AS to check if it’s already in this list and if not then place it.
Will take some time, but should work.
I thought that AS lists were mutable. you should be able to remove the item from the list though, and it would simplify the process. Not sure how, been awhile since I done standard AS…
remove listitems with Standardadditions won’t work. you’ll have to create new List on the fly …
But as he’s got the index it should be as simple as:
set myList to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set hh to random number from 1 to (count of items in myList)
if hh is equal to 1 then
set myList to items 2 thru -1 of myList
else if hh is equal to (count of items in myList) then
set myList to items 1 thru -2 of myList
else
set myList to items 1 thru (hh - 1) of myList & items (hh + 1) thru -1 of myList
end if
you could also give this a try:
global folderPath
tell application "Finder"
set folderPath to (folder of the front window) as text
end tell
set fileNameList to list folder folderPath without invisibles
set shuffledAliasList to my sl(fileNameList, count of fileNameList)
tell application "Adobe InDesign CS5.5"
activate
tell active document
set List_1 to (every item of all page items whose label is "poo")
set negIndex to 0
repeat with i from 1 to count of List_1
set theFrame to item i of List_1
if not (exists graphic 1 of theFrame) then
tell theFrame
try
place item (i - negIndex) of shuffledAliasList
end try
end tell
else
set negIndex to negIndex + 1
end if
end repeat
end tell
end tell
on sl(aList, nbr)
script L
property indexList : {}
property tmpList : {}
end script
set counter to 0
repeat nbr times
set counter to counter + 1
copy counter to end of L's indexList
end repeat
repeat nbr times
--Nigel¿
set myIndex to some integer of L's indexList
set myAlias to alias (folderPath & item myIndex of aList)
if (folder of (info for myAlias)) is false then copy myAlias to end of L's tmpList
set item myIndex of L's indexList to null
end repeat
return L's tmpList
end sl
This is great! Just what I was trying to get to. Very smart to shuffle the list first and then work through that list rather than trying to remove placed files from a single working list. Thanks very much.