Created
November 20, 2011 01:24
-
-
Save midore/1379657 to your computer and use it in GitHub Desktop.
practice macruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework "ScriptingBridge" | |
module MyBridge | |
class ITunes | |
attr_reader :lib, :lib_music | |
def initialize | |
@i = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes") | |
@lib = @i.sources[0].libraryPlaylists[0] | |
@lib_music = @playlist = @i.sources[0].userPlaylists[0] | |
end | |
def quit; @i.quit; end | |
def run; @i.run; end | |
def act; @i.activate; end | |
def playlist(str=nil) | |
return @playlist unless str | |
@playlist = @i.sources[0].userPlaylists.find{|x| x.name == str} | |
@playlist = @lib_music unless @playlist | |
return @playlist | |
end | |
def songs | |
return @playlist.tracks | |
end | |
def files | |
return @playlist.fileTracks | |
end | |
def play_playlist | |
@playlist.playOnce(false) if @playlist | |
end | |
def play_song(song) | |
song.playOnce(true) if song | |
end | |
def checkfile(track) | |
# exchange: iTunesTrack to iTunesFileTrack | |
ft = track.get | |
return false if ft.location.nil? | |
return true | |
end | |
def list_exist(str) | |
a = @i.sources[0].userPlaylists.find{|x| x.name == str} | |
return false unless a | |
return true if a | |
end | |
def makelist(str) | |
return playlist(str) if list_exist(str) | |
# I dont know. | |
# @i.makePlaylistWithProperties{name:"#{str}"} #=>(NoMethodError) | |
a = "\'tell application \"iTunes\" to make playlist with properties {name:\"#{str}\"}\'" | |
system("osascript -e #{a} -e 'return'") | |
playlist(str) | |
end | |
def search(target=nil, w, opt) | |
# target: only track.name | |
wx = Regexp.new(w, opt) | |
a = @lib_music.tracks.find_all{|x| wx.match(x.name)} | |
return nil if a.empty? | |
return a | |
end | |
def sort(target=nil, ary) | |
# target: only track.name | |
ary.sort_by{|x| x.name} | |
end | |
def trackmovetolist(ary, list) | |
a = songs.map{|x| x.databaseID} | |
ary.each{|y| y.duplicateTo(list) unless a.find_index(y.databaseID)} | |
end | |
end | |
end | |
i = MyBridge::ITunes.new() | |
i.run | |
#i.act | |
#p i.playlist("トップレート") | |
#p i.songs[0..11] | |
p ary = i.search("I", false) | |
exit unless ary | |
p list = i.makelist("abc") | |
p i.playlist.name | |
i.trackmovetolist(ary, list) | |
i.play_playlist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment