0

AppleScript newb here. After hours and hours I cannot find how to add leading zeroes to this rename script, which renames a bunch of pngs and jpgs etc. to the parent folder's name.

Currently it appends _1, _2 to the file name whereas I'd like it to be _001, _002

How would this script be rewritten to do that?

on run {input, parameters}

tell application "Finder"
    repeat with thisItem in input
        set folderName to name of thisItem
        set theseFiles to files of entire contents of thisItem
        repeat with i from 1 to count theseFiles
            set name of item i of theseFiles to ¬
                folderName & "_" & i & "." & ¬
                name extension of item i of theseFiles
        end repeat
    end repeat
end tell
end run

Thank you!

2
  • So you want to have the i variable with leading zeros? Should there be always two zeros in front or should it be 010 for i == 10?
    – nohillside
    Apr 30, 2022 at 17:58
  • Thank you! Good question. For sorting, it should be _001 up until _010 and then _100 Apr 30, 2022 at 20:00

1 Answer 1

1

One solution would be to add the number to a string of zeros and get the desired number of digits from the end. Using your script (you might want to add some error handling for duplicates, etc), it would be something like:

   repeat with i from 1 to count theseFiles
      set suffix to text -3 thru -1 of ("000000" & i)
      set name of item i of theseFiles to ¬
         folderName & "_" & suffix & "." & ¬
         name extension of item i of theseFiles
   end repeat
1
  • Thank you Red Menace! I will try this! Apr 30, 2022 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .