MacOS applications are typically launched via the open
command.
But the open
command does not support passing arguments to the application being opened.
However, you can use the open
command with the -a
option to open an application, and you can specify the application name instead of the path:
open -a Eclipse
This will not open a specific project directly, since Eclipse does not natively support opening specific projects via command line.
An alternative is to create an AppleScript that starts Eclipse and then uses the UI scripting capabilities of AppleScript to navigate the menus to open a project. You can see an example in "Open multiple Eclipse workspaces on the Mac"
It is better to use a shell script as a wrapper around the AppleScript, since AppleScript itself does not support command line arguments.
For instance:
#!/bin/bash
# Check if a parameter was given
if [[ $# -eq 0 ]] ; then
# No parameter given, ask for workspace
osascript -e '
-- Ask for a new workspace path
set newWorkspace to choose folder with prompt "Select your workspace folder:"
-- Open the selected workspace
do shell script "open /Applications/Eclipse.app -n --args -data " & POSIX path of newWorkspace'
else
# Parameter given, use as workspace
workspace="$1"
# If the parameter is a dot, expand to current working directory
if [[ "$workspace" == "." ]] ; then
workspace="$PWD"
fi
# Open the workspace
osascript -e "do shell script \"open /Applications/Eclipse.app -n --args -data $workspace\""
fi
You can save this script in a file, for example eclipse_workspace.sh
. Make sure to make it executable by running chmod +x eclipse_workspace.sh
.
You can then call this script from the command line with or without a parameter:
- With a parameter:
./eclipse_workspace.sh /path/to/workspace
- With a dot as a parameter:
./eclipse_workspace.sh .
- Without a parameter:
./eclipse_workspace.sh
Result:
- In the first case, it will use the given path as the workspace.
- In the second case, it will use the current working directory as the workspace.
- In the third case, it will ask you to select a workspace.
You can then define an alias to eclipse_workspace.sh
(I like alias e=/path/to/eclipse_workspace.sh
).
And then, from command-line, you would type...
e
Or
e .
Or
e /path/to/workspace