Connect apps and files

Open files, URLs, and applications

Use the macOS open command to hand an item to Launch Services or select a specific application deliberately.

The open command connects shell work to Mac applications. It opens a file with its default app, opens a URL, or picks a specific application. Behind it sits Launch Services, the same system that decides what happens when you double-click something in Finder.

The three forms I use every day:

open report.pdf
open https://localhost:4321/
open -a "Visual Studio Code" project

The first line opens the PDF in whatever app owns PDFs, Preview on most Macs. The second opens your dev server in the default browser. The third overrides the default: -a opens the item with a specific application, here a project folder handed to an editor.

Two variants for automations

open .
open -R ~/Projects/acme/media/acme-2026-08-03.png

open . shows the current directory in Finder. It’s the fastest bridge from a terminal session to the GUI.

open -R reveals the file in a Finder window and selects it. This is the polite way for an automation to finish. The user sees exactly what was produced, already selected. Our screenshot sorter ends with this line.

Check the exit status

Verification is direct. The right app comes to the front with the right content. For scripting, check the exit status too:

open missing.pdf
# The file /Users/flavio/missing.pdf does not exist.
echo $?
# 1

A non-zero exit lets your script stop, instead of carrying on without the document it was supposed to show.

Validate before you open

Opening is a user-visible side effect. open launches whatever it’s given. A URL pointing somewhere hostile. A file whose extension hides what it really is.

In an automation, an unexpected open is your machine acting without you. So validate every path and URL first, especially when the input comes from another program or from downloaded data.

My rule: allow only the schemes you expect, like https:, and only paths inside your project. Refuse everything else before the command runs:

case "$target" in
  https://*|"$HOME/Projects/"*) open "$target" ;;
  *) echo "refusing to open: $target" >&2; exit 1 ;;
esac

That’s a few lines, and it turns open from a small risk into a boring, predictable step.

Try this: add open -R "$destination" as the last line of your sort script, run it once, and watch Finder select the new file.

Lesson completed