Sub topic 16
Opening and listing tabs from Terminal and AppleScript
This chapter shows how to open pages in Safari from Terminal and how to read the title and address of every open tab with AppleScript. It is a small side topic. Everything you do here can also be done with the mouse, so treat it as an optional extra for repeatable tasks.
It assumes you know the Terminal basics: running a command, quoting arguments, and saving a text file. It also assumes you know the earlier chapters' vocabulary of windows, tabs, tab groups, and profiles.
Two tools for two jobs
macOS gives you two ways to control Safari from the command line. They do different things.
| Tool | What it does | What it cannot do |
|---|---|---|
| `open` | Asks macOS to launch an app or open a URL or file with it | Read anything back from Safari |
| `osascript` | Runs AppleScript, which sends commands to Safari and gets answers | Nothing special, but it needs your permission first |
Use open when you only need to send a page to Safari. Use AppleScript when you need to know what Safari is showing, or when you need more control over which window or tab is used.
Opening pages with open
The basic form names the application and the address:
open -a Safari https://www.apple.com-a means "use this application". Safari launches if it is not running. Otherwise the page opens in Safari and Safari comes to the front.
Some useful variations:
# Open several pages at once
open -a Safari https://www.apple.com https://support.apple.com
# Open a local HTML file
open -a Safari ~/Documents/report.html
# Open without bringing Safari to the front
open -g -a Safari https://www.apple.comKeep these details in mind:
- Write the full address, including
https://. Without it,opentreats the text as a file name. - Quote addresses that contain
&,?, or#. Otherwise the shell interprets them. For example, useopen -a Safari "https://example.com/search?q=mac&page=2". - Where the page appears depends on your Safari settings, such as whether links open in a new tab or a new window.
opengives you no control over that choice. opendoes not report which tab or window it used. You can only tell by looking, or by listing tabs with AppleScript.
Listing tab titles with AppleScript
AppleScript is macOS's built-in scripting language. Apps that support it expose objects, such as windows and tabs, with properties you can read. Safari exposes window, tab, name, and URL.
This is the shortest useful script. It returns the titles of all tabs in the frontmost window:
tell application "Safari"
get name of every tab of front window
end tellRun it from Terminal by passing it to osascript:
osascript -e 'tell application "Safari" to get name of every tab of front window'The result is a comma-separated list on one line. That is hard to read when titles contain commas, so a loop is a better choice for anything beyond a quick check.
Listing every tab in every window
This script prints one line per tab, with the title and address separated by a vertical bar. Save it as list-tabs.applescript:
tell application "Safari"
set output to ""
repeat with w in windows
repeat with t in tabs of w
set tabTitle to name of t
set tabURL to URL of t
if tabTitle is missing value then set tabTitle to ""
if tabURL is missing value then set tabURL to ""
set output to output & tabTitle & " | " & tabURL & linefeed
end repeat
end repeat
return output
end tellRun it:
osascript list-tabs.applescriptThe two missing value checks matter. A new empty tab has no address, and joining text with missing value raises an error. Without the checks, the script fails the first time you have a blank tab open.
To save the list to a file, redirect the output:
osascript list-tabs.applescript > ~/Desktop/open-tabs.txtOpening a tab in the front window
open cannot choose a window, but AppleScript can. This script takes an address as an argument and opens it in a new tab of the frontmost window. If Safari has no windows, it creates one. Save it as open-tab.applescript:
on run argv
set theURL to item 1 of argv
tell application "Safari"
activate
if (count of windows) is 0 then
make new document with properties {URL:theURL}
else
tell front window
set current tab to (make new tab with properties {URL:theURL})
end tell
end if
end tell
end runRun it with the address as an argument:
osascript open-tab.applescript "https://www.apple.com"Passing the address as an argument is safer than building a script string in the shell. The address is treated as plain data, so quotes or special characters in it cannot change what the script does.
The permission prompt
The first time a script controls Safari, macOS asks whether the app that ran it, usually Terminal, may control Safari. Click OK. If you click Don't Allow, or the prompt never appears, the script fails with an error like this:
execution error: Not authorized to send Apple events to Safari. (-1743)To fix it, open System Settings, go to Privacy & Security, then Automation. Find your terminal app and turn on the switch for Safari. Menu names can differ slightly between macOS versions.
The permission belongs to the app that launched the script. If you run the same script from another terminal app, from Script Editor, or from a scheduled job, each one needs its own approval.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Not authorized to send Apple events` (-1743) | Automation permission denied | Enable Safari for your terminal app in Privacy & Security, then Automation |
| `Can't get window 1` or `Can't get front window` (-1728) | Safari has no open windows | Check `count of windows` first, as `open-tab.applescript` does |
| `Can't make missing value into type text` | A tab has no title or address | Test for `missing value` before joining text |
| The page opens, but in an unexpected place | `open` follows your Safari link settings | Use AppleScript with `front window` when placement matters |
Limits to know about
front windowis the window that was most recently active. If you are working in a Private Browsing window or in a window from another profile, it may be the one the script reads. Check what is in front before you run a script that lists or opens tabs.- Safari's scripting support for profiles and tab groups is limited. Do not assume you can switch profiles from a script. Open Script Editor, choose File, then Open Dictionary, and pick Safari to see exactly what your version supports.
- Running JavaScript inside a page with
do JavaScriptneeds an extra setting in Safari's Develop menu. You do not need it for listing or opening tabs.
Exercises
Exercise
You run open -a Safari https://example.com/search?q=mac&page=2 and Safari opens a page with no search term. Why, and how do you fix it?
Show answer
The shell treats & as "run the previous command in the background" and ? as a filename wildcard, so the address is cut off or altered. Wrap the address in quotes: open -a Safari "https://example.com/search?q=mac&page=2".
Exercise
Your tab-listing script worked yesterday but fails today with "Can't make missing value into type text". Which change in Safari most likely caused it, and which line fixes it?
Show answer
A tab with no title or address, such as a new blank tab, is now open. Its name or URL is missing value. Add a check like if tabURL is missing value then set tabURL to "" (and the same for the title) before joining the text.
Exercise
Why does open-tab.applescript take the address through on run argv instead of having the shell paste the address into an osascript -e string?
Show answer
With an argument, the address is data and cannot change the script's code. Pasting it into a script string means quotes or other special characters in the address could break the script or alter what it does.