Skip to content

Instantly share code, notes, and snippets.

@mazbox
Last active January 13, 2023 01:25
Show Gist options
  • Save mazbox/4fde165eab9c7ae2368381c2d0988cce to your computer and use it in GitHub Desktop.
Save mazbox/4fde165eab9c7ae2368381c2d0988cce to your computer and use it in GitHub Desktop.
play/pause Music.app on Mac when running
#!/bin/zsh
musicAppOpen=`osascript -e 'tell application "System Events" to (name of processes) contains "Music"'`
rm -f "/tmp/xcode-itunes.txt"
if [ $musicAppOpen = 'true' ]; then
musicState=`osascript -e 'tell application "Music" to player state as string'`
if [ $musicState = 'playing' ]; then
echo 'play' > '/tmp/xcode-itunes.txt'
osascript -e 'tell application "Music" to pause'
fi
fi
#!/bin/zsh
musicAppOpen=`osascript -e 'tell application "System Events" to (name of processes) contains "Music"'`
if [[ $musicAppOpen = 'true' && -f "/tmp/xcode-itunes.txt" ]];
then
osascript -e 'tell application "Music" to play'
fi
@mazbox
Copy link
Author

mazbox commented Jan 12, 2023

Simple shell scripts for pausing your music if playing when running app from Xcode, and unpausing when app finishes running.

If Music.app isn't open or you're not playing any music, the scripts won't attempt to unpause.

INSTALLATION

  1. Save somewhere where you keep scripts, e.g. ~/bin and chmod +x them.
  2. In Xcode, go to Xcode > Behaviors > Edit Behaviors...
  3. In left-hand column, scroll down to the "Running" section and click "Starts" (e.g. when running starts)
  4. In right-hand column, scroll down to "Run", choose "Choose Script..." and choose Xcode-music-pause.sh
  5. repeat steps 3 and 4 for "Completes" in the "Running" section and choose "Xcode-music-play.sh"

the first time you run it, you will have to allow it permission but after that you're golden.

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