Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a925c3bd95
Fetching contributors…

Cannot retrieve contributors at this time

file 72 lines (57 sloc) 2.311 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
"""
mp3players

Contains objects encapsulating methods to manage adding tracks
to audio players.
"""
import sys, os, os.path, re

def main():
    p = iTunesMac()
    p.addTrack(sys.argv[1])

class iTunesMac:
    OSASCRIPT = '/usr/bin/osascript'
    
    ADD_TRACK_SCRIPT = """
property arguments : "%s"

-- Derive a Mac-style path from a given POSIX path.
set track_path to arguments
set track_file to POSIX file track_path

-- Launch iTunes as hidden, if not already running.
tell application "System Events"
if not (exists process "iTunes") then
tell application "iTunes"
launch
set visible of front window to false
end tell
end if
end tell

tell application "iTunes"
-- Import the new track into main library.
set this_track to add track_file to playlist "library" of source "library"
set the genre of this_track to "Podcast"

-- Create the "Podcasts" playlist if needed, add new track to it.
if not (exists user playlist "Podcasts" of source "library") then
make new playlist of source "library" with properties {name:"Podcasts"}
end if
duplicate this_track to playlist "Podcasts" of source "library"
-- This might fail if no iPod is connected
try
-- Find an iPod
set the_iPod to some source whose kind is iPod

-- Copy the new track to the iPod main library
duplicate this_track to playlist 1 of the_iPod

-- Create the "Podcasts" playlist if needed, add new track to it.
if not (exists user playlist "Podcasts" of the_iPod) then
make new playlist of the_iPod with properties {name:"Podcasts"}
end if
duplicate this_track to playlist "Podcasts" of the_iPod
end try

end tell
"""
    
    def _executeAppleScript(self, script):
        """ Given the text of an AppleScript, attempt to execute it. """
        fi = os.popen(self.OSASCRIPT, "w")
        fi.write(script)
        fi.close()

    def addTrack(self, fn):
        abs_fn = os.path.abspath(fn)
        self._executeAppleScript(self.ADD_TRACK_SCRIPT % abs_fn)
    
if __name__=='__main__': main()
Something went wrong with that request. Please try again.