Calling Command-Line Tools
In AppleScript, the
do shell script
command is used to execute command-line tools. This command is implemented by the Standard Additions scripting addition included with OS X.Note
If you ever need to close a Mac OS X application (gracefully) from the Mac Terminal command line or from a shell script, I can confirm that this command works: osascript -e 'quit app 'Safari' I use that command from a Mac/Unix shell script to close the Safari browser in. How to open Terminal on Mac. The Terminal app is in the Utilities folder in Applications. To open it, either open your Applications folder, then open Utilities and double-click on Terminal, or press Command - spacebar to launch Spotlight and type 'Terminal,' then double-click the search result. In this how-to I'll show you how to launch an OS X app from the command line with the open command. Note: A little bit of prior UNIX experience will be helpful here. First off, I will admit this. I need to programmatically open a shell script in a Terminal window and run it with administrator privileges. I'm using osascript as it displays a convenient prompt for an admin login/password. The long ribbon of icons at the bottom of the Mac's screen is called the Dock.
The Terminal app in
/Applications/Utilities/
is scriptable and provides another way to execute command-line tools from scripts. Executing Commands
The direct parameter of the
do shell script
command is a string containing the shell code you want to execute, as demonstrated in Listing 39-1, which simply lists a directory.APPLESCRIPT
Listing 39-1AppleScript: Executing a simple shell command that lists the contents of a directorydo shell script 'ls /Applications/'
(*
--> Result:
'App Store.app
Automator.app
Calculator.app
Calendar.app
..'
*)
Since the direct parameter of
do shell script
is a string, you can concatenate it with other strings at run time. Listing 39-2, for example, concatenates a shell command to a previously defined parameter value.APPLESCRIPT
set theHostName to 'www.apple.com'
do shell script 'ping -c1 ' & theHostName
Quoting Strings
The shell uses space characters to separate parameters and gives special meaning to certain punctuation marks, such as
$
, (
, )
, and *
. To ensure that strings are treated as expected—for example, spaces aren’t seen as delimiters—it’s best to wrap strings in quotes. This process is known as quoting. If your string contains quotes, they must also be escaped (preceded by a /
character) so they are interpreted as part of the string. Listing 39-3 shows an example of an error occurring as a result of a parameter that contains a space.APPLESCRIPT
How To Open App From Shell Mac Os
Listing 39-3AppleScript: An error resulting from a string containing a spaceset thePath to '/Library/Application Support/'
do shell script 'ls ' & thePath
--> Result: error 'ls: /Library/Application: No such file or directoryrls: Support: No such file or directory' number 1
Dnd apps for kindle. The easiest way to quote a string is to use the
quoted form
property of the text class, as demonstrated in Listing 39-4. This property returns the string in a form that’s safe from further interpretation by the shell, regardless of its contents.APPLESCRIPT
Listing 39-4AppleScript: Quoting a string to prevent errorsset thePath to quoted form of '/Library/Application Support/'
--> Result: '/Library/Application Support/'
do shell script 'ls ' & thePath
(*
--> Result:
'App Store
Apple
..
'
*)
More Information
Digital scrapbook app mac. For more information about the
do shell script
command, see Commands Reference in AppleScript Language Guide and Technical Note TN2065.Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13
Terminal User Guide
You can change the default shell that new Terminal windows and tabs open with.
- In the Terminal app on your Mac, choose Terminal > Preferences, then click General.
- Under “Shells open with,” select “Command (complete path),” then enter the path to the shell you want to use.
In a networked environment, a system administrator can specify the default shell. For more information about the features of each shell, enter “man” and the name of the shell.
Open Mac Apps On Windows
See alsoChange General preferences in Terminal on MacApple Support article: Use zsh as the default shell on your Maczsh shell man page