Select file from command-line
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).
For more examples of the different dialog boxes you can create, see: https://invisible-island.net/dialog/dialog-figures.html
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 withPrompt
attribute