Class: Henchman::AppleScript
- Inherits:
-
Object
- Object
- Henchman::AppleScript
- Defined in:
- lib/applescript.rb
Instance Method Summary collapse
- #applescript_command(script) ⇒ Object
- #fetch?(buttons = []) ⇒ Boolean
- #get_active_app ⇒ Object
- #get_active_app_script ⇒ Object
- #get_album_tracks_of(selection) ⇒ Object
- #get_album_tracks_script(artist, album) ⇒ Object
- #get_playlist ⇒ Object
- #get_playlist_script ⇒ Object
- #get_playlist_tracks(playlist, skip = []) ⇒ Object
- #get_playlist_tracks_script(playlist, skip = [], size = 5) ⇒ Object
- #get_selection ⇒ Object
- #get_selection_script ⇒ Object
- #get_tracks_with_location ⇒ Object
- #get_tracks_with_location_script ⇒ Object
- #progress ⇒ Object
- #prompt_script(buttons = []) ⇒ Object
- #set_track_location(selection, local_file) ⇒ Object
- #setup(config) ⇒ Object
- #update_track_location_script(track_id, local_file) ⇒ Object
Instance Method Details
#applescript_command(script) ⇒ Object
187 188 189 |
# File 'lib/applescript.rb', line 187 def applescript_command(script) "osascript -e '#{script}' 2> /dev/null" end |
#fetch?(buttons = []) ⇒ Boolean
262 263 264 265 |
# File 'lib/applescript.rb', line 262 def fetch? = [] resp = %x(#{applescript_command(prompt_script )}).chomp resp.split(',').first.split(':').last.split('Download ').last rescue '' end |
#get_active_app ⇒ Object
267 268 269 |
# File 'lib/applescript.rb', line 267 def get_active_app %x(#{applescript_command(get_active_app_script)}).chomp end |
#get_active_app_script ⇒ Object
12 13 14 15 16 17 |
# File 'lib/applescript.rb', line 12 def get_active_app_script "tell application \"System Events\"\n"\ " set activeApp to name of first application process whose frontmost is true\n"\ " return activeApp\n"\ "end tell" end |
#get_album_tracks_of(selection) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/applescript.rb', line 244 def get_album_tracks_of selection artist = selection[:artist] album = selection[:album] tracks = Array.new tmp_tracks = %x(#{applescript_command(get_album_tracks_script artist, album)}).chomp tmp_tracks = tmp_tracks.split @delimiter_major tmp_tracks.each do |track| next if track.empty? tmp_track = track.split @delimiter next if tmp_track[3] == selection[:id] tracks.push( {:artist => tmp_track[0], :album => tmp_track[1], :track => tmp_track[2], :id => tmp_track[3]} ) end tracks end |
#get_album_tracks_script(artist, album) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/applescript.rb', line 75 def get_album_tracks_script artist, album "tell application \"iTunes\"\n"\ " try\n"\ " set album_tracks to "\ " (every track whose artist is \"#{artist.gsub(/'/){ %q('"'"') }}\" "\ " and album is \"#{album.gsub(/'/){ %q('"'"') }}\")\n"\ " set str to \"\"\n"\ " repeat with album_track in album_tracks\n"\ " set data_location to location of album_track as string\n"\ " if data_location is equal to \"missing value\" then\n"\ " set data_artist to artist of album_track as string\n"\ " set data_album to album of album_track as string\n"\ " set data_title to name of album_track as string\n"\ " set data_id to database ID of album_track as string\n"\ " set str to str & data_artist & \"#{@delimiter}\" "\ " & data_album & \"#{@delimiter}\" "\ " & data_title & \"#{@delimiter}\" "\ " & data_id & \"#{@delimiter_major}\"\n"\ " end if\n"\ " end repeat\n"\ " return str\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell" end |
#get_playlist ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/applescript.rb', line 219 def get_playlist playlist = %x(#{applescript_command(get_playlist_script)}).chomp playlist = playlist.split @delimiter if playlist[1] != 'none' false else playlist[0] end end |
#get_playlist_script ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/applescript.rb', line 102 def get_playlist_script "tell application \"iTunes\"\n"\ " try\n"\ " set selected_playlist to (get view of front window)\n"\ " set playlist_name to name of selected_playlist as string\n"\ " set playlist_special to special kind of selected_playlist as string\n"\ " set str to playlist_name & \"#{@delimiter}\" & playlist_special\n"\ " return str\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell"\ end |
#get_playlist_tracks(playlist, skip = []) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/applescript.rb', line 229 def get_playlist_tracks playlist, skip = [] tracks = Array.new tmp_tracks = %x(#{applescript_command(get_playlist_tracks_script playlist, skip)}).chomp tmp_tracks = tmp_tracks.force_encoding("UTF-8").split @delimiter_major tmp_tracks.each do |track| next if track.empty? tmp_track = track.split @delimiter tracks.push( {:artist => tmp_track[0], :album => tmp_track[1], :track => tmp_track[2], :id => tmp_track[3]} ) end tracks end |
#get_playlist_tracks_script(playlist, skip = [], size = 5) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/applescript.rb', line 136 def get_playlist_tracks_script playlist, skip = [], size = 5 "property counter : 0\n"\ "tell application \"iTunes\"\n"\ " try\n"\ " set playlist_tracks to every track in playlist \"#{playlist.gsub(/'/){ %q('"'"') }}\"\n"\ " set str to \"\"\n"\ " repeat with playlist_track in playlist_tracks\n"\ " set data_location to location of playlist_track as string\n"\ " set data_id to database ID of playlist_track as string\n"\ " if data_location is equal to \"missing value\" "\ " and data_id is not in [#{skip.map{|e| "\"#{e}\""}.join(',')}] then\n"\ " set data_artist to artist of playlist_track as string\n"\ " set data_album to album of playlist_track as string\n"\ " set data_title to name of playlist_track as string\n"\ " set str to str & data_artist & \"#{@delimiter}\""\ " & data_album & \"#{@delimiter}\""\ " & data_title & \"#{@delimiter}\""\ " & data_id & \"#{@delimiter_major}\"\n"\ " set counter to counter + 1\n"\ " if counter is equal to #{size} then exit repeat\n"\ " end if\n"\ " end repeat\n"\ " return str\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell" end |
#get_selection ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/applescript.rb', line 191 def get_selection selection = %x( #{applescript_command(get_selection_script)} ).chomp info = selection.split @delimiter track = Hash.new if !info.empty? track[:artist] = info[0] track[:album] = info[1] track[:track] = info[2] track[:id] = info[3] track[:path] = info[4] end track end |
#get_selection_script ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/applescript.rb', line 53 def get_selection_script "tell application \"iTunes\"\n"\ " try\n"\ " if class of selection as string is \"file track\" then\n"\ " set data_artist to artist of selection as string\n"\ " set data_album to album of selection as string\n"\ " set data_title to name of selection as string\n"\ " set data_id to database ID of selection as string\n"\ " set data_location to POSIX path of (location of selection as string)\n"\ " set str to data_artist & \"#{@delimiter}\" & "\ " data_album & \"#{@delimiter}\" & "\ " data_title & \"#{@delimiter}\" & "\ " data_id & \"#{@delimiter}\" & "\ " data_location\n"\ " return str\n"\ " end if\n"\ " on error\n"\ " return \"\"\n"\ " end try\n"\ "end tell" end |
#get_tracks_with_location ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/applescript.rb', line 205 def get_tracks_with_location tracks = Array.new tmp_tracks = %x(#{applescript_command(get_tracks_with_location_script)}).chomp tmp_tracks = tmp_tracks.split @delimiter_major tmp_tracks.each do |track| next if track.empty? tmp_track = track.split @delimiter tracks.push( {:id => tmp_track[0], :date => (tmp_track[1] != 'missing value') ? DateTime.parse(tmp_track[1]) : DateTime.new, :path => tmp_track[2]} ) end tracks end |
#get_tracks_with_location_script ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/applescript.rb', line 165 def get_tracks_with_location_script "tell application \"iTunes\"\n"\ " try\n"\ " set all_tracks to every track in playlist \"Music\"\n"\ " set str to \"\"\n"\ " repeat with cur_track in all_tracks\n"\ " set data_location to location of cur_track as string\n"\ " if data_location is not equal to \"missing value\" then\n"\ " set data_id to database ID of cur_track as string\n"\ " set data_date to played date of cur_track\n"\ " set str to str & data_id & \"#{@delimiter}\""\ " & data_date & \"#{@delimiter}\""\ " & (POSIX path of data_location as string) & \"#{@delimiter_major}\"\n"\ " end if\n"\ " end repeat\n"\ " return str\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell\n"\ end |
#progress ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/applescript.rb', line 116 def progress "set progress description to \"A simple progress indicator\"\n"\ "set progress additional description to \"Preparingā¦\"\n"\ "set progress total steps to -1\n"\ "\n"\ "delay 5\n"\ "\n"\ "set progress total steps to 100\n"\ "repeat with i from 1 to 100\n"\ " try\n"\ " set progress additional description to \"I am on step \" & i\n"\ " set progress completed steps to i\n"\ " delay 0.2\n"\ " on error thisErr\n"\ " display alert thisErr\n"\ " exit repeat\n"\ " end try\n"\ "end repeat" end |
#prompt_script(buttons = []) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/applescript.rb', line 19 def prompt_script = [] = .slice(0..1).map { |b| "Download #{b.gsub(/'/){ %q('"'"') }}" } "tell application \"iTunes\"\n"\ " display dialog \"\""\ " buttons {"\ " \"Cancel\""\ " #{(.length > 0) ? ",\"#{.first}\"" : ''}"\ " #{(.length > 1) ? ",\"#{.last}\"" : ''}"\ " }"\ " with title \"Henchman š\""\ " cancel button \"Cancel\""\ " default button \"#{(.length > 0) ? .last : 'Cancel'}\""\ " giving up after 60"\ " with icon note\n"\ "end tell" end |
#set_track_location(selection, local_file) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/applescript.rb', line 271 def set_track_location selection, local_file puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\ "Updating location of #{selection.reject{|k,v| k == :path || k == :id}.values.join(':')} to #{local_file}" ret = %x(#{applescript_command(update_track_location_script selection[:id], local_file)}).chomp if ret.empty? || ret == '0' false else true end end |
#setup(config) ⇒ Object
7 8 9 10 |
# File 'lib/applescript.rb', line 7 def setup config @delimiter = config[:delimiter] @delimiter_major = config[:delimiter_major] end |
#update_track_location_script(track_id, local_file) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/applescript.rb', line 36 def update_track_location_script track_id, local_file "tell application \"iTunes\"\n"\ " try\n"\ " set data_tracks to (every track whose database ID is \"#{track_id}\")\n"\ " if (count of data_tracks) is 1 then\n"\ " set location of (item 1 of data_tracks) to "\ " (POSIX file \"#{local_file.gsub(/'/){ %q('"'"') }}\")\n"\ " return 1\n"\ " else\n"\ " return 0\n"\ " end if\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell" end |