Search code examples
duplicatesapplescriptrename

Applescript to duplicate and rename


I'm attempting to create an AppleScript to duplicate a single file into a new location while renaming the file. I am getting hung up on the renaming of the file.

I've tried setting "with properties", setting aliases, and I still get errors.

tell application "Finder"
    set JobName to text returned of (display dialog "Please Enter Crate Name:" default answer "Job_Name")
    set loc to alias "Volumes:MusicExt:_Serato_:Subcrates:"
    set templatefile to alias "Volumes:MusicExt:Serato_Working:crate-template.crate"

    duplicate file templatefile to loc --> with properies {name:JobName}
    duplicate file templatefile to loc --> with properies {name:"10. Pre-CMY.m3u"}
 end tell

Solution

    • First of all HFS paths start always with a disk name and never with Volumes.
    • Second of all never put the file keyword in front of an alias specifier

    With the Finder you need two steps: Duplicate the file and then rename it.
    Take advantage of the return value of duplicate which is the duplicated file.

    tell application "Finder"
        set JobName to text returned of (display dialog "Please Enter Crate Name:" default answer "Job_Name")
        -- In this case just HFS string paths are preferable
        set loc to "MusicExt:_Serato_:Subcrates:"
        set templatefile to "MusicExt:Serato_Working:crate-template.crate"
    
        set duplicatedFile to duplicate file templatefile to folder loc
        set name of duplicatedFile to JobName -- (& ".crate") is there no file extension??
     end tell
    

    Note: with properties works only with the make command.


    An alternative is the cp shell command which can duplicate and rename a file simultaneously. The Finder is not involved in this case.

    set JobName to text returned of (display dialog "Please Enter Crate Name Without Extension:" default answer "Job_Name")
    set loc to "/Volumes/MusicExt/_Serato_/Subcrates/" & JobName & ".crate"
    set templatefile to "/Volumes/MusicExt/Serato_Working/crate-template.crate"
    do shell script "/bin/cp " & quoted form of templatefile & space & quoted form of loc