Search code examples
bashapplescriptitunes

Tell iTunes to open a .m3u playlist file


I'm trying to open a .m3u playlist file in iTune using applescript. I can open an iTune playlist file using:

#!/usr/bin/osascript
tell application "iTunes"
    play playlist "my playlist"
end tell

How can I pass a full path that points to a .m3u file present in any location? Something like:

#!/usr/bin/osascript
tell application "iTunes"
    play playlist "/path/to/folder/file.m3u"
end tell

Thanks.


Solution

  • Since you point out in the comments to your question that you can open an m3u playlist in iTunes and it creates an iTunes playlist, then my suggestion would be to do it that way. First issue the "open" command and after the playlist is created in iTunes then issue the "play playlist" command.

    Assuming the name of the playlist created in iTunes is the file name of the m3u file then this might work. Note also that applescript uses file specifiers, not posix paths to a file, so we convert your posix path to a file specifier by using the "POSIX file" command.

    I haven't tried this but it's my best guess as to what might work. Good luck.

    set posixPath to "/path/to/folder/fileName.m3u"
    set fileSpecifier to POSIX file posixPath
    tell application "iTunes"
        open fileSpecifier
        delay 1 -- delay however many seconds needed to allow the playlist to be created
        play playlist "fileName"
    end tell
    

    EDIT: The error could come from when iTunes converts your m3u file into an iTunes playlist. There might be something in the m3u file causing the error. I don't know what to suggest for that.

    However, the error might also come from the POSIX file command. That's touchy sometimes. So one thing you might try to address that possibility is to coerce that command to text and then create the specifier using the word "file" before the string path. So try this. If the error is from the POSIX file command then this should fix it.

    set posixPath to "/path/to/folder/fileName.m3u"
    set fileSpecifier to (POSIX file posixPath) as text
    tell application "iTunes"
        open file fileSpecifier
        delay 1 -- delay however many seconds needed to allow the playlist to be created
        play playlist "fileName"
    end tell