I received this script from a coworker of mine. I am not sure what he was doing with the <> and <> tags. And the script editor does not compile, errors on the 2nd line. The intent is to replace all faux “All Caps” with a change case of using standard capitalization.
Can anybody help me make sense of this?
tell application "Adobe InDesign CS4"
set «class pChg» to nothing
set «class pFnd» to nothing
tell active document
set searchResults to «event K2 Find» every story given «class wFnd»:{capitalization:all caps}
repeat with x from 1 to count of searchResults
repeat with y from 1 to count of item x of searchResults
set selection to item y of item x of searchResults
set capitalization of selection to normal
changecase of selection using uppercase
end repeat
end repeat
select nothing
end tell
end tell
It means the script was written for InDesign CS2, and the way find/change is done has changed rather dramatically since then. Have a look at the sample script FindchangebyList.applescript to see the new syntax.
Thank you Shane,
Believe it or not, I was aware of some changes made with the Find/Change. I wasn’t sure if his code had some interesting workarounds. glad to see it wasn’t so. And thank you for directing me to FindchangebyList.applescript.
Something must be wrong, because I think my script is working? Scary to think I may be getting this? However, I am not sure if the syntax of lines 2-5 are perfect.
tell application "Adobe InDesign CS4"
set allCaps to {capitalization:all caps}
set properties of find grep preferences to allCaps
tell active document
set searchResults to find grep allCaps
repeat with x from 1 to count of searchResults
repeat with y from 1 to count of item x of searchResults
set selection to item y of item x of searchResults
set capitalization of selection to normal
changecase of selection using uppercase
end repeat
end repeat
select nothing
end tell
set find grep preferences to nothing
set change grep preferences to nothing
set find change grep options to nothing
end tell
There’s no need for the double repeats, or for selecting anything. And “set find change grep options to nothing” does nothing – you need to set the properties of the options to suit. Try this:
tell application "Adobe InDesign CS3"
set find grep preferences to nothing
set change grep preferences to nothing
set allCaps to {capitalization:all caps}
set properties of find grep preferences to allCaps
tell active document
set searchResults to find grep allCaps
repeat with i from 1 to count of searchResults
set capitalization of item i of searchResults to normal
changecase item i of searchResults using uppercase
end repeat
end tell
end tell
One thing to watch out for is that if you’re doing a search and the changes you make affect the number of characters, you should loop through from last to first.
Doh! I should have realized that I didn’t need to select the character first. This is much better and faster by removing that aspect. THANK YOU!
You last statement is interesting. I assume you are saying that if during the find/change the number characters is increased that meets the search criteria, then that equal amount of characters added to the initial count will be missed since it doesn’t know to repeat through that x number of items?
btw, the only reason I set the grep preferences to nothing at the end was to clear the field in the find/change dialog box. Because it seems to hang onto what is applied in the script.
The references returned from the search are of the form character x to character y. That means if a change alters the length of the story, all the following references will be out.
Right, but to be safe you should do it before hand, otherwise you’ll inherit any existing settings. And “set find change grep options to nothing” does, well, nothing.
The “set find change grep options to nothing” statement actually toggles a handful of (potentially legacy) parameters. It’s a good idea to set the preferences and option values at both the start and end of your script, similarly to working with TIDs.
None of the previous scripts utilize the Change portion of the Find/Change; by doing so, you can eliminate looping. My take:
tell application "Adobe InDesign CS3"
set {find grep preferences, change grep preferences, find change grep options} to {nothing, nothing, nothing}
set {find grep preferences's properties, change grep preferences's properties} to {{capitalization:all caps}, {capitalization:normal}}
tell document 1 to change grep (changecase (find grep) using uppercase)
set {find grep preferences, change grep preferences, find change grep options} to {nothing, nothing, nothing}
end tell
Excellent Marc, even faster. And I always forget how you can combine preferences into one line the way you do. I look back at some older scripts I wrote and laugh and cry
Keep in mind that writing “set {find grep preferences, change grep preferences, find change grep options} to {nothing, nothing, nothing}” still sends three events, as you can see in the event log. But Marc’s single changecase is a big timesaver.