3

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.

6
2

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

2
  • While switching between text and gui applications is inefficient, it makes sense for a script that is being written for a non-technical user who has little to no experience with command-line interfaces. I appreciate the added context and the list of widgets! I'll check those out! – Jack Aug 17 '20 at 18:35
  • I think there’s some in “Oh My ZSH” ( or available as plugins). I don’t use that tool personally, but you may run across it in your travels. – Allan Aug 17 '20 at 18:53
0

You can launch GUI Dialog to select a file in shell script with following code:

theSelectedFile="$(osascript -l JavaScript -e 'a=Application.currentApplication();a.includeStandardAdditions=true;a.chooseFile({withPrompt:"Please select a file to process:"}).toString()')"

echo "Selected file: $theSelectedFile"

You can customize Window title: see withPromptattribute

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .