{"id":2269,"date":"2009-01-03T18:53:53","date_gmt":"2009-01-04T00:53:53","guid":{"rendered":"http:\/\/granades.com\/?p=2269"},"modified":"2009-01-03T18:53:53","modified_gmt":"2009-01-04T00:53:53","slug":"converting-itunes-playlists-to-m3u-playlists-on-a-mac","status":"publish","type":"post","link":"https:\/\/granades.com\/?p=2269","title":{"rendered":"Converting iTunes Playlists to M3U Playlists on a Mac"},"content":{"rendered":"<p>So I wasn&#8217;t content with just <a href=\"https:\/\/granades.com\/wordpress\/2009\/01\/02\/converting-itunes-playlists-to-m3u-playlists\/\">one way<\/a> of creating M3U playlists out of iTunes. I went and wrote another. There&#8217;s more than one way to do it, as I learned writing Perl programs.<\/p>\n<p>Pity I&#8217;m writing in Python.<\/p>\n<p>Anyway, this method only works on a Mac, because it uses Applescript. Actually, it uses <a href=\"http:\/\/appscript.sourceforge.net\/\">appscript<\/a>, an event bridge that lets you do what Applescript does, only in a real language like Python. It&#8217;s called <a href=\"https:\/\/granades.com\/wordpress\/downloads\/iTunesm3u.py\">iTunesm3u.py<\/a>, and requires that you <a href=\"http:\/\/appscript.sourceforge.net\/py-appscript\/install.html\">install py-appscript<\/a> first. To use it, have iTunes open. Select a playlist. Then run the script (or double-click the script if you turn it into an app with <a href=\"http:\/\/svn.pythonmac.org\/py2app\/py2app\/trunk\/doc\/index.html\">py2app<\/a>) and it&#8217;ll create an .m3u file in the same directory as the script.<\/p>\n<pre><code>\r\n#!\/usr\/bin\/env python\r\n\r\n# Using the appscript Python-to-applescript bridge, convert an iTunes\r\n# playlist into m3u format.\r\n#\r\n# Author: Stephen Granade\r\n# Date: 2 January 2009\r\n\r\nimport math, re, sys, os\r\nfrom appscript import *\r\n\r\n# Search-and-replace strings to adjust the mp3's locations if necessary.\r\nsandrStrs = { \"\/Volumes\": \"smb:\/\/sargent\" }\r\n\r\n# For more information about what classes etc. are available from iTunes\r\n# via appscript, see\r\n# http:\/\/appscript.sourceforge.net\/objc-appscript\/iTunes-objc\/index.html\r\n\r\n# Get ahold of iTunes\r\niTunes = app('iTunes')\r\n\r\n# The browser window's view is the currently-selected playlist\r\ntry:\r\n    playlist=iTunes.browser_windows[1].view()\r\nexcept CommandError, detail:\r\n    if detail.errornumber == -1731:\r\n        print \"There is no current playlist.\"\r\n        sys.stdout.write('\\a')\r\n        sys.stdout.flush()\r\n        sys.exit()\r\n    else:\r\n        raise CommandError, detail\r\n\r\n# Use the playlist's name as that of the m3u file\r\noutfn = os.path.abspath(__file__)\r\noutfn = os.path.join(os.path.dirname(outfn),playlist.name()+\".m3u\")\r\n\r\noutf = open(outfn, \"w\")\r\n\r\noutf.write(\"#EXTM3U\\n\")\r\n\r\nfor track in playlist.tracks():\r\n    # Skip any files whose locations aren't currently available\r\n    # If the file doesn't exist on disk, then trying to access the\r\n    # location will throw an AttributeError exception.\r\n    try:\r\n        fileloc = track.location().path\r\n        if not os.path.isfile(fileloc):\r\n            throw(AttributeError)\r\n        # Perform search-and-replace on the file location\r\n        for old, new in sandrStrs.iteritems():\r\n            fileloc = fileloc.replace(old, new)\r\n        durstr = \"%d\" % math.floor(track.duration())\r\n        outf.write(\"#EXTINF:\"+durstr+\",\"+track.name()+\"\\n\")\r\n        outf.write(fileloc+\"\\n\")\r\n    except AttributeError:\r\n        print \"\\\"\"+track.name()+\"\\\" isn't available on disk. Skipping.\"\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So I wasn&#8217;t content with just one way of creating M3U playlists out of iTunes. I went and wrote another. There&#8217;s more than one way to do it, as I learned writing Perl programs. Pity I&#8217;m writing in Python. Anyway, this method only works on a Mac, because it uses Applescript. Actually, it uses appscript, &hellip; <a href=\"https:\/\/granades.com\/?p=2269\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Converting iTunes Playlists to M3U Playlists on a Mac<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2269","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2269"}],"version-history":[{"count":3,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":2272,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2269\/revisions\/2272"}],"wp:attachment":[{"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}