AppleScript to Export music.app XML nightly

I plan to continue maintaining some playlists in music.app, so I have to re-run the music.app XML export any time my playlists change in music.app. To solve this piece the AppleScript below can be scheduled as an alarm in the calendar app to run nightly, repeatedly replacing the XML you created initially with the latest export from music.app. Note this script assumes you have already performed an initial XML export to the directory where Roon is looking for your music.

Just thought I’d share my solution to this problem!

set targetApp to "Music"
set theMenu to "File"
set theSubMenu to "Library"
set theItem to "Export Library…"

tell application targetApp
	activate
	tell application "System Events"
		tell application process targetApp
			set frontmost to true
			tell menu bar 1
				tell menu bar item theMenu
					tell menu theMenu
						click menu item theItem of menu of menu item theSubMenu
						delay 1.2
						key code 36
						delay 0.2
						keystroke "r" using {command down}
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Screen Shot 2020-04-19 at 3.02.36 PM

1 Like