AppleScript shown in QuarkXPress Masterclass

Discuss AppleScripting for QuarkXPress 10, 9 & 8 (and before)

AppleScript shown in QuarkXPress Masterclass

Postby Matthias Guenther (Quark) » 09 Jul 2014, 02:32

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 http://forums.quark.com
-- 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"
   activate
   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
               try
                  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)
   else
      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
   else
      if ThisItem is not equal to 0 then
         return (item ThisItem of arrItem) as text
      else
         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.

Thanks
Matthias
Feel there should be more chatting, more tips, more user interaction?
Join us on Facebook, a forum-like group with over 2,000 QuarkXPress fans interacting:
http://www.facebook.com/groups/quarkxpress



Image
User avatar
Matthias Guenther (Quark)
Quarkian
Quarkian
 
Posts: 5271
Joined: 04 Jun 2004, 15:06
Location: http://bit.ly/QuarkHamburg

Re: AppleScript shown in QuarkXPress Masterclass

Postby Matthias Guenther (Quark) » 15 Jul 2014, 05:58

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
Matthias
Feel there should be more chatting, more tips, more user interaction?
Join us on Facebook, a forum-like group with over 2,000 QuarkXPress fans interacting:
http://www.facebook.com/groups/quarkxpress



Image
User avatar
Matthias Guenther (Quark)
Quarkian
Quarkian
 
Posts: 5271
Joined: 04 Jun 2004, 15:06
Location: http://bit.ly/QuarkHamburg

Re: AppleScript shown in QuarkXPress Masterclass

Postby franz.wohlkoenig » 20 Nov 2014, 01:53

Hi Matthias,

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

Regards
27" iMac • 3,1 GHz Intel Core i5 • 12 GB Memory • System: 10.10.5
XPress since 3.1, actual 2015
User avatar
franz.wohlkoenig
 
Posts: 46
Joined: 15 Jul 2014, 05:43
Location: Carinthia, Austria, Europe

Re: AppleScript shown in QuarkXPress Masterclass

Postby franz.wohlkoenig » 19 Jan 2015, 05:57

Hi,

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.

Regards
27" iMac • 3,1 GHz Intel Core i5 • 12 GB Memory • System: 10.10.5
XPress since 3.1, actual 2015
User avatar
franz.wohlkoenig
 
Posts: 46
Joined: 15 Jul 2014, 05:43
Location: Carinthia, Austria, Europe

Re: AppleScript shown in QuarkXPress Masterclass

Postby G.E.R.R.Y. » 04 Aug 2015, 08:52

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:
G.E.R.R.Y.
 
Posts: 71
Joined: 27 Nov 2013, 17:33

Re: AppleScript shown in QuarkXPress Masterclass

Postby Matthias Guenther (Quark) » 04 Aug 2015, 09:04

Hi Gerry,

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

Have a look here please: https://youtu.be/lEg_luUTosY?t=12m35s

Thanks
Matthias
Feel there should be more chatting, more tips, more user interaction?
Join us on Facebook, a forum-like group with over 2,000 QuarkXPress fans interacting:
http://www.facebook.com/groups/quarkxpress



Image
User avatar
Matthias Guenther (Quark)
Quarkian
Quarkian
 
Posts: 5271
Joined: 04 Jun 2004, 15:06
Location: http://bit.ly/QuarkHamburg


Return to QuarkXPress 8, 9 & 10: AppleScript

Who is online

Users browsing this forum: No registered users and 1 guest