Simple AppleScript Help Needed

Discussion in 'Mac Programming' started by MichaelQuick232, Oct 14, 2011.

  1. MichaelQuick232 macrumors newbie

    Joined:
    Oct 14, 2011
    #1
    Hello all!
    I'm relatively new here, and to AppleScript. However, I'm tryiong to get terminal to do this

    (Script Starts)
    1)open terminal
    2) enter command "heyu off A2"
    3) hit "return"
    4) wait 3 seconds
    5) close terminal

    I currently have this script, but it's not seem to working.

    tell application "Terminal"
    activate
    keystroke heyu off A2
    keystroke (ASCII character 13)
    delay 3
    quit
    end tell


    any help?

    thanks!

    --Mike
     
  2. Wondercow macrumors 6502a

    Joined:
    Aug 27, 2008
    Location:
    Toronto, Canada
    #2
    There are two reasons it's not working:

    keystroke must be issued through System Events
    keystrokes themselves must be enclosed in quotes, otherwise the system will assume that you mean to issue the command keystroke heyu off A2 and, obviously, it won't understand.

    Try this:

    Code:
    tell application "Terminal"
    	activate
    	tell application "System Events"
    		keystroke "heyu off A2"
    		keystroke return
    	end tell
    	delay 3
    	quit
    end tell
    
     
  3. MichaelQuick232 thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #3
    Thanks!! It worked like a charm. the only thing is, after it says "quit" after i turn it into an application in the Save As... and when I run it, it brings up a dialog box that says
    "Connection is invalid." (to the terminal) how do i get it to close that box at the end of the script too?

    --Mike
     
  4. chown33, Oct 15, 2011
    Last edited: Oct 15, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    You should look up the AppleScript command do shell script.

    Example:
    Code:
    do shell script "heyu off A2 ; sleep 3"
    

    On the larger question, you can't tell an application to quit within a tell/end tell block. The reason is that quit breaks the connection by quitting the application, but the tell block needs a connection. It's like telling someone you're talking to on the phone, to hang up the phone then you'll tell them what to do next. Catch-22.

    To tell an application to quit, you must use the one-line form:
    Code:
    tell app "Terminal" to quit
    
    and it must be outside the tell/end block of any other communications with that app.
     

Share This Page