Interchangeability of Finder, Terminal, and AppleScript

For many of its features, macOS offers you several different ways of doing the same thing. Even something as simple as shutting down or restarting can be initiated using the Finder’s menu commands, with keystroke commands, forced using the Power button, or with the shutdown or reboot commands in Terminal.

There are subtle differences between some of them. The preferred keystroke commands are Control-Command-Eject (to restart) and Control-Option-Command-Eject (to shut down), as they close all open apps in an orderly fashion first. But if you just want a quick return to where you are now, use Control-Command-Power button (restart) or press and hold the Power button (shutdown).

You can achieve the same thing in Terminal by typing
sudo shutdown -r now
to restart (that’s the -r option), or
sudo shutdown now
to shut down. In both cases, you will then have to authenticate with your admin password. These are handy because they can be called by an AppleScript, using its do shell script command, with the authentication option:
do shell script "shutdown -r now" with administrator privileges
for example.

Rough equivalents using halt and reboot are
sudo reboot
to restart immediately, or
sudo halt
to shut down immediately.

Just as AppleScript can call shell commands, so you can execute AppleScript from the command shell, using the command osascript: to understand all its options, read its man pages using man osascript.

Opening and running an app is also easy to achieve in many different ways. In the Finder, you can double-click its icon or use a launcher such as the Dock, or merely select its icon and use the Finder’s Open command. They are not identical: Gatekeeper checks downloaded apps’ signatures when they are first run. If Gatekeeper cannot find a valid signature, it will only offer you the option to open the app if you use Finder’s menu command, and not after a double-click, for example.

In Terminal, you would normally run a command tool by simply typing its name and options, as in the above examples. That doesn’t work with proper apps, for which you must use the open command with its -a option:
open -a BBEdit
for instance. By default, open looks in the top-level Applications folder; if the app is installed somewhere else, you will need to provide its full pathname.

In AppleScript, you can launch an app using two different commands
run application "BBEdit"
or
tell application "BBEdit" to run
There’s also nothing to stop you from using the do shell script command with open either, if you want to go around the houses a bit more.

Much of the time, these alternatives are esoteric knowledge which you will not use. However there are times when they come in handy, such as when the world is playing up and all you can interact with is an open Terminal window, or remotely via a network connection and the secure shell ssh.

The one time that they could come in handy is when you have restarted into Recovery mode because of problems. Unfortunately, Apple has there locked out most of them from use. The cut-down version of macOS which runs in Recovery mode does not contain commands like open or osascript. Although your normal startup disk is mounted in /Volumes, and you can try most of them from their paths on that disk, only shutdown, reboot, and halt will actually work. Commands like open, which could be so useful, or the ability to run an AppleScript, cannot work in Recovery mode because of its cut-down version of macOS.

One way round that limitation is to build yourself a recovery memory stick, onto which you can install most of macOS, and start up from that rather than Recovery mode.

Key command tools which are missing from Recovery mode include most of those in /usr/bin and /usr/sbin, such as open, osascript, and log. There is also no man command, so you will need to remember how to use the commands which are available to you.

So Recovery mode does limit you to the basic utilities including macOS installer, Disk Utility and its repair tools, basic file and disk maintenance commands (including diskutil and disktool), ssh, basic network testing such as ping, and the likes of scutil. Neither can you customise your Recovery partition with the additional tools which you fancy.