Search code examples
macosapplescriptrenameautomator

AppleScript to append date to file


I was recently able to make a drag and drop script in Automator that allowed me to zip and name a file and then automatically apply the date (DDMMYY) but now it's defaulting to (DDMMYYYY) and I can't change it. I've googled for a solution and nothing works since this needs to be at the end of the file name.

Does anyone know what I'm doing wrong or does anyone have an actual script that can help me? Everything I've found only works if the date is at the start of the file name, not at the end (but before the extension).

Workflow from Automator


Solution

  • You can use this AppleScript inside a Run AppleScript action, it renames the archive inserting the date before the file extension

    on run {input, parameters}
        set thePath to POSIX path of (item 1 of input)
        set currentDate to do shell script "date +%d%m%y"
        set newPath to text 1 thru -5 of thePath & "_" & currentDate & ".zip"
        do shell script "mv " & quoted form of thePath & space & quoted form of newPath
        return {POSIX file newPath as alias}
    end run