I have a script which i have hooked up as a folder action, but whenever I run it, the main repeat loop just doesn’t repeat. Its meant to repeat for each of the items in input, but it only repeats for item 1 of the input. Any ideas?
on run {input, parameters}
tell application "Finder"
set theCount to (number of items of input)
repeat with i from 1 to theCount
set theFile to (item i of input as alias)
set n to 0
repeat while ("/path/to/folder/" & n & ".jpg") exists
set n to n + 1
end repeat
set newName to (n & ".jpg") as string
set name of file theFile to newName
end repeat
end tell
end run
Hi.
The Finder doesn’t recognise POSIX paths, so the inner repeat condition is never true and n is never incremented.
Assuming that ‘input’ is a list, and that it contains aliases to files or objects which can be coerced to such, and that the object of the exercise is to give each file a new name which isn’t already taken in the folder where it currently resides, your script should look something like this:
on run {input, parameters}
tell application "Finder"
set theCount to (count input)
repeat with i from 1 to theCount
set theFile to (item i of input as alias)
set theContainer to theFile's container
set n to 0
repeat while (file ((n as text) & ".jpg") of theContainer exists)
set n to n + 1
end repeat
set newName to (n as text) & ".jpg"
set name of file theFile to newName
end repeat
end tell
end run
Thank you, works perfectly
EDIT: Made more time efficient by changing location of “set n to 0”
on run {input, parameters}
tell application "Finder"
set theCount to (count input)
set n to 0
repeat with i from 1 to theCount
set theFile to (item i of input as alias)
set theContainer to theFile's container
repeat while (file ((n as text) & ".jpg") of theContainer exists)
set n to n + 1
end repeat
set newName to (n as text) & ".jpg"
set name of file theFile to newName
end repeat
end tell
end run