Simple find/replace scripting, continued

Discuss AppleScripting for QuarkXPress 10, 9 & 8 (and before)
Hansa Junchun
Posts: 0
Joined: 08 Nov 2012, 09:48

Simple find/replace scripting, continued

Post by Hansa Junchun » 26 Nov 2012, 03:13

Greetings folks, the main sequence of my work was completed right on time, thanks to your input. Now on to dreary regular find-replace tasks.
I am having difficulty defining simple Quark keystrokes in a script. Here is what I THOUGHT would work but has not.

--finding two spaces and replacing with one space

tell document 1 of application "QuarkXPress"
set {every text of every story where it is " "} to " "
end tell
--finding a space and closed parenthesis and replacing with just closed parenthesis
tell document 1 of application "QuarkXPress"
set (every text of every story where it is " )") to ")"
end tell
--finding two returns and replacing with one return
tell document 1 of application "QuarkXPress"
set (every text of every story where it is {"return", " return"}) to {"return"}
end tell
--finding a bullet and space and replacing with a bullet and tab
tell document 1 of application "QuarkXPress"
set (every text of every story where it is " ") to "" & {"tab"}
end tell

Obviously there is some confusion with parentheses but some searching on this and other forums has yielded no other scripts defining return, closed parenthesis, or tabfor use in a script. Or, as usual, I haven't spotted them!
Your guidance is much appreciated.

Emma
Posts: 657
Joined: 07 Jul 2004, 08:43

Simple find/replace scripting, continued

Post by Emma » 26 Nov 2012, 03:23

For double returns try:
set (every text of every story where it is return & return) to return(no quotes - you're not looking for the word "return" !!
Similarly for tab set (every text of every story where it is " ") to ("" & tab)
I'm shaky on the brackets too, as I usually wrap this up in a sub-routine. But def get rid of quotes where you don't want them!

Hansa Junchun
Posts: 0
Joined: 08 Nov 2012, 09:48

Simple find/replace scripting, continued

Post by Hansa Junchun » 28 Nov 2012, 04:16

That's useful, thanks! Here's the most basic version.
tell application "QuarkXPress"
set t to count documents
repeat with i from 1 to t
try
tell document 1 of application "QuarkXPress"
set (every text of every story where it is return & return) to return
set {every text of every story where it is " "} to " "
set (every text of every story where it is " )") to ")"
end tell
end try
save document 1
close document 1
end repeat
end tell
The MAIN problem with this, is that when it replaces the returns, it imposes the style sheet of the first paragraph onto the next one. So, H2s become text, text becomes H3, and so on.
Do you believe there is a way to inform the script to leave style sheets alone?

Scripting_Ace
Posts: 684
Joined: 29 Jun 2004, 13:14

Simple find/replace scripting, continued

Post by Scripting_Ace » 28 Nov 2012, 04:39

... Or use a try... end try block.

Hansa Junchun
Posts: 0
Joined: 08 Nov 2012, 09:48

Simple find/replace scripting, continued

Post by Hansa Junchun » 28 Nov 2012, 04:40

Yes, Thanks. That did work. My next situation is to ask the script to leave style sheets alone. I posted the fixed script above.

Scripting_Ace
Posts: 684
Joined: 29 Jun 2004, 13:14

Simple find/replace scripting, continued

Post by Scripting_Ace » 28 Nov 2012, 05:23

You did not implement the try block correctly. You need to "try" each instance seperately or else you might endup skipping some replacements. As soon as the call errors out the script steps out of the try block potentially missing the other calls to make.

What do you mean by "leaving the style alone"?
HTH

Hansa Junchun
Posts: 0
Joined: 08 Nov 2012, 09:48

Simple find/replace scripting, continued

Post by Hansa Junchun » 28 Nov 2012, 05:49

This is the line in question:

set (every text of every story where it is return & return) to return

When it replaces the returns, it imposes the style sheet of the first paragraph onto the next one. So, H2s become text, text becomes H3, and so on down the document.
The way I imagine it, the scriptt must be removing both returns first and then adding a brand new return, which causes the paragraph to pick up the style sheet of the one above it.

Scripting_Ace
Posts: 684
Joined: 29 Jun 2004, 13:14

Simple find/replace scripting, continued

Post by Scripting_Ace » 28 Nov 2012, 06:07

You might want to:
repeat with s from 1 to count of stories
set x to get offset of every text of sotry s where it is (return & return)
repeat with c from (count of x) to 1 by -1
tell story s
delete character (c+1)
end tell
end repeat
end repeat
This is untested code and will probably not work as is but it gives you something to work with.

HTH

Hansa Junchun
Posts: 0
Joined: 08 Nov 2012, 09:48

Simple find/replace scripting, continued

Post by Hansa Junchun » 29 Nov 2012, 04:33

Good morning,
The above doesn't work as is, and I confess too little experience to know how to improve it. I'll keep hunting for solutions on the web as time permits and hopefully post my findings here at some point. In the meantime my other script works pretty well, though you're right that the try block is too big and sometimes skips fixes. I will have to update it with individual tries.

Scripting_Ace
Posts: 684
Joined: 29 Jun 2004, 13:14

Simple find/replace scripting, continued

Post by Scripting_Ace » 29 Nov 2012, 05:29

OK than,
try this:

001tell application "QuarkXPress 8"
002set DocName to name of document 1
003tell document DocName
004--finding SPACE+SPACE and replacing with SPACE
005set doLoop to true
006repeat while doLoop
007set doLoop to my QuarkFindChange(DocName, " ", " ")
008end repeat

009--finding a space and closed parenthesis and replacing with just closed parenthesis
010tell me to QuarkFindChange(DocName, " )", ")")

011--finding a bullet and space and replacing with a bullet and tab
012tell me to QuarkFindChange(DocName, " ", ("" & tab))

013--finding SPACE+RETURN and replacing with RETURN
014tell me to QuarkFindChange(DocName, (" " & return), return)

015--finding RETURN+SPACE and replacing with RETURN
016tell me to QuarkFindChange(DocName, (return & " "), return)

017--finding RETURN+RETURN and replacing with RETURN
018repeat with s from 1 to count of stories
019tell story s
020try
021set doLoop to true
022set doubleReturnOffsets to (offset of every text where it is (return & return))
023on error
024set doLoop to false
025end try
026
027repeat while doLoop
028repeat with x from (count of doubleReturnOffsets) to 1 by -1
029set thisOffset to (item x of doubleReturnOffsets) as integer
030delete character (thisOffset + 1)
031end repeat
032
033try
034set doubleReturnOffsets to (offset of every text where it is (return & return))
035on error
036set doLoop to false
037end try
038end repeat
039end tell
040end repeat
041end tell

042end tell


043on QuarkFindChange(DocName, FindWhat, ReplaceWith)
044tell application "QuarkXPress 8"
045tell document DocName
046try
047set doLoop to true
048set (every text of every story where it is FindWhat) to ReplaceWith
049on error
050set doLoop to false
051end try
052end tell
053end tell
054return doLoop
055end QuarkFindChange
Notes: The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip all of them.

Post Reply

Return to “QuarkXPress 8, 9 & 10: AppleScript”