Created
December 26, 2022 18:36
-
-
Save earwin/1f722d8f4bcf1ea3d3fa72a468f87a0a to your computer and use it in GitHub Desktop.
Sync OSX GUI app environment with command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# zsh and ruby are used for convenience, but both are shipped with macos, so no big deal | |
DONOR_SHELL=/opt/local/bin/zsh # pick the shell you use in terminal and have your env configured in | |
SCRIPT=${0:A} # autodetect this script's absolute path | |
PLIST=~/Library/LaunchAgents/osx-env-sync.plist | |
write_plist() { | |
cat > $1 <<EOF | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>osx-env-sync</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>$SCRIPT</string> | |
<string>sync</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
</dict> | |
</plist> | |
EOF | |
} | |
install() { | |
write_plist $PLIST | |
launchctl load $PLIST | |
} | |
uninstall() { | |
launchctl unload $PLIST | |
rm -f $PLIST | |
} | |
sync() { | |
# `--login -c` stanza is supported by all shells i checked: bash, zsh, fish, xonsh | |
# gotcha: macos zsh users tend to set up environment in .zshrc as opposed to proper .zshenv, | |
# so might want to add `-i` to zsh invocation below | |
env -i $DONOR_SHELL --login -c env | | |
ruby -ne '$_ =~ /^(.+)=(.+)$/; system "launchctl", "setenv", $1, $2' # ruby saves us from fucking with escapes | |
} | |
case $1 in | |
install ) install;; | |
uninstall ) uninstall;; | |
reinstall ) uninstall; install;; | |
sync) sync;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment