Renaming files problem

Please help. Here is what I want a script to do.

Prompt to choose a folder of files to rename

Prompt to input a string of characters to be renamed

Prompt to input the replacement string of characters

end.

I thought there use to be a script like this already in the forum, but I can’t seem to find it.

Thanks.

If you have a look in the Scripts folder of the Library Folder on your Mac you’ll find a number of very useful scripts that can help you learn. These are also accessable though the script menu feature. The following is adapted slightly from one of the Finder scripts found there.

set the source_folder to choose folder with prompt "Folder containing items to edit:"


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 "" 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

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

Thank you mattc,

It works beautifully.

Hi Guys,

This script is great but I have been trying to make it do just a little more but with no success so it’s back to the experts.

Basically I have found that it works for the selected folder and it contents but doesn’t go any deeper, I need it to go into all of the sub-folders and their files as well.

Any help would be great.

All the best,

Duke.

Model: iBook
AppleScript: Version 2.1 (80)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Hi, Duke.

Fortunately, there’s no need for recursion here as the Finder’s bulk references can sort out the items that need renaming. The following is just a rehash of the above script. It works well enough and eliminates the original’s dubious practice of coercing ‘display dialog’ records to list.

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 "" 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_item_name to text_items as Unicode text
			set AppleScript's text item delimiters to astid
			-- ... and rename the associated item.
			my set_item_name(item i of item_list, new_item_name)
		end repeat
	end if
end tell

beep 2

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container to (the container of this_item)
		if not (exists item new_item_name of the parent_container) 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
				set new_item_name to my get_new_name(error_message, new_item_name)
				if (new_item_name is 0) then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			set new_item_name to my get_new_name("This name is already taken, please rename.", new_item_name)
			if (new_item_name is 0) then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

on get_new_name(msg, default_answer)
	tell application (path to frontmost application as Unicode text)
		set {text returned:new_item_name, 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_item_name
		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_new_name