Here’s a simple script I wrote to simply look for instances of text containing 2 spaces and replace with 1 space.
tell document 1 of application "InDesign"
set Textbox to selection
repeat with anItem in Textbox
search Textbox with find attributes {find text:" "} --IF NONE ARE FOUND, THERE IS NO RESULT (SCRIPT ERRORS OUT)
set repeatCount to count result
repeat repeatCount times
search Textbox with find attributes {find text:" "} with change attributes {change text:" "}
end repeat
end repeat
end tell
It works great, as long as there it finds at least one instance to correct. If the initial search doesn’t find return any results, then the variable “result” is not defined. Can anyone tell me how to address this issue? I’d like to display dialog “No Double Spaces found” rather than the script erroring out.
Thanks - slimjim
I don’t have InDesign, so can’t test, but you need a try block shown below (but not compiled)
repeat with anItem in Textbox
search Textbox with find attributes {find text:" "} --IF NONE ARE FOUND, THERE IS NO RESULT (SCRIPT ERRORS OUT)
try
set repeatCount to count result
on error
exit repeat
end try
repeat repeatCount times
search Textbox with find attributes {find text:" "} with change attributes {change text:" "}
end repeat
end repeat
tell document 1 of application "InDesign"
set Textbox to selection
repeat with anItem in Textbox
try
search Textbox with find attributes {find text:" "} --IF NONE ARE FOUND, THERE IS NO RESULT (SCRIPT ERRORS OUT)
on error
display dialog "No Double Spaces found"
exit repeat
end try
set repeatCount to count result
repeat repeatCount times
search Textbox with find attributes {find text:" "} with change attributes {change text:" "}
end repeat
end repeat
end tell
Adam - Thank you! I knew it had to be something easy that I was overlooking.
James - when I tried yours I had to move “set repeatCount to count result” into the try block. Otherwise, the result variable was still undefined. After getting that to work, I determined that I’d rather have no error message displayed after all. (It just slowed down my work flow) But at least now I don’t get an error from my QuicKeys…
I do not understand why you want to write a script for this, unless it is for excercising.
Often I get documents from people with double spaces in it, so I simply find and replace them in Indesign.
IKON -
That’s what it does. Finds double spaces and replaces them with a single space. I got tired of opening Find/Replace, typing in 2 spaces, typing in 1 space, hitting Replace All, hitting OK, Hitting Replace All again (in case there were any triple spaces) making sure that no results were found, blah, blah, blah.
So I wrote this script and assigned it to a QuicKey.
I simply select the textbox (or textboxes) that I think have double spaces, hit the QuicKey and the problem is solved in a split second.
If necessity is the mother of invention, then laziness is that mother’s inbred cousin. I’m lazy, therefore I write Applescripts.
-slimjim
Whoops, good call LOL. I couldn’t test the script as the machine I was on didn’t have InDesign, but glad you got it working… And doh, Adam beat me to the punch… again
If I remember correctly you can do an entire story, including overset and linked text boxes, if you use the parent story for your object reference. That might help, or it could be a hinderance.
Jerome -
That would make sense, but wouldn’t be much use to me. You got me thinking though and I replaced “set Textbox to selection” with “set Textbox to every text frame” which works much better for my application.
slimjim