Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add `current_track`, `player_state`

  • Loading branch information...
commit c907f70bc13bc39aafe03527c439c4a8fe35654a 1 parent e5a9273
@mislav authored
Showing with 97 additions and 68 deletions.
  1. +15 −3 README.md
  2. +82 −65 itunes.rb
View
18 README.md
@@ -4,15 +4,27 @@ Prerequisites: [MacRuby](http://www.macruby.org/)
Example usage:
- favorites = ITunesManager.find_or_create_playlist 'Favorites'
+ ITunesManager.app.run
+
+ # start playing at the beginning
+ ITunesManager.music.playOnce(false)
- track = ITunesManager.music.fileTracks.first
+ ITunesManager.player_state #=> :playing
+
+ ITunesManager.app.playpause
+ ITunesManager.player_state #=> :paused
- # see iTunes.h under "@interface iTunesTrack" for more properties
+ track = ITunesManager.current_track
+
+ # for more properties, see iTunes.h under "@interface iTunesTrack"
track.name
track.artist
track.album
+
+ # playlist management
+ favorites = ITunesManager.find_or_create_playlist 'Favorites'
+ track = ITunesManager.music.fileTracks.first
favorites << track
favorites.add ITunesManager.music.search('daft punk')
View
147 itunes.rb
@@ -2,11 +2,89 @@
framework 'Foundation'
framework 'ScriptingBridge'
-SBApplication.applicationWithBundleIdentifier('com.apple.itunes')
+module ITunesManager
+ def self.app
+ @app ||= SBApplication.applicationWithBundleIdentifier('com.apple.itunes').tap do
+ # $ sdef /Applications/iTunes.app | sdp -fh --basename iTunes
+ # $ gen_bridge_metadata -c '-I.' iTunes.h > iTunes.bridgesupport
+ load_bridge_support_file 'iTunes.bridgesupport'
+ end
+ end
-# $ sdef /Applications/iTunes.app | sdp -fh --basename iTunes
-# $ gen_bridge_metadata -c '-I.' iTunes.h > iTunes.bridgesupport
-load_bridge_support_file 'iTunes.bridgesupport'
+ # trigger loading bridge support file
+ self.app
+
+ def self.library
+ @library ||= app.sources['Library']
+ end
+
+ def self.music
+ library.userPlaylists['Music']
+ end
+
+ PlayerStates = {
+ ITunesEPlSStopped => :stopped,
+ ITunesEPlSPlaying => :playing,
+ ITunesEPlSPaused => :paused,
+ ITunesEPlSFastForwarding => :fast_forwarding,
+ ITunesEPlSRewinding => :rewinding
+ }
+
+ def self.player_state
+ PlayerStates[app.playerState]
+ end
+
+ def self.current_track
+ ensure_found app.currentTrack
+ end
+
+ def self.find_folder(name)
+ ensure_found library.playlists[name]
+ end
+
+ def self.find_or_create_folder(name)
+ folder = find_folder name
+
+ if folder
+ raise %(iTunes item "#{folder.name}" exists but is not a folder) unless folder.folder?
+ folder
+ else
+ folder = ITunesFolderPlaylist.alloc.initWithProperties(name: name)
+ library.playlists.addObject(folder)
+ library.playlists[name]
+ end
+ end
+
+ def self.find_playlist(name)
+ ensure_found library.userPlaylists[name]
+ end
+
+ def self.find_or_create_playlist(name, folder = nil)
+ playlist = find_playlist name
+
+ unless playlist
+ playlist = ITunesUserPlaylist.alloc.initWithProperties(name: name)
+ library.userPlaylists.addObject(playlist)
+ playlist = library.userPlaylists[name]
+ end
+
+ if folder
+ folder = find_or_create_folder(folder) if String === folder
+ playlist.moveTo(folder) unless folder == playlist.parent
+ end
+
+ playlist
+ end
+
+ class << self
+ private
+ def ensure_found(item)
+ # work around the fact that iTunesItem lookups will often
+ # return an object regardless of whether it actually exists
+ item.id == 0 ? nil : item
+ end
+ end
+end
class SBElementArray
def [](value)
@@ -69,64 +147,3 @@ def inspect
end
end
end
-
-module ITunesManager
- def self.app
- SBApplication.applicationWithBundleIdentifier('com.apple.itunes')
- end
-
- def self.library
- @library ||= app.sources['Library']
- end
-
- def self.music
- library.userPlaylists['Music']
- end
-
- def self.find_folder(name)
- ensure_found library.playlists[name]
- end
-
- def self.find_or_create_folder(name)
- folder = find_folder name
-
- if folder
- raise %(iTunes item "#{folder.name}" exists but is not a folder) unless folder.folder?
- folder
- else
- folder = ITunesFolderPlaylist.alloc.initWithProperties(name: name)
- library.playlists.addObject(folder)
- library.playlists[name]
- end
- end
-
- def self.find_playlist(name)
- ensure_found library.userPlaylists[name]
- end
-
- def self.find_or_create_playlist(name, folder = nil)
- playlist = find_playlist name
-
- unless playlist
- playlist = ITunesUserPlaylist.alloc.initWithProperties(name: name)
- library.userPlaylists.addObject(playlist)
- playlist = library.userPlaylists[name]
- end
-
- if folder
- folder = find_or_create_folder(folder) if String === folder
- playlist.moveTo(folder) unless folder == playlist.parent
- end
-
- playlist
- end
-
- class << self
- private
- def ensure_found(playlist)
- # work around the fact that playlist lookup will return an object
- # regardless of whether it actually exists
- playlist.specialKind == 0 ? nil : playlist
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.