Simple find/replace scripting, continued

Discuss AppleScripting for QuarkXPress 10, 9 & 8 (and before)
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.

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

Simple find/replace scripting, continued

Post by Hansa Junchun » 04 Dec 2012, 05:07

Oh, that's greatt! I ran it and the latter portion caused my test to get caught in an endless loop. I therefore put the following to use and it worked brilliantly
tell application "QuarkXPress"
set DocName to name of document 1
tell document DocName
repeat with s from 1 to count of stories
tell story s
try
set doLoop to true
set doubleReturnOffsets to (offset of every text where it is (return & return))
on error
set doLoop to false
end try

repeat while doLoop
repeat with x from (count of doubleReturnOffsets) to 1 by -1
set thisOffset to (item x of doubleReturnOffsets) as integer
delete character (thisOffset + 1)
end repeat
try
set doubleReturnOffsets to (offset of every text where it is (return & return))
on error
set doLoop to false
end try
end repeat
end tell
end repeat
end tell

end tell


Thanks again!

Post Reply

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