Re: move theFile to thePath replacing no
Re: move theFile to thePath replacing no
- Subject: Re: move theFile to thePath replacing no
- From: Nigel Smith <email@hidden>
- Date: Fri, 25 Jul 2003 17:42:11 +0100
On 25/7/03 15:39, "Gary Lists" <email@hidden> wrote:
>
Do you wish to change a file name? Is that all? Replace a file with
>
another? What?
He's trying to emulate URLAccess's behaviour. Move a file to a folder and,
if a file already exists with the same name, rename the new file.
The tricky bit is renaming the file. Prefixing or suffixing with a number is
easiest, but the first mucks up alphabetical listings and the second messes
up file endings. Best, but most difficult, way is to insert something before
the last dot in the name, if the name has a dot, otherwise at the end of the
name.
Something like this will do it on OSX. Note that if you are still using OS9
you will have to add a name-length check.
--begin script
set extension to ""
set theFile to choose file
set theFolder to choose folder
tell application "Finder"
set fileName to name of theFile
set fileContainer to (container of theFile as alias) as text
if not (exists file ("" & theFolder & fileName)) then
tell application "Finder"
move theFile to theFolder
end tell
else
if fileName contains "." then
set {AppleScript's text item delimiters, oldTIDs} <nobreak>
to {".", AppleScript's text item delimiters}
set extension to "." & (text item -1 of fileName as text)
set fileName to text items 1 thru -2 of fileName as text
set AppleScript's text item delimiters to oldTIDs
end if
set x to 1
repeat until not (exists file ("" & theFolder & <nobreak>
fileName & "-" & x & extension))
set x to x + 1
end repeat
set fileName to fileName & "-" & x & extension
set name of theFile to fileName
move file (fileContainer & fileName) to theFolder
end if
end tell
--end script
That'll get you started -- until the list gurus show you a better way!
Later,
Nigel
_______________________________________________
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.