Page 1 of 1

AppleScript shown in QuarkXPress Masterclass

Posted: 09 Jul 2014, 02:32
by Matthias Guenther (Quark)
Hi all,

here's the AppleScript shown in Session 3 of the QuarkXPress Masterclass. It does a multiple find/change on (one or) multiple documents.

All you need is a folder of QuarkXPress files that have the ending ".qxp" (all these will be processed) and a tab-delimited (not comma separated) text file that lists the find/change pairs. You can use a text editor, Excel (export as tab-separated text) or even QuarkXPress (import/export text, encoding Mac Roman) to create such a tab separated list.

Please open Apple's AppleScript-Editor and paste all of the code in there:

Code: Select all

-- Script to apply several Find/Change pairs in one go at all documents in a given folder
-- Experimental script, USE AT YOUR OWN RISK!
-- Make sure that you have a backup beforehand
-- Based on a script idea by user ScriptingAce in forum
-- Modified by Matthias Guenther

-- Reminder dialog that all documents should be closed, could probably be improved to save and close all documents automatically
display dialog "Please first close all documents in QuarkXPress to run this script!" & return & "And make sure that you have a recent backup of your files!" buttons {"All documents are closed and I have a backup!", "Cancel"} default button 2 with icon stop

-- Locate the folder with all QuarkXPress documents
-- Script will work on all files having a file extension of .qxp
set QFolder to "Please locate the folder containing your QuarkXPress files:" & return & "(All files with the file extension of .qxp will be processed)" & return & return & "WARNING: Changes will be applied and saved without prompting. Have a backup of your files before proceeding!"
set Doc_Folder to (choose folder with prompt QFolder) as text
set theFiles to {}
tell application "Finder"
	tell folder Doc_Folder
		repeat with i from 1 to count of files
			if name extension of file i is "qxp" then
				set end of theFiles to file i as text
			end if
		end repeat
	end tell
end tell

-- Load the find/change pairs out of a Tab Separated file
-- Make sure all pairs are defined as followed: SEARCHSTRING TAB REPLACESTRING RETURN
set QFile to "Please locate your text file that contains the Tab Delimited Find/Change Pairs:"
set Search_File to (choose file with prompt QFile) as text
set Search_Rows to read file Search_File using delimiter {return}
set Search_Items to {}
repeat with This_Row in Search_Rows
	set end of Search_Items to my GetTextItem(This_Row as text, tab, 0)
end repeat

-- Have QuarkXPress open all files and apply the find/change pairs
-- WARNING: As QuarkXPress afterwards saves the changes without warning, make sure you have a backup of your original file
tell application "QuarkXPress"
	repeat with oneFile in theFiles
		open file oneFile
		tell document 1
			repeat with ThisPair in Search_Items
				set {searchstring, replaceString} to ThisPair as list
				tell every story
						set (every text where it is searchstring) to replaceString
					end try
				end tell
			end repeat
		end tell
		close document 1 saving yes
	end repeat
end tell

-- Sub Routines
on GetTextItem(ThisString, ThisDelim, ThisItem)
	-- ThisString -> String to look in
	-- ThisDelim -> Text element that delimit the string
	-- ThisItem -> Number of the element to return (0 for all)
	copy the text item delimiters to OldDelims
	set the text item delimiters to ThisDelim
	if class of ThisItem is list then
		set fromItem to (item 1 of ThisItem) as integer
		set toitem to (item 2 of ThisItem) as integer
		set arrItem to (text items fromItem thru toitem of ThisString)
		set arrItem to every text item of ThisString
	end if
	set the text item delimiters to OldDelims
	if class of ThisItem is list then
		return arrItem as text
		if ThisItem is not equal to 0 then
			return (item ThisItem of arrItem) as text
			return arrItem -- return every items
		end if
	end if
end GetTextItem
I copied large parts of the great script that ScriptingAce posted here (viewtopic.php?f=21&t=18872&p=88291&hilit=pairs#p88293), I just slightly modified it.

As explained, AppleScripts typically overwrite files, so make sure you have a backup of your files before running it against it. Also, though I tested it personally (but just with QuarkXPress 10), it was under no means extensively tested.

Enjoy! And you might also want to make that a runtime only.

And maybe the AppleScript community, Emma, Ace, JMS, and all the others here, will enhance it further.


Re: AppleScript shown in QuarkXPress Masterclass

Posted: 15 Jul 2014, 05:58
by Matthias Guenther (Quark)
By the way, I got some feedback that some special characters do not show up correctly in the tab separated file and therefore are not replaced properly.

For example, " - " (dash) to " -- " (en dash).

My workflow: Use QuarkXPress to generate the special characters, so create a text box, put in the special pairs (separated by tabs and each line ending with a return character). Then export as text file (File > Save Text) and choose "Plain Text", Encoding "Mac Roman".

Works great for me.

Hope that helps

Re: AppleScript shown in QuarkXPress Masterclass

Posted: 20 Nov 2014, 01:53
by franz.wohlkoenig
Hi Matthias,

XPress 10.5 on MacOS10.10 crashes using this Script. Can post Cash-Report if useful.


Re: AppleScript shown in QuarkXPress Masterclass

Posted: 19 Jan 2015, 05:57
by franz.wohlkoenig

new Test: no Crash anymore;-)

A Question about your Tipp: What about 2 empty Paragraphs changing on 1 Paragraph? Tried your Solution and also "\p\p(this is an tab)\p" like QXP-Search and Replace - both no Success.


Re: AppleScript shown in QuarkXPress Masterclass

Posted: 04 Aug 2015, 08:52
by G.E.R.R.Y.
Thanks for all that, Matthias. However, I think I'm a bit hesitant to try such a detailed Applescript especially when I won't have a clue what I'm doing. :oops:

Re: AppleScript shown in QuarkXPress Masterclass

Posted: 04 Aug 2015, 09:04
by Matthias Guenther (Quark)
Hi Gerry,

true, maybe not a good idea for pre-flight checks. ;-)

Have a look here please: