Well. I’m glad you seem to be enthusiastic despite it not doing what you want.
Getting what you want requires a bit of a rethink. As a test, I’ve tried putting together your Automator workflow. I can’t get “Filter Finder Items” to pass anything at all, but without it, the “Run AppleScript” action receives aliases, which is good to know. In fact though, the three-line, commented-out ‘tell’ statement at the top of the script below can, if you prefer, be uncommented and used instead of the first three actions in the workflow.
The script contains its own sort (for my own peace of mind), generates its own number sequences, and uses two or three digits in the names, depending on whether there are less than 100 JPEGs in the folder or 100-or-more.
on run {input}
-- If preferred, the next three lines can be uncommented and used instead of the preceding automator actions.
-- tell application "Finder"
-- set input to files of entire contents of folder "Rename" of desktop as alias list
-- end tell
-- Get the equivalent HFS paths for all the aliases in the input.
set filePaths to getHFSPaths(input)
-- For safety, ensure the paths are sorted by folder and then file name, considering numeric strings.
considering numeric strings
ShellSort(filePaths, 1, -1)
end considering
-- Rename JPG/JPEG files as required.
renameJPGs(filePaths)
return input -- Aliases still valid after files are renamed.
end run
-- Rename JPGs/JPEGs.
on renameJPGs(filePaths)
script o
property fp : filePaths
end script
-- Get a list of lists containing the numbers of {files, JPEGs} in the folders, in the same order as the folders occur in filePaths.
set folderCounts to getFolderCounts(filePaths)
-- Initialise various indices and counters.
set f to 0 -- Index into folderCounts.
set thisFolderCount to 0 -- The number of files in the current folder (item 1 of item f of folderCounts).
set flleCounter to 1 -- File counter within the current folder.
set jpgCounter to 1 -- Number to use in the next JPG/JPEG name.
repeat with i from 1 to (count filePaths)
-- Increment the file counter. If more than the number of files in the current folder, reset for the next folder.
set flleCounter to flleCounter + 1
if (flleCounter > thisFolderCount) then
set f to f + 1
set {thisFolderCount, thisJPGCount} to item f of folderCounts
if (thisJPGCount < 100) then
set d to -2 -- Use two digits in names when < 100 JPEGS in the folder.
else
set d to -3 -- Otherwise use three.
end if
set flleCounter to 1
set jpgCounter to 1
end if
-- Get the next path from the list and rename the file if it's a JPG or JPEG.
set thisPath to item i of o's fp
if ((thisPath ends with ".jpg") or (thisPath ends with ".jpeg")) then
tell application "Finder"
set name of file thisPath to "Photo " & text d thru -1 of ((1000 + jpgCounter) as text) & ".jpg"
set jpgCounter to jpgCounter + 1
end tell
end if
end repeat
end renameJPGs
-- Given a list of ordered file paths, return a list of lists containing the numbers of {files, JPEGs} in the folders involved.
on getFolderCounts(filePaths)
script o
property fp : filePaths
end script
set folderCounts to {}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set thisPath to beginning of o's fp
set currentFolder to text 1 thru text item -2 of thisPath
set c to 1
set j to ((thisPath ends with ".jpg") or (thisPath ends with ".jpeg")) as integer
repeat with i from 2 to (count filePaths)
set thisPath to item i of o's fp
set thisFolder to text 1 thru text item -2 of thisPath
if (thisFolder is currentFolder) then
set c to c + 1
if ((thisPath ends with ".jpg") or (thisPath ends with ".jpeg")) then set j to j + 1
else
set end of folderCounts to {c, j}
set currentFolder to thisFolder
set c to 1
set j to ((thisPath ends with ".jpg") or (thisPath ends with ".jpeg")) as integer
end if
end repeat
set end of folderCounts to {c, j}
set AppleScript's text item delimiters to astid
return folderCounts
end getFolderCounts
-- Return a list of equivalent HFS paths from a list of aliases.
on getHFSPaths(aliasList)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set filePaths to paragraphs of (aliasList as text)
set AppleScript's text item delimiters to astid
return filePaths
end getHFSPaths
(* Shell sort, by Donald Shell, 1959.
AppleScript implementation: Nigel Garvey, 2010.
Parameters: (list, range index 1, range index 2)
*)
on ShellSort(theList, l, r)
script o
property lst : theList
end script
set listLen to (count theList)
if (listLen > 1) then
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1
if (l > r) then set {l, r} to {r, l}
set inc to (r - l + 1) div 2
repeat while (inc > 0)
repeat with j from (l + inc) to r
set v to item j of o's lst
repeat with i from (j - inc) to l by -inc
tell item i of o's lst
if (it > v) then
set item (i + inc) of o's lst to it
else
set i to i + inc
exit repeat
end if
end tell
end repeat
set item i of o's lst to v
end repeat
set inc to (inc / 2.2) as integer
end repeat
end if
return -- nothing.
end ShellSort