Skip to content

Commit

Permalink
Added iTunes artwork and track info scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Burns committed Dec 28, 2011
1 parent 2308d63 commit f3720ee
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions itunes_track_info.rb
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby
require 'yaml'
cache_file_path='/tmp/.itunes-current-track'

# Run the cache updater, so we don't have to have an item in Geek Tool for it
cache_updater = File.join(File.dirname(__FILE__), 'update_itunes_cache.rb')
`#{cache_updater}`

# This shouldn't happen but just in case
Kernel.exit 0 unless File.exists? cache_file_path
cache_file=File.open(cache_file_path, 'r') { |f| YAML.load(f.read) }


def right_align(str, amt=20)
"%#{amt}s" % str
end

def stars_from_rating rating
"★" * (rating.to_i/20)
end

puts cache_file[:name]
puts cache_file[:artist]
puts cache_file[:album]
13 changes: 13 additions & 0 deletions itunes_track_stars.rb
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
require 'yaml'
cache_file_path='/tmp/.itunes-current-track'
Kernel.exit 0 unless File.exists? cache_file_path
cache_file=File.open(cache_file_path, 'r') { |f| YAML.load(f.read) }


def stars_from_rating rating
"★" * rating
#"☆" * rating
end

puts stars_from_rating(cache_file[:stars])
Binary file added tools/iTunesArtwork.scpt
Binary file not shown.
31 changes: 31 additions & 0 deletions update_itunes_cache.rb
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
require 'yaml'
cache_file_path='/tmp/.itunes-current-track'
cache_file=File.open(cache_file_path, 'r') { |f| YAML.load(f.read) } if File.exists? cache_file_path

def update_album_artwork
artwork_script=File.join(File.dirname(__FILE__), 'tools/iTunesArtwork.scpt')
`osascript "#{artwork_script}"` if File.exists?(artwork_script)
end

def track_info(clause)
script = %Q{tell application "iTunes" to get #{clause} of current track}
#puts "Running: #{script}"
`osascript -e '#{script}'`.strip
end

# Get track info
track = {}
track[:id] = track_info 'id'

# If our IDs match, there is no reason to re-do all of this
Kernel.exit 0 if cache_file && cache_file[:id] == track[:id]

track[:name] = track_info 'name'
track[:artist] = track_info 'artist'
track[:album] = track_info 'album'
track[:rating] = track_info('rating').to_i
track[:stars] = (track[:rating]/20)
update_album_artwork

File.open(cache_file_path, 'w') { |f| f.write(track.to_yaml) }

0 comments on commit f3720ee

Please sign in to comment.