Sub topic 16
Opening and listing tabs from Terminal and AppleScript
This chapter shows how to open a page in Safari from Terminal and how to list your open tabs with AppleScript. It assumes you can open Terminal and Script Editor (both are in Applications > Utilities and Applications). It also assumes you know what tabs and windows are in Safari, as covered earlier in this tutorial.
Why automate tabs at all
Most of the time the mouse and keyboard shortcuts are faster. Automation helps when you want to repeat a task, such as opening the same set of sites each morning, or when you want a plain-text list of what is open. It also serves as a starting point for larger scripts.
There are two tools here:
- The
opencommand, which comes with macOS and starts apps or opens files and URLs from Terminal. - AppleScript, a scripting language that can ask Safari for information, such as the names of its tabs.
Opening a URL with the open command
The -a option tells open which application to use. Without it, macOS uses your default browser, which might not be Safari.
open -a Safari https://www.apple.comIf Safari is not running, it starts. If it is running, the page opens in a new tab in an existing window or in a new window, depending on your Safari settings. You can pass more than one URL, and each one is opened.
open -a Safari https://www.apple.com https://support.apple.comListing tab titles with AppleScript
AppleScript describes actions as sentences. The script below asks Safari for the name (the page title) of every tab in the front window.
tell application "Safari"
get name of every tab of front window
end tellTo run it, open Script Editor, paste the script, and click the Run button. The Result pane at the bottom shows a list of titles, such as {"Apple", "Apple Support"}.
You can also run it from Terminal with osascript. Passing -e supplies one line of script, so you use several -e options for several lines.
osascript -e 'tell application "Safari"' -e 'get name of every tab of front window' -e 'end tell'The automation permission prompt
The first time a script controls Safari, macOS asks whether the app running the script may control Safari. That app is Script Editor or Terminal. Click OK to allow it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The page opens in a different browser | You left out `-a Safari` | Add `-a Safari` |
| "Not authorized to send Apple events" | Automation permission was denied | Turn Safari on in Privacy & Security > Automation |
| "Can't get window 1" or a similar error | Safari has no open windows | Open a window first, or add a check in the script |
| Only some tabs are listed | The script reads only the front window | Loop over every window, as in the next section |
Listing tabs from every window
front window covers one window only. To cover all windows, repeat the same request for each one.
tell application "Safari"
set allTitles to {}
repeat with w in windows
set allTitles to allTitles & (name of every tab of w)
end repeat
return allTitles
end tellThe script builds an empty list, adds each window's tab titles to it, and returns the combined list.
Exercise
Which command opens https://support.apple.com in Safari even when Safari is not your default browser, and why does your AppleScript fail with "not authorized" the first time you decline the permission prompt?
Show answer
Use open -a Safari https://support.apple.com. The -a Safari option names the application. The script fails because macOS blocks the app running it (Terminal or Script Editor) from controlling Safari until you allow it under Privacy & Security > Automation.
Exercise
How would you change the script get name of every tab of front window to list the tab titles of all open windows?
Show answer
Loop over windows with repeat with w in windows, and collect name of every tab of w into one list, as in the "Listing tabs from every window" script.