Droplet to Rename Folders and Files
I found a script to do this with some manual intervention but wanted to take this one step further by creating a droplet and making it drag and drop. This droplet works, but the problem comes in that it still wants you to choose the target folder from a dialog and I would like to just auto process the dropped folder and it's contents. Also, is there a way to tell AS to use characters 3-6 of the dropped folder's name to replace any instances of "0000" in sub-folder or document names enclosed? Is there any uneccessary code which can be stripped out?
Here is the script as it stands now:
on open of finderObjects
set the source_folder to (choose folder with prompt "Folder containing items to edit:") as Unicode text
display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result
repeat
display dialog "Enter text to find in the item names:" default answer "0000" buttons {"Cancel", "OK"} default button 2
set the search_string to the text returned of the result
if the search_string is not "" then exit repeat
end repeat
repeat
display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
set the replacement_string to the text returned of the result
if the replacement_string contains ":" then
beep
display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
else if the replacement_string contains "/" then
beep
display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
else
exit repeat
end if
end repeat
display dialog "Replace “" & the search_string & "” with “" & the replacement_string & "” in every item name?" buttons {"Cancel", "OK"} default button 2
tell application "Finder"
-- Get a Finder reference to the relvant items.
if (search_parameter is "Folder Names") then
set item_reference to a reference to (folders of entire contents of folder source_folder whose name contains search_string)
else if (search_parameter is "File Names") then
set item_reference to a reference to (files of entire contents of folder source_folder whose name contains search_string)
else
set item_reference to a reference to (items of entire contents of folder source_folder whose name contains search_string)
end if
-- Get a list of aliases to the items.
-- (Individual Finder references might fail when renaming items within renamed folders.)
try
set item_list to item_reference as alias list
on error
set item_list to item_reference as alias as list
end try
if item_list is not {} then
-- If there are any relevant items, get their names.
set current_names to name of item_reference
-- Doctor each name...
repeat with i from 1 to (count current_names)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to search_string
set text_items to text items of (item i of current_names)
set AppleScript's text item delimiters to replacement_string
set new itemname to text_items as Unicode text
set AppleScript's text item delimiters to astid
-- ... and rename the associated item.
my set itemname(item i of item_list, new itemname)
end repeat
end if
end tell
end open
beep 2
on set item_name(thisitem, new itemname)
tell application "Finder"
--activate
set the parent_container to (the container of this_item)
if not (exists item new itemname of the parent_container) then
try
set the name of this_item to new itemname
on error the error_message number the error_number
if the error_number is -59 then
set the error_message to "This name contains improper characters, such as a colon (:)."
else --the suggested name is too long
--set the error_message to error_message -- "The name is more than 31 characters long."
end if
--beep
set new itemname to my get new_name(errormessage, new itemname)
if (new itemname is 0) then return 0
my set item_name(thisitem, new itemname)
end try
else --the name already exists
--beep
set new itemname to my get newname("This name is already taken, please rename.", new itemname)
if (new itemname is 0) then return 0
my set item_name(thisitem, new itemname)
end if
end tell
end set itemname
on get newname(msg, default_answer)
tell application (path to frontmost application as Unicode text)
set {text returned:new itemname, button returned:button_pressed} to (display dialog msg default answer default_answer buttons {"Cancel", "Skip", "OK"} default button 3)
if (button_pressed is "OK") then
return new itemname
else if (button_pressed is "Skip") then
return 0
else
error number -128 -- only necessary on non-English systems.
end if
end tell
end get newname
Various Mixed Macs Mac OS X (10.4.3)
Various Mixed Macs Mac OS X (10.4.3)