Mac How do you make AppleScript send a "return"?

bingefeller

macrumors 6502a
Original poster
Jun 25, 2007
531
19
Northern Ireland
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.
 

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
How do I get AppleScript to actually change me to that directory so I don't have to press return?
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!
 
  • Like
Reactions: bingefeller

bingefeller

macrumors 6502a
Original poster
Jun 25, 2007
531
19
Northern Ireland
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!
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.
 

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
Thanks superscape.

Can I ask why you avoid UI scripting?
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?
 

Mark FX

macrumors regular
Nov 18, 2011
121
10
West Sussex, UK
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
 
Last edited:

Similar threads

  • Aouttier
7
Replies
7
Views
587
  • Aouttier
5
Replies
5
Views
463
  • gpchess2k
6
Replies
6
Views
996
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.