Skip to content

Instantly share code, notes, and snippets.

@elmimmo
Last active December 18, 2024 21:40
Show Gist options
  • Save elmimmo/dc38b811239c4350924ea42c5fdde6ab to your computer and use it in GitHub Desktop.
Save elmimmo/dc38b811239c4350924ea42c5fdde6ab to your computer and use it in GitHub Desktop.
Run adjacent command-line program from custom directory
#!/usr/bin/env bash
set -e
: ${run_dir:="${HOME}/Library/Application Support/CUSTOM_DIRECTORY"}
# Find absolute path to script
script_path=$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")
if [ -n "$(readlink "$script_path")" ]; then
script_path=$(readlink "$script_path")
fi
mkdir -p "$run_dir" && cd "$_"
"${script_path%.*}"
@elmimmo
Copy link
Author

elmimmo commented Dec 17, 2024

In macOS, double-clicking a command-line program will open it in Terminal and run it from the Home folder, potentially polluting it with whatever output it might create there.

In order to easily have a command-line program run from a custom directory:

  1. Download and place this script in the same folder as the command-line program.
  2. Name the script the same as the command-line program followed by the .tool suffix.
  3. Give the script execute permissions in Terminal by running chmod +x followed by space and dropping the script file in the Terminal window.
  4. Edit the script’s contents so that the value of run_dir between quotes is the path you want to run the command-line program from.

From then on, run the program by double-clicking the script in order to have it ran from the custom directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment