Applescript results to txt file

78 Views Asked by At

I'm trying to write an apple script that gets the unwatched TVShows and puts the filename (preferably without the full path, just "whatever.m4v") in a text file.

Here is the code I have:

tell application "iTunes"
    set watchedEpisodes to tracks of playlist "TV Shows" whose unplayed is false and played count > 0
    set unwatchedEpisodes to tracks of playlist "TV Shows" whose unplayed is true
    if (count unwatchedEpisodes) > 0 then
        set trackNames to {}
        repeat with anEpisode in unwatchedEpisodes
            set end of trackNames to ((name of anEpisode) as text) & " of show " & show of anEpisode & " Filename: " & location of anEpisode
        end repeat

        set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

        set unwatchedTVShows to trackNames as text
        do shell script "echo " & unwatchedTVShows & " > /Volumes/Data/Media/Kodi/unwatchedTVShows.txt"

        #display dialog trackNames as text

        set AppleScript's text item delimiters to TID
    end if
end tell

I get the following error:

error "iTunes got an error: sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2

If I replace the "unwatchedTVShows" varible in the do shell statement like this:

do shell script "echo " & "test" & " > /Volumes/Data/Media/Kodi/unwatchedTVShows.txt"

it works as I expect (I have a filename called "unwatchedTVShows.txt" and it contains the word "test". I can use the "Display dialog trackNames as text" line that I currently have commented and it works fine. What am I missing?

1

There are 1 best solutions below

1
vadian On BEST ANSWER

The list of shows contains quotes and spaces. Just escape the entire list with quoted form of:

do shell script "echo " & quoted form of unwatchedTVShows & " > /Volumes/Data/Media/Kodi/unwatchedTVShows.txt"