0

I'm new to AppleScript so I'm sure it's something obvious. This works if I have selected multiple files (I use it with the right-click context menu), but if one file is selected, nothing happens (no errors).

The intention is to take some file called someRandom-imageIdontremember.jpg and use its Creation Date to rename to 2005-03-01_someRandom-imageIdontremember.jpg and likewise if I select multiple files.

on run {input, parameters}
    tell application "Finder"
        set selectedFiles to (get selection)
        repeat with i from 1 to (count selectedFiles)
            set aFile to item i of selectedFiles
            set fileName to name of aFile
            set {year:y, month:m, day:d} to creation date of aFile
            set dateStamp to (y as text) & "-" & text -2 thru -1 of ("0" & (m as integer)) & "-" & ¬
                text -2 thru -1 of ("0" & d)
            set name of aFile to dateStamp & "_" & fileName
        end repeat
    end tell
    
    return input
end run

Any suggestions/ideas are appreciated.

3
  • Your script works for me, but note that when using the Run AppleScript action the workflow arguments (selection, whatever) are already being passed in the input argument.
    – red_menace
    Commented Dec 17, 2023 at 21:38
  • thanks for responding, that's so weird that it worked. just with one file? thanks for the tip about the args
    – Rosie
    Commented Dec 17, 2023 at 22:33
  • oh wow, after editing to use input rather than (get selection), it worked with the one file. thanks again
    – Rosie
    Commented Dec 17, 2023 at 22:35

1 Answer 1

0

It works with one file when using the input passed on run, rather than (get selection)

on run {input, parameters}

    tell application "Finder"
        set selectedFiles to input
        repeat with i from 1 to (count selectedFiles)
            set aFile to item i of selectedFiles
            set fileName to name of aFile
            set {year:y, month:m, day:d} to creation date of aFile
            set dateStamp to (y as text) & "-" & text -2 thru -1 of ("0" & (m as integer)) & "-" & ¬
                text -2 thru -1 of ("0" & d)
            set name of aFile to dateStamp & "_" & fileName
        end repeat
    end tell
    
    return input
end run

thanks @red_menace

1
  • It shouldn't have made a difference, but welcome to Automator.
    – red_menace
    Commented Dec 17, 2023 at 23:04

You must log in to answer this question.

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