Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add `current_track`, `player_state`

  • Loading branch information...
commit c907f70bc13bc39aafe03527c439c4a8fe35654a 1 parent e5a9273
Mislav Marohnić authored

Showing 2 changed files with 97 additions and 68 deletions. Show diff stats Hide diff stats

  1. +15 3 README.md
  2. +82 65 itunes.rb
18 README.md
Source Rendered
@@ -4,15 +4,27 @@ Prerequisites: [MacRuby](http://www.macruby.org/)
4 4
5 5 Example usage:
6 6
7   - favorites = ITunesManager.find_or_create_playlist 'Favorites'
  7 + ITunesManager.app.run
  8 +
  9 + # start playing at the beginning
  10 + ITunesManager.music.playOnce(false)
8 11
9   - track = ITunesManager.music.fileTracks.first
  12 + ITunesManager.player_state #=> :playing
  13 +
  14 + ITunesManager.app.playpause
  15 + ITunesManager.player_state #=> :paused
10 16
11   - # see iTunes.h under "@interface iTunesTrack" for more properties
  17 + track = ITunesManager.current_track
  18 +
  19 + # for more properties, see iTunes.h under "@interface iTunesTrack"
12 20 track.name
13 21 track.artist
14 22 track.album
  23 +
  24 + # playlist management
  25 + favorites = ITunesManager.find_or_create_playlist 'Favorites'
15 26
  27 + track = ITunesManager.music.fileTracks.first
16 28 favorites << track
17 29
18 30 favorites.add ITunesManager.music.search('daft punk')
147 itunes.rb
@@ -2,11 +2,89 @@
2 2 framework 'Foundation'
3 3 framework 'ScriptingBridge'
4 4
5   -SBApplication.applicationWithBundleIdentifier('com.apple.itunes')
  5 +module ITunesManager
  6 + def self.app
  7 + @app ||= SBApplication.applicationWithBundleIdentifier('com.apple.itunes').tap do
  8 + # $ sdef /Applications/iTunes.app | sdp -fh --basename iTunes
  9 + # $ gen_bridge_metadata -c '-I.' iTunes.h > iTunes.bridgesupport
  10 + load_bridge_support_file 'iTunes.bridgesupport'
  11 + end
  12 + end
6 13
7   -# $ sdef /Applications/iTunes.app | sdp -fh --basename iTunes
8   -# $ gen_bridge_metadata -c '-I.' iTunes.h > iTunes.bridgesupport
9   -load_bridge_support_file 'iTunes.bridgesupport'
  14 + # trigger loading bridge support file
  15 + self.app
  16 +
  17 + def self.library
  18 + @library ||= app.sources['Library']
  19 + end
  20 +
  21 + def self.music
  22 + library.userPlaylists['Music']
  23 + end
  24 +
  25 + PlayerStates = {
  26 + ITunesEPlSStopped => :stopped,
  27 + ITunesEPlSPlaying => :playing,
  28 + ITunesEPlSPaused => :paused,
  29 + ITunesEPlSFastForwarding => :fast_forwarding,
  30 + ITunesEPlSRewinding => :rewinding
  31 + }
  32 +
  33 + def self.player_state
  34 + PlayerStates[app.playerState]
  35 + end
  36 +
  37 + def self.current_track
  38 + ensure_found app.currentTrack
  39 + end
  40 +
  41 + def self.find_folder(name)
  42 + ensure_found library.playlists[name]
  43 + end
  44 +
  45 + def self.find_or_create_folder(name)
  46 + folder = find_folder name
  47 +
  48 + if folder
  49 + raise %(iTunes item "#{folder.name}" exists but is not a folder) unless folder.folder?
  50 + folder
  51 + else
  52 + folder = ITunesFolderPlaylist.alloc.initWithProperties(name: name)
  53 + library.playlists.addObject(folder)
  54 + library.playlists[name]
  55 + end
  56 + end
  57 +
  58 + def self.find_playlist(name)
  59 + ensure_found library.userPlaylists[name]
  60 + end
  61 +
  62 + def self.find_or_create_playlist(name, folder = nil)
  63 + playlist = find_playlist name
  64 +
  65 + unless playlist
  66 + playlist = ITunesUserPlaylist.alloc.initWithProperties(name: name)
  67 + library.userPlaylists.addObject(playlist)
  68 + playlist = library.userPlaylists[name]
  69 + end
  70 +
  71 + if folder
  72 + folder = find_or_create_folder(folder) if String === folder
  73 + playlist.moveTo(folder) unless folder == playlist.parent
  74 + end
  75 +
  76 + playlist
  77 + end
  78 +
  79 + class << self
  80 + private
  81 + def ensure_found(item)
  82 + # work around the fact that iTunesItem lookups will often
  83 + # return an object regardless of whether it actually exists
  84 + item.id == 0 ? nil : item
  85 + end
  86 + end
  87 +end
10 88
11 89 class SBElementArray
12 90 def [](value)
@@ -69,64 +147,3 @@ def inspect
69 147 end
70 148 end
71 149 end
72   -
73   -module ITunesManager
74   - def self.app
75   - SBApplication.applicationWithBundleIdentifier('com.apple.itunes')
76   - end
77   -
78   - def self.library
79   - @library ||= app.sources['Library']
80   - end
81   -
82   - def self.music
83   - library.userPlaylists['Music']
84   - end
85   -
86   - def self.find_folder(name)
87   - ensure_found library.playlists[name]
88   - end
89   -
90   - def self.find_or_create_folder(name)
91   - folder = find_folder name
92   -
93   - if folder
94   - raise %(iTunes item "#{folder.name}" exists but is not a folder) unless folder.folder?
95   - folder
96   - else
97   - folder = ITunesFolderPlaylist.alloc.initWithProperties(name: name)
98   - library.playlists.addObject(folder)
99   - library.playlists[name]
100   - end
101   - end
102   -
103   - def self.find_playlist(name)
104   - ensure_found library.userPlaylists[name]
105   - end
106   -
107   - def self.find_or_create_playlist(name, folder = nil)
108   - playlist = find_playlist name
109   -
110   - unless playlist
111   - playlist = ITunesUserPlaylist.alloc.initWithProperties(name: name)
112   - library.userPlaylists.addObject(playlist)
113   - playlist = library.userPlaylists[name]
114   - end
115   -
116   - if folder
117   - folder = find_or_create_folder(folder) if String === folder
118   - playlist.moveTo(folder) unless folder == playlist.parent
119   - end
120   -
121   - playlist
122   - end
123   -
124   - class << self
125   - private
126   - def ensure_found(playlist)
127   - # work around the fact that playlist lookup will return an object
128   - # regardless of whether it actually exists
129   - playlist.specialKind == 0 ? nil : playlist
130   - end
131   - end
132   -end

0 comments on commit c907f70

Please sign in to comment.
Something went wrong with that request. Please try again.