Hello,
I am running OS/X . 2.6 with Script Editor 1.9. And I seem to be unable to write just one script to complete a multiple file renaming task. So, out of frustration, and a desire to get the job done myself, I managed to write (or I should say "rewrite", since the original Applescript for this purpose was Apple's) one "rename file" for each client, and then, to batch process them, I had to write two other Applescripts to trigger them (since my client list exceeds 20 and Script Editor informed me I had too many "tell statements") and then one Master-controller script to trigger the second two scripts (all scripts were saved as Applications, of course).
Now, the first set of over twenty scripts are all designed to rename individual files with different names depending upon what prefix is found in the original file name. For example, the Applscript below searched for "zu " and renames that particular file "Zurkha Account ", and so on:
–set the source_folder to choose folder with prompt “Folder containing items to edit:”
– get the path to the folder of the front window
– if no windows are open, the desktop folder will be used
try
tell application “Finder” to set the source_folder to (folder of the front window) as alias
on error – no open folder windows
set the source_folder to path to desktop folder as alias
end try
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 "zu " 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 "Zurkha Account " & (month of (current date)) & �
" " & (day of (current date)) & " " & �
(year of (current date)) 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
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info
set change_flag to false
if the current_name contains the search_string then
if the search_parameter is “Folder Names” and �
folder of this_info is true then
set the change_flag to true
else if the search_parameter is “File Names” and �
folder of this_info is false then
set the change_flag to true
else if the search_parameter is “Both” then
set the change_flag to true
end if
if the change_flag is true then
– replace target string using delimiters
set AppleScript’s text item delimiters to the search_string
set the text_item_list to every text item of the current_name
set AppleScript’s text item delimiters to the replacement_string
set the new_item_name to the text_item_list as string
set AppleScript’s text item delimiters to “”
my set_item_name(this_item, new_item_name)
end if
end if
end repeat
beep 2
on set_item_name(this_item, new_item_name)
tell application “Finder”
–activate
set the parent_container_path to (the container of this_item) as text
if not (exists item (the parent_container_path & new_item_name)) then
try
set the name of this_item to new_item_name
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
tell me to display dialog the error_message default answer new_item_name buttons {“Cancel”, “Skip”, “OK”} default button 3
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is “Skip” then return 0
my set_item_name(this_item, new_item_name)
end try
else --the name already exists
–beep
tell me to display dialog “This name is already taken, please rename.” default answer new_item_name buttons {“Cancel”, “Skip”, “OK”} default button 3
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is “Skip” then return 0
my set_item_name(this_item, new_item_name)
end if
end tell
end set_item_name
Here is the Script for the two second set of scripts:
(NOTE: In order for Finder to properly find the scripts entitled APRENAMESELECTED FOLDERETCTUITION&CURRENTDATE 3B it was necessary to shorten their file names in the 1 RenamingApplescripts4ClientFiles Folder on Drive C)
tell application “Finder”
open file (alias “Drive C:Saves&Downloads :Documents to:ClientAccountsJanuary2004to:1 RenamingApplescripts4ClientFiles:KNEFF Rename”)
tell application “Finder”
open file (alias “Drive C:Saves&Downloads:Documents :ClientAccountsJanuary2004to:1 RenamingApplescripts4ClientFiles:RINA Rename”)
tell application “Finder”
open file (alias “Drive C:Saves&Downloads 12-9-03 to:Documents 12-09=03 to:ClientAccountsJanuary2004to:1 RenamingApplescripts4ClientFiles:TEQUE Rename”)
tell application “Finder”
open file (alias “Drive C:Saves&Downloads :Documents :ClientAccounts2004to:1 RenamingApplescripts4ClientFiles:FIE Rename”)
end tell
end tell
end tell
end tell
Here is the Script for the third, Master-controller script (it just opens two Application files) :
tell application “Finder”
open file (alias “Drive C:Saves&Downloads :V A T S Applescripts Dec 09 2003 to:Application Scripts:PianoTeachingScriptsAPPLICATIONS:ApROUGH OpenAllRenameScriptsAtOnceNestedInStudentTuitionFolder 3”)
tell application “Finder”
open file (alias “Drive C:Saves&Downloads :V A T S Applescripts Dec 09 2003 to:Application Scripts:PianoTeachingScriptsAPPLICATIONS:ApROUGH OpenAllRenameScriptsAtOnceNestedInStudentTuitionFolder 3b”)
end tell
end tell
Being relativly new to Applescript & Mac in general, I can imagine that the experienced scripter will chuckle at the above. But, as I am sure you realize, we all must start somewhere. That being said, what I'd like to streamline in the above process is the following:
1.) Create just ONE script which will do all of the above.
2.) Cut down the number of dialog boxes which pop up for each script (in set one) from four (4) to two (2), if possible.
Any and all help and advice will be greatly appreciated.
Thanks in advance,
Variable as the shade