Applescript and Quark

Sorry for the delay on approval of comments. I’ve been busy and I didn’t realize people were contributing. Thanks for all the recent contributions. I really put most of these scripts here as a repository for myself. If I’m working on a project and I can’t remember how I did something I go back to here to find some of my own code documented. And now with your comments, I have some additional code to dig into.

Applescript is really quite a handy tool. It can be frustrating. I’ve been trying to write a script that will drop pages out of a Quark document based upon whether the page has a text box with a certain keyword. The reason for this is that I use a variable data plug-in called DesignMerge that will take a tab delimited text file as source and fill in all kinds of tagged areas of a Quark document, with some pretty nice logic to do other things based on the data, too. However, one thing I couldn’t do was drop out a page if it wasn’t wanted. So what I decided to do was use DesignMerge to put a text box on the page with the word DDDelete in it. At the end of the merge, I will run the Applescript to find the pages with that word in them. If it finds them it deletes the page. Here’s the script:

tell application “QuarkXPress”
tell document 1
set pageCount to count pages
repeat with x from 1 to pageCount by 1
–say x
if page x exists then
if text box 1 of page x exists then
if word 1 of text box 1 of page x is “DDDelete” then
delete page x
end if
end if
end if
end repeat
end tell
end tell

I have to put some if statements in there to verify there is a text box on the page, and also to verify there is a page there. If you start with 100 pages, when you get to the end of the script you might only have 80 left because it deletes them as it goes. That’s why I put in if page x exists. So what I now have is a 100 page Quark document. I send in to it tabbed copy with either yes or no values for the pages I want in the document. It builds a Table Of Contents based on that input using Design Merge logic, and then it puts a text box with the word “DDDelete” (intentionally mispelled) at the beginning of the page I want to drop. At the end of the merge I run the script to drop the unnecessary pages. Wala, a requested booklet.

Uggh, no

The page count is right, but the wrong pages were dropped. To correct the problem, I reversed the code so it starts at the end of the document and works forward as it drops pages. Thus the main change is: repeat with x from pageCount to 1 by -1. Now it drops the right pages. There is another advantage here. Now I don’t have to check if the page exists, so there is one less if condition per loop. So I can now drop: if page x exists then. Now it works. This script will probably continue to get tweaked, but now it really does what I need it to do. For a little extra security, I added save document 1 before the last end tell.

Comments are closed.