Duplicate file: syntax problem

Hi people,

I’ve got a problem with my applescript that I need a little bit of help on. The purpose of the script is to:

Duplicate a file (myfile-orig) into myfile-7January2003. The code is below.

tell application “Finder”

set theDate to current date
set myDay to (day of theDate) as string
set myMonth to (month of theDate) as string
set myYear to (year of theDate) as string

set myDestination to {"myfile-", myDay, myMonth, myYear} as string
set myDestination to reference

duplicate "myfile-orig" of startup disk to "g4_griff:" & myDestination

end tell

I know the duplicate syntax is incorrect. I’m not sure what is the best way to go about the duplicate and name change since I cannot find a rename filesystem applescript command.

Any help would be most appreciated!

Thanks!

Steve Griff

Hi, Steve.

The Finder’s ‘duplicate’ command needs to be given the original item (as a Finder reference or as an alias) and the folder or disk to which you want to duplicate it (ditto). The command returns a Finder reference to the duplicated item, and it’s then easy to set the name of this to the new name.

set theDate to current date
set myDay to (day of theDate) as string
set myMonth to (month of theDate) as string
set myYear to (year of theDate) as string

set nameForDuplicate to {"myfile-", myDay, myMonth, myYear} as string

tell application "Finder"
  set myDuplicate to (duplicate file "myfile-orig" of startup disk to disk "g4_griff:")
  set name of myDuplicate to nameForDuplicate
end tell

If you’re feeling particularly flash, you can of course combine the duplication and renaming in one line: :slight_smile:

tell application "Finder"
  set name of (duplicate file "myfile-orig" of startup disk to disk "g4_griff:") to nameForDuplicate
end tell

If you want to duplicate the file to the same folder as the original, you simply leave off the ‘to disk “g4_griff:”’. Unfortunately, there’s a problem in this case if you’re using OS 8.6. Because of a bug, 8.6’s ‘duplicate’ command doesn’t return the correct result when duplicating to the same folder as the original. (It returns an empty list when the destination’s not specified; it returns the original item if the source folder is specified as the destination.) I don’t know of any foolproof ways of dealing with this, but one possibility is:

-- To rename a duplicate in OS 8.6 when the destination's the same folder
tell application "Finder"
  -- Duplicate the item to *another* folder and get an alias to the result
  -- The alias will still find the duplicate after it's renamed
  set myDuplicate to (duplicate file "myfile-orig" of startup disk to desktop) as alias
  -- Rename the duplicate
  set name of myDuplicate to nameForDuplicate
  -- Move the duplicate to the same folder as the original
  move myDuplicate to startup disk
end tell

This convolution isn’t required in any version (I think) of OS 9.