How do you make AppleScript send a "return"?

Discussion in 'Mac Programming' started by bingefeller, Jul 24, 2017.

  1. bingefeller macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #1
    I've looked in various places and I can't find the answer. I have written a script to launch the Terminal and I want to open a certain directory, like this:

    Code:
    tell application "Terminal"
        activate
        delay 1
        tell application "System Events"
            keystroke "cd /Applications/mame0187-64bit"
        end tell
    end tell
    How do I get AppleScript to actually change me to that directory so I don't have to press return? There is probably an easier way of doing it than what I've done.

    Many thanks.
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi there,

    I tend to avoid UI scripting like the plague, but I believe you can send a return like this:

    Code:
    tell application "System Events"
       keystroke return
    end tell
    
    ...however, seeing you trying to script Terminal makes me wonder whether you wouldn't be better off just using a "do shell script" in AppleScript. For example, if you wanted to list all the files in your Music folder using the find command, you don't need the Terminal, you can just do:

    Code:
    set theFiles to every paragraph of (do shell script "find ~/Music -type f")
    
    Good luck!
     
  3. bingefeller thread starter macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #3
    Thanks superscape.

    Can I ask why you avoid UI scripting? I have zero knowledge and just thought this was the best and easiest way to get to the directory I want.

    I looked at shell scripts, to see if I could figure out to make one do what I want, but it looks complex.
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Mostly because it's glitchy - you just need the developer of the app you're scripting to move a button or change a keystroke combination and your code breaks. It has its use cases, but I tend to think of it as an absolute last resort.

    What do you want to do when you get to that directory?
     
  5. Mark FX, Jul 24, 2017
    Last edited: Jul 24, 2017

    Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #5
    Using your example code, you would insert the "return" as superscape suggested like below.

    Code:
    tell application "Terminal"
       activate
       delay 1
       tell application "System Events"
           keystroke ("cd /Applications/mame0187-64bit" & (return as text))
       end tell
    end tell
    
    And I agree with suoerscape, you shouldn't be looking at UI scripting or indeed "Terminal" scripting for your project, unless it's absolutely necessary, and there's no other way to achieve what you want.

    AppleScript and the AppleScriptObjC Framework should provide all you need for most tasks.
    And if you find you need to use command line tools and utilities, then as suggested use an AppleScript "do shell script" command, or use the Cocoa NSTask class to run shell commands.

    Regards Mark
     

Share This Page