Scripting the Non-scriptable
Page 3

Using GUI Scripting as
Export to PDF in QuarkXPress

Since the export as PDF feature in QuarkXPress is not directly accessible via AppleScript, the export as PDF window is a prime candidate for GUI scripting. Let's walk through the process of navigating this window via GUI scripting.

First, we will begin by targeting the system events application. Each running application is recognized as a process of the system events application. So, you'll need to ensure that QuarkXPress is running and in the front when you attempt to run your code. This can be done by activating QuarkXPress prior to targeting the system events code. For example:

-- Launch QuarkXPress and bring it to the front

tell application "QuarkXPress" to activate

-- Target the System Events application

tell application "System Events"

     tell process "QuarkXPress"

          -- We'll add our GUI scripting code here    

     end tell

end tell

Next, we are ready to open and navigate the export as PDF window. To do this, we will instruct system events to click the file > export > layout as PDF menu in QuarkXPress. Since this window may take a moment to open, we will add an AppleScript repeat statement to wait until the export as PDF window exists. For example:

-- Activate QuarkXPress

tell application "QuarkXPress" to activate

-- Target System Events

tell application "System Events"

     tell process "QuarkXPress"

          -- Select the File > Export > Layout as PDF... menu

          click menu item "Layout as PDF..." of menu 1 of menu item "Export" of menu "File" of menu bar 1

          -- Wait for the Export to PDF window to appear

          repeat until window "Export as PDF" exists

          end repeat

     end tell

end tell

When the export as PDF window is displayed, by default, the cursor will be in the save as field. Therefore, we will tell system events to enter a name for the PDF to be created, using the keystroke command. Since navigating to a specific folder on the hard drive may prove difficult using GUI scripting, we will instruct system events to type the D character, while holding the command key, to automatically select the desktop. Then, we will have system events type a return character, that will select the save button and dismiss the export as PDF window. Here's an example of the completed script:

-- Activate Quark

tell application "QuarkXPress" to activate

-- Target System Events

tell application "System Events"

     tell process "QuarkXPress"

          -- Select the File > Export > Layout as PDF... menu

          click menu item "Layout as PDF..." of menu 1 of menu item "Export" of menu "File" of menu bar 1

          -- Wait for the Export as PDF window to appear

          repeat until window "Export as PDF" exists

          end repeat

          -- Enter a name for the PDF

          keystroke "My Project"

          -- Select the desktop

          keystroke "D" using command down

          -- Dismiss the window

          keystroke return

     end tell

end tell

In the example above, we typed a return character to dismiss the export as PDF window. As an alternative, we could have written code to dismiss the window by actually clicking the save button in the window. For example:

click button "Save" of window "Export as PDF"

1 | 2 | 3 | 4 | PDF Version

X-Ray Magazine
X-Ray Magazine Logo
If you enjoy our articles, click here to subscribe.
AddThis Social Bookmark Button
 
Free JavaScripts provided by The JavaScript Source

 

Quite Imposing Plus an Acrobat Plug-in
Cre8 Conference: Add More Skills to Your Team's Tool Belt
CopyFlow Gold for QuarkXPress 7 and InDesign CS3
Cre8 Conference: Add More Skills to Your Team's Tool Belt
Creative Public