Shortcut for loading different TV.app libraries

I had thousands upon thousands of videos and movies that I would watch on my Apple TV through the TV app. And even though yes I created different playlists to keep things organized, it just became way too much.

With Media Sharing and Home Sharing enabled in System Settings, the only videos or movies that I could watch on my Apple TV are from the TV library that I currently have loaded in the TV.app in macOS. This becomes especially handy when you have guests or friends over and you are watching movies on your Apple TV and you don’t want them to see every movie you have as you are scrolling through. This is where having different TV libraries comes in very handy.

Because the TV app allows to load different libraries to choose from, I decided to make a whole bunch of different library folders and library files to keep things organized. For example, I created a movies library folder for my movies, a library folder for my music videos, etc….

The only problem was choosing different TV libraries to load into the TV app became a pain in the ass. It meant having to close the TV app if it was currently running then holding the option key while I re-launched the TV app from the dock. (holding the option key while I re-launching the TV app is what brings up the window giving you the option to choose a different library to load). But then once that window opens you still have to scroll through different folders and find the .tvlibrary file you want to load. That was a pain in the ass also.

So here is a shortcut that allows you to choose the TV library file to load into the TV app automatically. The only thing that needs to be done is entering the POSIX paths of each one of your library files into the Shortcut’s List command. You can see how I have them set up in my example.

I will also post the AppleScript code itself with comments explaining what each command will do.

Choose and Load TV library

Here is the AppleScript code with its comments

on quitAndChooseLibrary(chosenLibrary)
	-- Check if the TV app is currently running
	set tvAppIsRunning to application "TV" is running
	
	-- Copy the chosen library path to the clipboard
	set the clipboard to chosenLibrary
	
	-- If the TV app is running, quit it
	if application "TV" is running then tell application "TV" to quit
	
	-- Wait for half a second to make sure the app is fully closed
	delay 0.5
	
	-- Hold down the Option key (this helps to bring up the library selection window)
	tell application "System Events" to key down option
	
	-- Brief pause to ensure the key press is registered
	delay 0.1
	
	-- Start the TV app again
	tell application "TV" to activate
	
	-- Wait shortly to let the app open
	delay 0.1
	
	-- Release the Option key
	tell application "System Events"
		key up option
		
		-- Keep checking until the "Choose Library" button shows up
		repeat while not (exists of button "Choose Library…" of window 1 of application process "TV")
			delay 0.1 -- Keep checking every 0.1 seconds
		end repeat
		
		-- Click the "Choose Library" button
		click button "Choose Library…" of window 1 of application process "TV"
		
		-- Wait a bit for the next window to open
		delay 1
		
		-- Paste the library path (Command + V)
		keystroke "v" using command down
		
		-- Short pause to ensure the paste worked
		delay 0.1
		
		-- Press Return to confirm
		keystroke return
		
		-- Wait a second to see if there's an "OK" button (like an error message)
		delay 1
		if UI element "OK" of window 1 of application process "TV" exists then
			click UI element "OK" of window 1 of application process "TV" -- Click "OK" if it exists
		end if
		
		-- Bring the TV app to the front
		set frontmost of application process "TV" to true
	end tell
	
	-- If the TV app wasn’t running originally, quit it to return things to normal
	if not tvAppIsRunning then
		tell application "TV" to quit
	else
		tell application "TV" to activate
	end if
	
end quitAndChooseLibrary
1 Like