Hello all. I am working on my first applescript. It is a pretty simple script…but seeing as this in my first day working with it…I am pulling my hair out getting this to work.
All I want to do is have a folder action where…when a file is added to a specific folder…it duplicates the file to a different folder…I have that part working. What I want is for the duplicated file to be renamed…or add the current time to the name. Adding hte time would be great.
Any help for this newb is appreciated.
Here is what I am working with:
on adding folder items to this_folder after receiving these_items
tell application "Finder"
duplicate these_items to folder "Ruby:users:zookeeper:desktop:Backup"
end tell
end adding folder items to
This might work but it is far from perfect. It isn’t perfect because (1) it was written hastily, (2) it doesn’t check to see if the new file names are too long, (3) it could fail if the new file name already exists in the destination, (4) it needs more error checking and (5) it could likely be optimized (made more efficient). If you plan to rename batches of files on a regular basis, you can refine the script or use Jon Nathan’s excellent file naming utility, Name those Files!.
on adding folder items to this_folder after receiving these_items
set curr_time to my format_current_time()
repeat with item_ in these_items
set info_ to (info for item_)
if folder of info_ is false then
if name extension of info_ is missing value then
set new_name to (name of info_ & " " & curr_time)
else
set name_ext to name extension of info_
set ext_length to (count name_ext) + 2
set new_name to (text 1 thru -ext_length of (name of info_)) & " " & curr_time & "." & name_ext
end if
tell application "Finder"
try
set dupe_ to (duplicate item_ to folder "Ruby:users:zookeeper:desktop:Backup")
set name of dupe_ to new_name
on error e
display dialog e
end try
end tell
end if
end repeat
end adding folder items to
on format_current_time()
set time_ to time string of (current date)
try
set text item delimiters to " "
set time_ to time_'s (text items 1 thru -2) as text
set text item delimiters to ":"
set time_ to time_'s text items
set text item delimiters to "_"
set time_ to time_'s (text items 1 thru end) as text
set text item delimiters to ""
on error
set text item delimiters to ""
end try
time_
end format_current_time
– Rob (who should never write scripts before consuming the recommended daily dose of coffee)
It does a great job at adding the current time to the backup folder, but it does not change the name of the original file…that is, when I add a file to the Save folder (the folder with the action attached to it), the name is unchanged. It duplicates to the Backup folder and adds the date, but needs to change the name when items enter the Save folder.
Do you want the name changed before or after the duplicate file is created? Changing it before means that the duplicate and original will have the same name. If that’s what you want, this might work.
on adding folder items to this_folder after receiving these_items
set curr_time to my format_current_time()
repeat with item_ in these_items
set info_ to (info for item_)
if folder of info_ is false then
if name extension of info_ is missing value then
set new_name to (name of info_ & " " & curr_time)
else
set name_ext to name extension of info_
set ext_length to (count name_ext) + 2
set new_name to (text 1 thru -ext_length of (name of info_)) & " " & curr_time & "." & name_ext
end if
tell application "Finder"
try
set name of item_ to new_name
update item_
duplicate item_ to folder "Ruby:users:zookeeper:desktop:Backup"
on error e
display dialog e
end try
end tell
end if
end repeat
end adding folder items to
on format_current_time()
set time_ to time string of (current date)
try
set text item delimiters to " "
set time_ to time_'s (text items 1 thru -2) as text
set text item delimiters to ":"
set time_ to time_'s text items
set text item delimiters to "_"
set time_ to time_'s (text items 1 thru end) as text
set text item delimiters to ""
on error
set text item delimiters to ""
end try
time_
end format_current_time
Thanks a alot for the help…I really appreciate it. It did change the file name…but it kept on doing it…every second! I just quit the Finder…I do think this is what I want. I have realized that I am not sure folder actions will “see” a modification in a file…that is…this works great when dropping a file in the folder…but if I am working on a document…and hit “save”…the script does not recognize the modified file as new…so it does not duplicate it…If I do Save as and replace the old copy with the new (provided it has hte same name)…it does not duplicate the file. I have to change the name. That’s why I thought the initial name change would work…I play with it a while.
How do I get the the name change out of the loop?
The folder action is being triggered each time the name of the file is changed so I suspect that you’ll need to move the files to another folder and then rename them to avoid the issue.
Regarding modified file content, there is no folder action handler that detects files that have been modified.