Is it possible to have a folder that as files are dropped into it, will remove the first 2 characters (they are actually spaces), and the add .df1 to the filenames?
I need to do this to several database files each month to make them compatiable with what is required on PC. (no leading spaces and .df1 on the end)
Yes, it’s possible with a Folder Action script. ScriptBuilders has several renaming scripts which could probably be adapted. It would be helpful if you provided more info when asking for assistance, such as Mac OS version, AppleScript version, etc. It helps us to help you.
I use Mac OS 8.6 and AS v1.1.3
I have found that the following works to accomplish the desired action.
It should be attached (as a folder action) to the folder to which you are adding the items.
It requires Akua Sweets osaxen available at MacScripter.
suggest a title “add-Rename to .dfl”
on adding folder items to this_folder after receiving added_items
tell application “Finder”
set theItems to every item of added_items
repeat with thisItem in theItems
get basic info for thisItem – requires Akua Sweets scripting addition
if kind of result is file then --checks to insure that item is a file, not a folder or disk
set oldName to name of thisItem
get character 1 of oldName & character 2 of oldName – gets 1st 2 characters of file name
if result is equal to " " then --checks that 1st 2 characters are spaces
set the new_name to (characters (2 + 1) thru -1 of the oldName) as string --removes 1st 2 characters
set name of thisItem to new_name & “.df1”
end if
end if
end repeat
end tell
end adding folder items to
PS: I have found the extension “Folder Actions Plus” very helpful because it causes execution of the attached folder action even when the folder is NOT open.
Is can be found at: http://sweeney-grant.com/eagrant/
Thanks for your quick reply. I’m new to MacScripter and will post version numbers in the future.
I’m running Mac OS 9.2.2 with AppleScript version 1.8.3, with Folder Actions v1.6.
I have installed Folder Actions Plus V1.1b3 and have AKUA Sweets 1.4.3 in the Scripting additions folder.
I have tried the script that Paul Courtade (Thanks Paul) so kindly suggested and it does not work. Maybe I am doing something wrong, but I can’t see what.
Well, I’ve made some progess on this Applescript task. (Thanks everyone) The code below works,but still need some more help.
This will only work the first time. The files I need to rename, for example Readme1, will be renamed readme1.df1, but if I change Readme1 and drop it into the folder again it will not rename it, replacing the other file. Instead I end up with Readme1 and Readme1.df1. So my questions is how to get the script to delete the eariler file it the name already there.
Thanks in advance,
Vic
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
--get the name of the folder
set this_folder_name to the name of this_folder
end tell
-- find out how many new items have been placed in the folder
set new_item_count to the number of items in the added_items
tell application "Finder"
set theItems to every item of added_items
repeat with thisItem in theItems
set oldName to name of thisItem
get character 1 of oldName -- gets 1st character of file name
if result is equal to " " then --checks that 1st character is a space
set the new_name to (characters (1 + 1) thru -1 of the oldName) as string --removes 1st character
set name of thisItem to new_name & ".df1"
end if
end repeat
end tell
end try
end adding folder items to
I’ve been tinkering with your code and think I may be able to help.
The following seems to work in OS 8.6 for me - give it a try !
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
--get the name of the folder
set this_folder_name to the name of this_folder
end tell
-- find out how many new items have been placed in the folder
--set new_item_count to the number of items in the added_items (doesn't appear to be needed)
tell application "Finder"
set theItems to every item of added_items
repeat with thisItem in theItems
set oldName to name of thisItem
get character 1 of oldName -- gets 1st character of file name
if result is equal to " " then --checks that 1st character is a space
set the new_name to ((characters (1 + 1) thru -1 of the oldName) as string) & ".df1" --removes 1st character & adds ".df1"
if file new_name of the folder this_folder exists then
move file new_name of the folder this_folder to trash -- moves the original file to the trash for later emptying
--comment out line above and uncomment line below to delete the original file (without moving to the trash) requires Jon's commands
--deleteFile file new_name of the folder this_folder with unlocking
end if
set name of thisItem to new_name
end if
end repeat
end tell
on error
end try
end adding folder items to