Counting occurences of action in applescript

I’ve worked (and received help) to get an applescript to perform a series of Find and Replace tasks in InDesign but would love a dialog that tells me how many of each task it’s performed. Like the regular F&R does. How would i write code to have my script do that?

I have it generating a dialog saying what is being done when it starts and a dialog that says that it is “Done”.

Also, how could I have it play a beep or sound when it’s done too?

do you mean that you want a dialog that tells you how many occurences were replaced for a a certainsearch? I’m not sure I quite understnad what you want to do. If your answer to my question is yes, then the

search for <string> replacing with <string> 

command returns a list of the texts that were replaced. By using count command with this list, you can find out the number of replacements.

This is the Find and Replace we created to make certain text items all superscript. If I did the F&R manually in InDesign it presents a dialog saying “# occurrences have been changed” after it’s performed the “replace all”. If you don’t have InDesign the Word F&R behaves very similar to give you an idea. I’d like for this script to provide the same kind of dialog for ALL the changes in one dialog list. Something like:
"# of ™ occurrences have been made SuperScript

of ® occurrences have been made SuperScript

of © occurrences have been made SuperScript"

of † occurrences have been made SuperScript"

I’m really new at this Applescript stuff so feel free to treat me like a dummy. How and where would I place your suggested commands to get that kind of result?


tell application "InDesign CS"
	set find preferences to nothing
	set change preferences to nothing
	set mydoc to document 1
	tell mydoc
		try
			set myssstyle to character style "Superscript"
		on error
			set myssstyle to (make character style with properties {name:"Superscript", position:superscript})
		end try
		display dialog "Script is now changing all ™, ®, © and † marks to Superscript. Please Wait." giving up after 2 with icon note
		set myfoundcharacter to search for "™" with change attributes {applied character style:myssstyle}
		set myfoundcharacter to search for "®" with change attributes {applied character style:myssstyle}
		set myfoundcharacter to search for "©with change attributes {applied character style:myssstyle}
		set myfoundcharacter to search for "†" with change attributes {applied character style:myssstyle}
		
	end tell
end tell
tell application "InDesign CS"
	display dialog "Done" giving up after 1 with icon note
end tell

ok, that’s very doable. Just keep track of the number of each type of character during the script.


set myfoundcharacter to search for "™" with change attributes {applied character style:myssstyle}
set countTMChar to count (myfoundcharacter)
set myfoundcharacter to search for "®" with change attributes {applied character style:myssstyle}
set countRChar to count (myfoundcharacter)
--etc.

at the end you can then have a dialog like


display dialog countTMChar & "of ™ occurences have been made SuperScript" & return & countRChar & "of ® occurrences have been made SuperScript" & return --etc.

OK I input the first part as you said and it seems to work great. But when I put the dialog code in I get an error.


	display dialog countTMchar & "of ™ occurences have been made SuperScript" & return & countRchar & "of ® occurrences have been made SuperScript" & return & countCchar & "of © occurrences have been made SuperScript" & return & countTchar & "of † occurrences have been made SuperScript" & return & "DONE!"

Returns error: "InDesign CS got an error: Can’t make {6, “of ™ occurences have been made SuperScript”, "
", 7, “of ® occurrences have been made SuperScript”, "
", 2, “of © occurrences have been made SuperScript”, "
", 6, “of † occurrences have been made SuperScript”, "
", “DONE!”} into a string.

So the count is working but the dialog just won’t display properly. Any Ideas? :smiley:

The problem may be the coersion of the integer countTMchar to a string. What happens if you add the line:

set countTMchar to countTMchar as string

I have had this problem before, and I am not sure why AS soemtimes does the conversion as expected, and sometimes it does not.

Andy

yeah, I’ve had that error too. what Andy said will probably work. I’ve also just stuck an empty string at the beginning of the dialog:


display dialog "" & countTMChar & "of ™ occurences have been made SuperScript" & return & countRChar & "of ® occurrences have been made SuperScript" & return --etc. 

Thanks that worked perfect. :smiley: