Ok i am writing a script that includes a portion where a file needs to be copied from one folder to the next. To add some intelligence to the process I want to rename the source file if a file of the same name already exists at the destination.
The process seems quite simple and its flow should be: set source folder; set destination folder; get the name of the first file in the source folder; see if the same name exists in the destination folder; if it does rename it by adding “001” to the filename; if it doesn’t exist then leave it alone.
The bit I can’t get to work is the renaming of the file! This I thought would be the easy bit!!!
I’ve trawled various message boards looking for a solution, have tried a number of methods but to no avail. I am sure my paths are correct because if I change the line that tries to rename the file to one that deletes the file and that works.
If anyone can tell me where I am going wrong I’d be very grateful. I’m running OS X 10.4.11
The script I’m using is this:
-- set path of the source folder
set hot_folder to "Macintosh HD:Users:User:Desktop:HotFolder:"
-- set path of the destination folder
set dest_folder to "Macintosh HD:Users:User:Desktop:DestinationFolder:"
--picks up the name of the second file in the source folder
tell application "System Events"
set file_name to name of file 2 of folder hot_folder
end tell
--set the full path of the file to be dealt with original path + name
set hot_file to hot_folder & file_name
--check to see if a file of the same name exists at the destination reports condition and renames original file if needed.
set dest_file_check to dest_folder & file_name
tell application "Finder"
if exists file dest_file_check then
display dialog dest_file_check & " already exists"
set AppleScript's text item delimiters to "."
set filenamePart1 to text item 1 of file_name
set filenamePart2 to text item 2 of file_name
set hot_file_new_name to hot_folder & filenamePart1 & "001." & filenamePart2 as string
set name of hot_file to hot_file_new_name
else
display dialog dest_file_check & " does not exist"
end if
end tell
When I run this script and it tries to rename I get an Error: Can’t set name of “blah blah…” to “blah blah001”