Skip to content

Instantly share code, notes, and snippets.

@jcfr
Created August 6, 2025 05:07
Show Gist options
  • Select an option

  • Save jcfr/4eca5ab7d5d0da9706ad4099a07c6949 to your computer and use it in GitHub Desktop.

Select an option

Save jcfr/4eca5ab7d5d0da9706ad4099a07c6949 to your computer and use it in GitHub Desktop.
This script provides a temporary workaround to launch 3D Slicer on macOS while [issue #8618](https://github.com/Slicer/Slicer/issues/8618) is being resolved. It ensures the environment is correctly configured by reading and applying settings from `SlicerLauncherSettings.ini`, allowing the application to start properly from the command line.
#!/usr/bin/env bash
#
# slicer-mac-launch.sh
#
# This script launches 3D Slicer on macOS, setting up environment variables
# based on the contents of SlicerLauncherSettings.ini.
#
# It is a workaround for issue https://github.com/Slicer/Slicer/issues/8618
# and ensures that Slicer can be started properly from the command line
# while waiting for the upstream fix to be implemented.
#
# Usage:
# ./slicer-mac-launch.sh /path/to/Slicer.app [arguments...]
#
set -euo pipefail
# Ensure an app path is provided
if [ $# -lt 1 ]; then
echo "Usage: $0 /path/to/Slicer.app [args...]"
exit 1
fi
APP_PATH=$(realpath "$1")
shift
INI_PATH="${APP_PATH}/Contents/bin/SlicerLauncherSettings.ini"
if [ ! -f "$INI_PATH" ]; then
echo "Error: SlicerLauncherSettings.ini not found at $INI_PATH"
exit 1
fi
SETTINGS_DIR=$(dirname "$INI_PATH")
echo "APP_PATH = ${APP_PATH}"
echo "INI_PATH = ${INI_PATH}"
echo "SETTINGS_DIR = ${SETTINGS_DIR}"
echo ""
# Function to parse INI sections
parse_ini_section() {
local section="$1"
/usr/bin/awk -v section="$section" '
$0 ~ ("^\\[" section "\\]$") { in_section=1; next }
/^\[.*\]/ { in_section=0 }
in_section && /^[^#;]/ && /^[^=]+=/ {
key = $0
sub(/=.*/, "", key)
val = $0
sub(/[^=]*=[[:space:]]*/, "", val)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", key)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", val)
print key "=" val
}
' "$INI_PATH"
}
# Replace placeholder in-place in the INI file (safe for local copy)
# If you prefer not to edit original, create a temporary copy
sed -i.bak "s|<APPLAUNCHER_SETTINGS_DIR>|${SETTINGS_DIR}|g" "$INI_PATH"
# Apply environment variables
eval "$(parse_ini_section "EnvironmentVariables" | while IFS='=' read -r key val; do
echo "export $key=\"$val\""
done)"
# Helper to update colon-separated env vars
append_paths_from_section() {
local section="$1"
local var="$2"
local new_entries=()
echo "Setting $var"
while IFS='=' read -r _ val; do
if [ -n "$val" ]; then
echo " $val"
new_entries+=("$val")
fi
done < <(parse_ini_section "$section")
if [ ${#new_entries[@]} -gt 0 ]; then
local joined
joined=$(IFS=:; echo "${new_entries[*]}")
# Get current value or empty string if unset
local current_val
current_val="$(printenv "$var" || true)"
# Prepend new entries
local new_val="${joined}${current_val:+:$current_val}"
# Export updated variable
export "$var=$new_val"
fi
}
append_paths_from_section "Paths" "PATH"
append_paths_from_section "LibraryPaths" "DYLD_LIBRARY_PATH"
append_paths_from_section "QT_PLUGIN_PATH" "QT_PLUGIN_PATH"
append_paths_from_section "PYTHONPATH" "PYTHONPATH"
# Launch the application
exec "${APP_PATH}/Contents/MacOS/Slicer" "$@"
@jcfr
Copy link
Author

jcfr commented Aug 6, 2025

Download the script:

curl -LO https://gist.githubusercontent.com/jcfr/4eca5ab7d5d0da9706ad4099a07c6949/raw/slicer-macos-launch.sh
chmod u+x slicer-macos-launch.sh

Run Slicer:

slicer-macos-launch.sh /path/to/Slicer.app

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