AppleScript to export details from Apple TV library (2021)

4 days ago 2

Here is a small script that can export details from your TV library in macOS 15 Catalina or later and was prompted by this thread: Export library list with file location from tv app - Apple Community. This is in lieu of an XML export option and in particular captures location which is not included when copying items from the screen and pasting elsewhere. It should be relatively easy to add additional details to the output if required.

Copy the text below the line and paste it into the Script Editor. which you can find in /Applications/Utilities. Save as Export TV Info in say ~/Documents/Scripts then run it using the Play button in the Script Editor. The script will save a text file on your desktop called TV Details.txt.

__________________________________________________________________

-- Export TV Info - V1.01 - © Steve MacGuire - 2021-04-05

tell application "TV"

set mySelection to tracks

set d to (date string of (current date))

set t to (time string of (current date))

set output to "<#>TV Details - Exported " & d & " " & t & return & return

repeat with aVideo in mySelection

set theName to name of aVideo

if exists location of aVideo then

set theLoc to location of aVideo

if theLoc is not missing value then

set thePath to POSIX path of theLoc

else

set thePath to "<Missing Location>"

end if

else

set thePath to "iCloud"

end if

set theName to name of aVideo

set theShow to show of aVideo

if theShow is not "" then

set seas to season number of aVideo

set epNum to episode number of aVideo

set epID to episode ID of aVideo

set seas to season number of aVideo

set epNum to episode number of aVideo

set output to output & "<Show>" & theShow & return

set output to output & "<Season>" & seas & return

set output to output & "<Episode #>" & epNum & return

set output to output & "<Episode ID>" & epID & return

end if

set output to output & "<Name>" & theName & return

set output to output & "<Location>" & thePath & return

set output to output & return

end repeat

my make_the_file(output)

display dialog "Output file created on the desktop" with title "Export TV Info"

end tell

-- ================================

-- create the text file, will overwrite unless file in use

-- ================================

to make_the_file(newContents)

tell application "Finder"

set file_name to (home as string) & "Desktop:TV Details.txt"

if (exists file file_name) then

do shell script "rm " & quoted form of POSIX path of file_name

end if

end tell

try

set fileRefr to (open for access file_name with write permission)

write newContents to fileRefr

close access fileRefr

on error errx number errNum from badObj

try

close access fileRefr

end try

if (errNum is equal to -48) then

do shell script "rm " & quoted form of POSIX path of file_name

my make_the_file(newContents)

else

display dialog "There has been an error creating the file:" & nlurn & nlurn & (badObj as string) & errx & nlurn & "error number: " & errNum buttons {"Cancel"}

end if

end try

end make_the_file

Read Entire Article