Re: Duplicate command syntax [harder than it sounds?]
Re: Duplicate command syntax [harder than it sounds?]
- Subject: Re: Duplicate command syntax [harder than it sounds?]
- From: has <email@hidden>
- Date: Thu, 4 Jul 2002 16:20:04 +0100
Matthew Smith wrote:
>
> Can anyone tell me what's wrong with this script?
>
>
>
> tell application "Finder"
>
> duplicate "Macintosh HD:file1.htm" to "Macintosh HD:file2.htm"
>
> end tell
>
>
>
You are telling the Finder to duplicate a string to another string.
>
Shouldn't you be duplicating a file?
>
>
tell application "Finder"
>
set newFile to duplicate file "Macintosh HD:file1.htm" to folder
>
"Macintosh HD:"
>
set name of newFile to "file2.htm"
>
end tell
Technically this is correct, although it's open to possible name-clashing
problems:
1. if you duplicate "file1.htm" to the new folder, but there's already a
"file1.htm" there
2. if you try to rename the moved file to "file2.htm", but there's already
a "file2.htm" there
Frustrating, no? The *proper* solution would be for the Finder's duplicate
and move commands to provide an option for simultaneously renaming the file
as it's copied/moved. In the absence of such an Apple-side fix, however, a
couple possibilities spring to mind if this will be a possible problem for
you:
1. you could duplicate/move files to an otherwise empty temp folder, rename
there and then move to the final location. The need for a dedicated temp
folder is somewhat clumsy, however, and is just more cruft added to the
filesystem. On the other hand, it's still much simpler than the
alternative...
2. you could take active measures to avoid name collision problems while
moving and renaming files:
- test the source folder to see if a file with the new name already exists:
- if no, rename the file there [1] before moving it to its
destination. [done]
- if yes, test if a file with the old name already exists in the
destination folder:
- if no, move the file to the destination folder and rename
there. [done]
- if yes, see [2] below
[1] I have a strong suspicion/recollection that duplicating a file in-situ
returns a reference to the original, not the copied file (though I don't
know if this is a bug on certain OS versions of a permanent 'feature').
This being true would put a pretty big black mark against any 'duplicate
in-situ' approach, meaning you'd *always* have to duplicate the file to a
new location first (some location... ANY location! as that will always
return a reference to the copied file).
[2] You're in a minor pickle now, as I can't think offhand of a really neat
solution to the problem. The ones I can think of are:
i. use a temp folder [or some existing folder in the filesystem where you
know you won't get name collisions] as per 1.
ii. temporarily rename one of the files you'd otherwise collide with, then
set its name back once you're done. But messing with unrelated files would
be a big no-no in my book as it might cause nasty side-effects elsewhere.
iii. give the file to be moved a unique temporary name, eg
"28945348file1.htm", move it to the destination folder and rename it there
to its final name, "file2.htm". Unfortunately, this doesn't deal with the
possibility that the destination folder already has an older "file2.htm"
which should be overwritten by the new one. You could use the Finder's
delete command, but this isn't ideal as you want to destroy the old file
completely, not merely move it to the trash.
--
Grief! And they say scripting's supposed to be easy... Can anyone see a
simpler solution, or is there anything obvious I've missed?
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.