Ledge

Getting a file out of the terminal and into another app

Type open -R followed by the file name and Finder opens a window with that file already selected, ready to drag into any app. To skip Finder entirely, open -a "App Name" followed by the file hands it straight to a named app. And when what you actually need is the text, not the file, pipe it into pbcopy and paste.

Those three commands cover most of the trip from a command line back to the rest of the Mac. Each has a point where it stops helping, and that is where the dragging starts.

Reveal the file in Finder with open -R

You have just run a script that wrote exports/q3-summary.pdf, and now it needs to go into an email. The terminal knows exactly where that file is; Finder has no idea you care about it.

open -R exports/q3-summary.pdf

That opens a Finder window on the exports folder with q3-summary.pdf highlighted. From there it is an ordinary Finder file: drag it into a Mail message, onto a chat window, into an upload field.

A few variations are worth keeping in your fingers:

  • open . opens the folder you are in as a Finder window, with nothing selected.
  • open .. opens the folder above it.
  • open exports opens a named folder.

Paths follow the usual shell rules. A name with spaces needs quotes, so open -R "Q3 Summary final.pdf", or let Tab completion escape it for you. If the path is wrong, open says so plainly: The file /Users/you/exports/q3-sumary.pdf does not exist. That message usually means a typo, or that you are in a different directory than you think (pwd will tell you which).

Hand it straight to an app with open -a

When you already know the destination app, Finder is an extra step. The -a flag names the app to open the file with:

open -a Preview chart.png
open -a TextEdit notes.txt
open -a "QuickTime Player" demo.mov

Use the app’s name as it appears in the Applications folder, in quotes if it has a space. A misspelling such as open -a Previw chart.png fails with Unable to find application named 'Previw' and nothing opens; fix the spelling, or give the full path instead, as in open -a /Applications/TextEdit.app notes.txt. For scripts that need to be exact, -b takes a bundle identifier, so open -b com.apple.Preview chart.png works whatever the app happens to be called on that Mac.

Plain open with no flag opens the file in its default app, the same as a double-click in Finder.

What open -a does is ask the app to open the file, the same thing as dropping it on the app’s Dock icon. That suits viewers and editors. Mail usually responds by starting a new message with the file attached. It does not help with destinations that are not apps at all: a channel in a chat app, a web upload form, a specific folder. For those you need the file in your hand, which means revealing it and dragging.

If you want to change which app opens a file type by default, rather than overriding it once from the terminal, handing a file to an app by name covers Open With and Get Info.

When you need the text, not the file: pbcopy

Sometimes the thing you want out of the terminal is not a file but what is in it, or what a command printed. pbcopy reads whatever is piped into it and puts it on the clipboard:

pbcopy < notes.md
ls -1 exports | pbcopy
realpath exports/q3-summary.pdf | pbcopy

The first copies the contents of a text file. The second copies a list of file names, one per line (how to copy a list of file names from a folder has the Finder route and the variations). The third copies a file’s full path, ready to paste into a document or a chat message. Its partner, pbpaste, prints the clipboard back into the terminal.

Two limits. pbcopy handles text: it stores input as plain text, unless the input begins with a Rich Text header, in which case it goes on the clipboard as rich text. And for long output, a document is often more useful than a clipboard: ls -l | open -f writes the output to a temporary file and opens it in your default text editor, where you can read it, trim it, and save it somewhere sensible.

Dragging from the Terminal window itself

Terminal can also hand you the folder you are working in without a command. When the window title shows the working directory (in Terminal’s Settings, under Profiles and then Window, the title options include “Working directory or document”), the folder’s icon appears in the title bar next to its name. That icon behaves like the folder: drag it into a Finder window or a Save dialog. Command-click the title to see the full path as a menu and jump to any folder above it in Finder.

This is the same proxy icon that document apps put in their title bars, and how to drag a file from a window’s title bar explains how it behaves in other apps.

Where a shelf helps

The weak point in all of this is the moment after open -R. Finder comes forward, usually on top of the app you wanted to drop into, and you are now arranging windows with a file held under your finger. If the destination is a browser upload page or a chat thread on another Space, that is a long drag.

Ledge splits it into two short ones. Reveal the file, drag it up to the notch at the top of the screen (the top center on a Mac without a notch), and let go. It waits on a shelf. Run the next command, reveal the next output, park that too. When you are ready, go to the destination, press Option-Space to drop the shelf down, and drag the files off.

Each destination gets the version it can use. A chat app receives an upload of the file, a Finder folder gets the file itself (Ledge moves it there), and dropping the same item back onto a terminal gives the terminal its path as text, which closes the loop described in how to give a terminal a file path instead of the file. The first time Ledge pastes a path into a terminal for you, macOS asks for Accessibility permission; it is not requested before then. Nothing leaves the Mac along the way, and there is no account.

Ledge does not pull files out of the terminal, and it does not know what a command produced. You still find the file with open -R or in Finder; the shelf simply holds it so the second half of the trip happens when you are ready.

Questions

Does open work when I am connected to another machine over SSH?

No. Inside an SSH session, commands run on the remote machine, so open tries to act there, not on your Mac. On a Linux server it is either missing or an unrelated command. Copy the file down first (for example with scp), then use open -R locally. Why dragging a file into an SSH session gives the wrong path covers the same split from the other side.

What is the quickest way to open a text file in an editor from the terminal?

open -e notes.txt opens it in TextEdit. open -t notes.txt opens it in whatever your Mac uses as its default text editor, which may be a different app if you have changed it.

Can I copy the file itself from the terminal, so I can paste it into Finder or Mail?

Not with pbcopy, which puts what you pipe into it on the clipboard as text (or rich text), never as a file. Reveal the file with open -R, press Command-C in the Finder window that opens, and paste wherever the file needs to go.