Skip to content

Instantly share code, notes, and snippets.

@donatj
Created March 30, 2012 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donatj/2251739 to your computer and use it in GitHub Desktop.
Save donatj/2251739 to your computer and use it in GitHub Desktop.
Simple IRC iTunes bot
require 'cinch'
bot = Cinch::Bot.new do
configure do |c|
c.server = ""
c.channels = ["#"]
c.password = ""
c.nick = ""
end
helpers do
def currentTrack()
`osascript -e 'Tell Application "iTunes" to return artist of current track & " - " & name of current track'`
end
def trackById(id)
`osascript -e 'Tell Application "iTunes" to return artist of track id #{id} & " - " & name of track id #{id}'`
end
end
on :message, ":playpause" do |m|
m.reply 'Play/Paused!';
`osascript -e 'Tell Application "iTunes" to PlayPause'`
end
on :message, ":play" do |m|
`osascript -e 'Tell Application "iTunes" to Play'`
m.reply "Playing: " + currentTrack()
end
on :message, /:play ([0-9]+)/ do |m, tid|
`osascript -e 'Tell Application "iTunes" to play track id #{tid}'`
m.reply "Playing: " + currentTrack()
end
on :message, ":pause" do |m|
`osascript -e 'Tell Application "iTunes" to Pause'`
end
on :message, ":track" do |m|
m.reply "Now Playing: " + currentTrack()
end
on :message, ":nexttrack" do |m|
`osascript -e 'Tell Application "iTunes" to Next Track'`
m.reply "Playing: " + currentTrack()
end
on :message, ":prevtrack" do |m|
m.reply 'playpaused!';
`osascript -e 'Tell Application "iTunes" to Previous Track'`
m.reply "Playing: " + currentTrack()
end
on :message, /:findtrack ([a-z0-9 ]+)/i do |m, track|
count = 0
msg = ""
`osascript -e 'Tell Application "iTunes" to return tracks whose name contains "#{track}" or artist contains "#{track}"'`.scan(/track id ([0-9]+)/) do |result|
count += 1
if count < 10
msg += result[0] + ": " + trackById(result[0]).strip() + "\r"
end
end
if msg != ""
m.reply msg
else
m.reply "Could not find any results for: " + track
end
end
end
bot.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment