Skip to content

Instantly share code, notes, and snippets.

@dakcarto
Last active July 17, 2020 14:34
Show Gist options
  • Save dakcarto/7091022 to your computer and use it in GitHub Desktop.
Save dakcarto/7091022 to your computer and use it in GitHub Desktop.
Simple shell script to set/replace Mac .app bundle environment variables
#!/bin/bash
# usage: <script> "path to .app bundle"
# convert to absolute path (needed by 'defaults' command)
CMD="import os, sys; print os.path.realpath(\"$1\")"
# echo $CMD
APP=$(/usr/bin/python -c "$CMD" )
if ! [[ "$APP" = /* ]]; then
echo "App path not absolute:"
echo "$APP"
exit 1
fi
DOMAIN="$APP/Contents/Info"
PLIST="$DOMAIN.plist"
if ! [ -f "$PLIST" ]; then
echo "Info.plist not found at:"
echo "$PLIST"
exit 1
fi
# first delete any LSEnvironment setting, ignoring errors
# CAUTION!: this may not be what you want, if the .app already has LSEnvironment settings
defaults delete $DOMAIN "LSEnvironment" 2> /dev/null
defaults write "$PLIST" "LSEnvironment" '{
"DYLD_FRAMEWORK_PATH" = "/usr/local/osgeo4mac/Frameworks:/System/Library/Frameworks";
"DYLD_VERSIONED_LIBRARY_PATH" = "/Users/larrys/QGIS/osgeo4mac/build/output/lib:/Users/larrys/QGIS/osgeo4mac/build/PlugIns/qgis:/usr/local/osgeo4mac/opt/sqlite/lib:/usr/local/osgeo4mac/opt/libxml2/lib:/usr/local/osgeo4mac/lib";
"PATH" = "/usr/local/osgeo4mac/bin:/usr/local/osgeo4mac/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin";
"GDAL_DRIVER_PATH" = "/usr/local/osgeo4mac/lib/gdalplugins";
"PYTHONHOME" = "/usr/local/osgeo4mac/Frameworks/Python.framework/Versions/2.7";
"PYTHONPATH" = "/usr/local/osgeo4mac/lib/python2.7/site-packages";
"PYQGIS_STARTUP" = "/usr/local/osgeo4mac/_osgeo4mac/pyqgis_startup.py";
}'
# leave the plist readable; convert from binary to XML format
plutil -convert xml1 -- "$PLIST"
# update modification date on app bundle, or changes won't take effect
touch "$APP"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment