How to find "Usa" and replace it with "USA"

can somebody tell me how to make this work? I can’t figure out how to use the considering case command, and I don’t even know if it applies here.
Thanks in advance :slight_smile:


try
	tell application "Finder" to set the cupsPrint_folder to (folder of the front window) as alias
	set olddelims to AppleScript's text item delimiters -- save their current state
end try
--Change Dh
set the cupsPrint_list to list folder cupsPrint_folder without invisibles
set cupsPrint_folder to cupsPrint_folder as string
repeat with i from 1 to number of items in the cupsPrint_list
	set thisCups_item to item i of the cupsPrint_list
	set thisCups_item to (cupsPrint_folder & thisCups_item) as alias
	set cupsItem_info to info for thisCups_item
	set the current_name to the name of cupsItem_info
	set change_flag to true
	if the change_flag is true then
		set the search_string to "Usa"
		set replacement_string to my change_case_of(search_string, "upper")
		-- replace target string using delimiters
		set AppleScript's text item delimiters to the search_string
		set the text_cupsPrint_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_cupsPrint_list as string
		set AppleScript's text item delimiters to ""
		my rm_Underscore(thisCups_item, new_item_name)
	end if
end repeat
on rm_Underscore(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
			end try
		end if
	end tell
end rm_Underscore
--End Change Dh

--Properties For Upper Case Handler
on change_case_of(this_text, this_case)
	set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
	set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	set the new_text to ""
	repeat with thisChar in this_text
		set x to the offset of thisChar in the comparison_string
		if x is not 0 then
			set the new_text to (the new_text & character x of the source_string) as string
		else
			set the new_text to (the new_text & thisChar) as string
		end if
	end repeat
	return the new_text
end change_case_of
--End Properties For Upper Case Handler

What do you want returned from

“Usability is a major consumer concern of the Usa’s population”

“Usability is a major consumer concern of the USA’s population” would be ideal, though not strictly necessary, since I could change the search term from “Usa” to "Usa " (with a space after), which would work well enough for my purposes.

The script I have actually changes the value of search_string from “Usa” to “USA”. I just don’t know how to make it apply the changes to the file names. Isn’t there something about AppleScript not making the distinction between uppercase and lowercase characers?

Sorry, I wasn’t terribly specific in my original post. Thanks for taking the time.

You might look at the considering and ignoring statements section of http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html

Perhaps something like this will work for you

set testString to "Usability is a major consumer concern of the Usa's population."

set tid to AppleScript's text item delimiters

set seperationMarks to {" ", "_", "-"}
repeat with oneSeparator in seperationMarks
	set findString to "usa"
	set replacestring to "USA"
	set findString to oneSeparator & findString & oneSeparator
	set replacestring to oneSeparator & replacestring & oneSeparator
	
	set AppleScript's text item delimiters to {findString}
	set testParts to text items of oneSeparator & testString & oneSeparator
	set AppleScript's text item delimiters to {replacestring}
	
	set returnString to testParts as string
	set testString to text ((length of replacestring) + 2) thru -((length of replacestring) + 2) of (testParts as string)
end repeat

thanks Mike, I’ll give it a whirl.

yea, I looked at the considering and ignoring stuff, but I couldn’t make it go :frowning:
If I had a better foundation in the language I might have a shot. Maybe one day I’ll figure it out.

Cheers :smiley:

didn’t work. Drat :confused:

You could use considering case, but I don’t think that it is necessary in this situation. The biggest problem would be to eleminate the potential for replacing words that have “usa” in them with “USA” as in Usability in the example case. For this sentence it is enough to search for usa’ but you might also have “usa.” or " usa ". So if we take that into consideration something like this should work:

set testString to "Usability is a major consumer concern of the Usa's population.  usa  usa. usable. tesusa."

set tid to AppleScript's text item delimiters

set searchStrings to {"usa'", " usa ", " usa."}
set replaceStrings to {"USA", " USA ", " USA."}

repeat with i from 1 to count of searchStrings
	set AppleScript's text item delimiters to item i of searchStrings
	set holdingList to every text item of testString
	set AppleScript's text item delimiters to item i of replaceStrings
	set testString to holdingList as string
end repeat


set AppleScript's text item delimiters to tid
return testString

that works great, but how do I return that result to the name of a file? That seems to be the sticking point for everything I try? I can get Applescript to do it, but I can’t get Finder to recognize the distinction and make the change.

You could probably find a more efficient way to do it but something like this should work:
set tid to AppleScript’s text item delimiters

set searchStrings to {"usa'", " usa ", " usa."}
set replaceStrings to {"USA", " USA ", " USA."}

set checkFolder to choose folder

tell application "Finder"
	set fileList to every file in checkFolder
	repeat with aFile in fileList
		set fileName to name of aFile
		
		repeat with i from 1 to count of searchStrings
			set AppleScript's text item delimiters to item i of searchStrings
			set newName to every text item of fileName
			set AppleScript's text item delimiters to item i of replaceStrings
			set fileName to newName as string
		end repeat
		set name of aFile to fileName
	end repeat
	
end tell

actually, this doesn’t work for the Usa to USA thing, but it works unbelievably well for the other post I’ve made today i.e. multiple search and multiple replace. Thanks a lot :smiley:

I think I have it. It’s convoluted (to say the least), but it works.

Basically, I ran the find and replace routine twice. On the first pass, I replaced “Usa” with the term “test.” On the second pass, I replaced the the term “test” with “USA.”

I had tried this before, and couldn’t get it to work. I think pulling the replacement terms from the clipboard is what made it go.

Here’s the code if anybody wants it…

try
	tell application "Finder" to set the cupsPrint_folder to (folder of the front window) as alias
	set olddelims to AppleScript's text item delimiters -- save their current state
end try

--Replacing Lowercase With UpperCase
set the search_string to "Usa "
tell application "Finder"
	set the clipboard to "test"
end tell
set replacement_string to (the clipboard) as text
set the cupsPrint_list to list folder cupsPrint_folder without invisibles
set cupsPrint_folder to cupsPrint_folder as string
repeat with i from 1 to number of items in the cupsPrint_list
	set thisCups_item to item i of the cupsPrint_list
	set thisCups_item to (cupsPrint_folder & thisCups_item) as alias
	set cupsItem_info to info for thisCups_item
	set the current_Filename to the name of cupsItem_info
	set change_flag to true
	-- replace target string using delimiters
	set AppleScript's text item delimiters to the search_string
	set the text_cupsPrint_list to every text item of the current_Filename
	set AppleScript's text item delimiters to the replacement_string
	set the new_item_name to the text_cupsPrint_list as string
	set AppleScript's text item delimiters to ""
	my upper_USA(thisCups_item, new_item_name)
end repeat
set newUpper to my change_case_of(search_string, "upper")
set the search_string to "test"
tell application "Finder"
	set the clipboard to newUpper
end tell
set replacement_string to (the clipboard) as text
set the cupsPrint_list to list folder cupsPrint_folder without invisibles
set cupsPrint_folder to cupsPrint_folder as string
repeat with i from 1 to number of items in the cupsPrint_list
	set thisCups_item to item i of the cupsPrint_list
	set thisCups_item to (cupsPrint_folder & thisCups_item) as alias
	set cupsItem_info to info for thisCups_item
	set the current_Filename to the name of cupsItem_info
	set change_flag to true
	-- replace target string using delimiters
	set AppleScript's text item delimiters to the search_string
	set the text_cupsPrint_list to every text item of the current_Filename
	set AppleScript's text item delimiters to the replacement_string
	set the new_item_name to the text_cupsPrint_list as string
	set AppleScript's text item delimiters to ""
	my upper_USA(thisCups_item, new_item_name)
end repeat
on upper_USA(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
			end try
		end if
	end tell
end upper_USA
--End Replacing Lowercase With UpperCase