AppleScript HELP!

Discussion in 'Mac Programming' started by southpaw1017, Jan 23, 2013.

  1. macrumors newbie

    Joined:
    Jan 23, 2013
    #1
    Before I begin, I'm completely new to AppleScript but I am very familiar with the Mac Interface.

    I'm using the application QuarkXPress to export files in a bunch of different formats. To increase my productivity, I made shortcuts for each of the menu commands. Now, to make myself even more productive, I'd like to make a script that will run each menu command for me, in order.

    Anyway, here's the horribly offensive code I've have so far:
    Code:
    tell application "QuarkXPress" to activate
    tell application "System Events"
    	click menu item "Usage..." of menu "Utilities" of menu bar item "Utilities" of menu bar 1 of application process "QuarkXPress"
    	tell application "System Events"
    		click menu item "Collect for Output..." of menu "File" of menu bar item "file" of menu bar 1 of application process "QuarkXPress"
    		tell application "System Events"
    			click menu item "Layout as PDF..." of menu "Export" of menu item "Export" of menu "File" of menu bar item "File" of menu bar 1 of application process "QuarkXPress"
    			tell application "System Events"
    				click menu item "Layouts as Project..." of menu "Export" of menu item "Export" of menu "File" of menu bar item "File" of menu bar 1 of application process "QuarkXPress"
    			end tell
    		end tell
    	end tell
    end tell
    Assuming a lot of you don't use Quark regularly and cannot test this code out for yourself, I'll tell you what happens. I get the first window (Usage...) to open up, but when I'm done with that page, the next window (Collect for Output...) does not open, and the code stops running for all I know. Sometimes, when I run the code one of the other commands will run, but when I am done using that the code will stop. Is there something I can write that will not run the next command until I close the current window?

    Since I'm assuming you guys can't use Quark, could you try and make an example using another application, like Safari?

    Thank you, and sorry for my lack of knowledge!!
     
  2. macrumors 6502

    superscape

    Joined:
    Feb 12, 2008
    Location:
    Yorkshire, UK
    #2
    Mmmm...

    Hi there,

    Personally, I'd use UI scripting as an absolute last resort - I've always found it a bit flakey. I've not scripted Quark Xpress for many years now, but as I remember it had quite a decent scripting dictionary. I'd strongly encourage you to use that if at all possible, only resorting to UI scripting if it turns out that what you need to do is otherwise impossible.
     
  3. superscape, Jan 24, 2013
    Last edited: Jan 24, 2013

    macrumors 6502

    superscape

    Joined:
    Feb 12, 2008
    Location:
    Yorkshire, UK
    #3
  4. macrumors newbie

    Joined:
    Oct 13, 2012
    #4
    Sometimes the script is going at a faster speed than the interface scripting process. It might be helpful to put delay commands between some of the user interface commands.

    Also, you can enclose some of the UI commands in a tell block instead of continuously typing out the name of the UI element (for example: tell application process "QuarkXPress" instead of ending all of the lines with: of application process "QuarkXPress"). And, you don't need to keep 'telling' System Events throughout the script.

    Code:
    tell application "QuarkXPress" to activate
    	tell application "System Events"
    		tell application process "QuarkXPress"
    			tell menu bar 1
    				click menu item "Usage..." of menu "Utilities" of menu bar item "Utilities"
    				delay 1.1 -- number of seconds to wait; increase/decrease as needed.
    				click menu item "Collect for Output..." of menu "File" of menu bar item "file"
    				
    				delay 1.8
    				click menu item "Layout as PDF..." of menu "Export" of menu item "Export" of menu "File" of menu bar item "File"
    
    				delay 2
    				click menu item "Layouts as Project..." of menu "Export" of menu item "Export" of menu "File" of menu bar item "File"
    			end tell
    		end tell
    	end tell
    end tell
     
  5. macrumors newbie

    Joined:
    Oct 13, 2012
    #5
    Argh. You know, I hate it when I post a question and I get a response from someone who has clearly not actually read my question. Please accept my apologies for skimming your query instead of reading it.

    I don't know if this is the most efficient way, but what I do when I need the script to wait for a certain condition is use a repeat loop (example below). In this case, I'm assuming that clicking "Usage..." results in a window of some sort, so I have the script repeat delaying until the "Usage..." window has time to appear after I've clicked the "Usage..." button, and then have the script wait until the window no longer exists. Note that QuarkXPress may not consider them windows, in which case you'll need to refer to them differently. Also, I'm assuming their names match the menu items. You'll need to find out the proper names for the windows/UI elements. And, finally, we're no longer using 'tell menu bar 1' because, more than likely, window will not be an element of the menu bar. So, we've put 'of menu bar 1' back at the end of the 'click menu item' line.

    Code:
    tell application "QuarkXPress" to activate
    	tell application "System Events"
    		tell application process "QuarkXPress"
    				click menu item "Usage..." of menu "Utilities" of menu bar item "Utilities" of menu bar 1
    				repeat until (name of every window) contains "Usage"
    					delay 0.5
    				end
    
    				repeat until (name of every window) does not contain "Usage"
    					delay 1
    				end
    
     

Share This Page