MacOS – Select file from command-line

command linemacos

Is it possible to launch Finder from the command-line to perform a file select action?

The purpose of this would be for the end user to, when running a shell script, be able to select a file using a GUI instead of typing in the path and filename.

Best Answer

Is it possible to launch Finder from the command-line to perform a file select action?

There's some confusion about Finder and what it is/does. You don't "launch" finder in the conventional sense. It's always running. It's the graphical interface through which you interact with your Mac system. You open a file using Finder's file selection tools or view the contents using Preview, etc.

Get to know the Finder on your Mac

The purpose of this would be for the end user to, when running a shell script, be able to select a file using a GUI instead of typing in the path and filename.

Bouncing back and forth between a GUI and a text environment is inefficient. Why would you begin typing a command in a text environment, then switch to a GUI to get your file, then switch back to text? Instead, you create the File Selection Dialog Box right there in the text environment.

There's a tool called dialog available on MacPorts and on Homebrew that allows you to create dialog and file selection boxes in your text environment. Below is an example of the file selection box from the deletefile.sh script (similar to what you described).

FSelect Dialog Box

For more examples of the different dialog boxes you can create, see: https://invisible-island.net/dialog/dialog-figures.html