Class: ITunes
- Inherits:
-
Object
show all
- Defined in:
- lib/itunes_command.rb
Constant Summary
collapse
- QUEUE_PLAYLIST =
'itunes-command'
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ ITunes
Returns a new instance of ITunes.
71
72
73
|
# File 'lib/itunes_command.rb', line 71
def initialize
@app = OSX::SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(message, *args) ⇒ Object
Delegate all other methods to the SBAppliation object for iTunes
76
77
78
|
# File 'lib/itunes_command.rb', line 76
def method_missing(message, *args)
@app.send(message, *args)
end
|
Instance Method Details
#add_track_to_playlist(track, playlist) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/itunes_command.rb', line 124
def add_track_to_playlist(track, playlist)
if playlist.is_a?(String)
playlist = playlist_by_name(playlist)
end
track = track.duplicateTo(playlist)
end
|
#add_tracks_to_playlist(tracks, playlist, credit = nil) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/itunes_command.rb', line 136
def add_tracks_to_playlist(tracks, playlist, credit=nil)
if playlist.is_a?(String)
playlist = playlist_by_name(playlist)
end
if tracks.is_a?(Array) tracks = OSX::NSArray.arrayWithArray(tracks)
end
tracks.makeObjectsPerformSelector_withObject("setEnabled:", 1)
if credit
tracks.makeObjectsPerformSelector_withObject("setComment:", credit)
end
tracks.makeObjectsPerformSelector_withObject("duplicateTo:", playlist)
end
|
#artists(playlist = library) ⇒ Object
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/itunes_command.rb', line 96
def artists(playlist=library)
return {} if playlist.tracks.empty?
artists = playlist.tracks.arrayByApplyingSelector("artist").select {|x| x.to_s =~ /\w/}
artist = Hash.new(0)
artists.each do |name|
artist[name.to_s] += 1
end
artist
end
|
#clear_queue ⇒ Object
185
186
187
|
# File 'lib/itunes_command.rb', line 185
def clear_queue
queue.tracks.removeAllObjects
end
|
#create_playlist(name) ⇒ Object
158
159
160
161
162
163
164
|
# File 'lib/itunes_command.rb', line 158
def create_playlist(name)
return if playlist_by_name(name)
props = {:name => name}
playlist = @app.classForScriptingClass("playlist").alloc.initWithProperties(props)
playlists.insertObject_atIndex(playlist, 0)
playlist
end
|
#find_track(query) ⇒ Object
Pass in a string to get matching tracks. Pass in an integer to search by databaseID
109
110
111
112
113
114
115
116
117
|
# File 'lib/itunes_command.rb', line 109
def find_track(query)
if query.is_a?(String)
library.searchFor_only(query, nil)
elsif query.is_a?(Integer) predicate = OSX::NSPredicate.predicateWithFormat("databaseID == #{query}")
library.tracks.filteredArrayUsingPredicate(predicate).first
end
end
|
#find_tracks(track_ids) ⇒ Object
119
120
121
122
|
# File 'lib/itunes_command.rb', line 119
def find_tracks(track_ids)
predicate = OSX::NSPredicate.predicateWithFormat("databaseID IN {%s}" % track_ids.join(','))
library.tracks.filteredArrayUsingPredicate(predicate)
end
|
#library ⇒ Object
92
93
94
|
# File 'lib/itunes_command.rb', line 92
def library
playlists.first
end
|
#party_shuffle ⇒ Object
80
81
82
|
# File 'lib/itunes_command.rb', line 80
def party_shuffle
playlists.detect {|x| x.name == "Party Shuffle"}
end
|
#playlist_by_name(name) ⇒ Object
88
89
90
|
# File 'lib/itunes_command.rb', line 88
def playlist_by_name(name)
playlists.detect {|p| p.name == name}
end
|
#playlists ⇒ Object
84
85
86
|
# File 'lib/itunes_command.rb', line 84
def playlists
@app.sources.first.playlists
end
|
#queue ⇒ Object
This is the playlist that itunes-rails uses
#queue_track(track) ⇒ Object
177
178
179
|
# File 'lib/itunes_command.rb', line 177
def queue_track(track)
add_track_to_playlist(track, queue)
end
|
#queue_tracks(tracks, credit = nil) ⇒ Object
181
182
183
|
# File 'lib/itunes_command.rb', line 181
def queue_tracks(tracks,credit=nil)
add_tracks_to_playlist(tracks, queue, credit)
end
|
#remove_track_from_playlist(track, playlist) ⇒ Object
152
153
154
155
156
|
# File 'lib/itunes_command.rb', line 152
def remove_track_from_playlist(track, playlist)
return
track.delete
end
|
#select_queue_playlist ⇒ Object
makes sure the queue playlist is selected. importance for pause/play, skip, etc.
168
169
170
|
# File 'lib/itunes_command.rb', line 168
def select_queue_playlist
browserWindows.first.view = queue
end
|