Class: Henchman::AppleScript
- Inherits:
-
Object
- Object
- Henchman::AppleScript
- Defined in:
- lib/applescript.rb
Instance Method Summary collapse
- #applescript_command(script) ⇒ Object
- #fetch? ⇒ Boolean
- #get_active_app ⇒ Object
- #get_active_app_script ⇒ Object
- #get_album_tracks_of(selection) ⇒ Object
- #get_album_tracks_script(artist, album) ⇒ Object
- #get_selection(ret) ⇒ Object
- #get_selection_script ⇒ Object
- #prompt_script ⇒ Object
- #set_track_location(selection, local_file) ⇒ Object
- #setup(config) ⇒ Object
- #update_track_location_script(selection, local_file) ⇒ Object
Instance Method Details
#applescript_command(script) ⇒ Object
82 83 84 |
# File 'lib/applescript.rb', line 82 def applescript_command(script) "osascript -e '#{script}' 2> /dev/null" end |
#fetch? ⇒ Boolean
110 111 112 |
# File 'lib/applescript.rb', line 110 def fetch? (%x(#{applescript_command(prompt_script)}).chomp == "button returned:OK") ? true : false end |
#get_active_app ⇒ Object
114 115 116 |
# File 'lib/applescript.rb', line 114 def get_active_app %x(#{applescript_command(get_active_app_script)}).chomp end |
#get_active_app_script ⇒ Object
9 10 11 12 13 14 |
# File 'lib/applescript.rb', line 9 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
101 102 103 104 105 106 107 108 |
# File 'lib/applescript.rb', line 101 def get_album_tracks_of selection artist = selection[:artist] album = selection[:album] tracks = %x(#{applescript_command(get_album_tracks_script artist, album)}).chomp tracks = tracks.split(@delimiter) tracks.delete selection[:track] tracks end |
#get_album_tracks_script(artist, album) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/applescript.rb', line 62 def get_album_tracks_script artist, album "tell application \"iTunes\"\n"\ " try\n"\ " set data_tracks to "\ " (every track whose artist is \"#{artist}\" "\ " and album is \"#{album}\")\n"\ " set data_tracks_str to \"\"\n"\ " repeat with data_track in data_tracks\n"\ " set data_tracks_str to data_tracks_str & (name of data_track) as string\n"\ " if (name of (last item of data_tracks)) is not (name of data_track) then\n"\ " set data_tracks_str to data_tracks_str & \"#{@delimiter}\"\n"\ " end if\n"\ " end repeat\n"\ " return data_tracks_str\n"\ " on error\n"\ " return 0\n"\ " end try\n"\ "end tell" end |
#get_selection(ret) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/applescript.rb', line 86 def get_selection ret selection = %x( #{applescript_command(get_selection_script)} ).chomp info = selection.split(@delimiter) if info.empty? false elsif info[3] == "/missing value" || !File.exists?(info[3]) ret[:artist] = info[0] ret[:album] = info[1] ret[:track] = info[2] true else false end end |
#get_selection_script ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/applescript.rb', line 42 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_location to POSIX path of (location of selection as string)\n"\ " set str to data_artist & \"#{@delimiter}\" & "\ " data_album & \"#{@delimiter}\" & "\ " data_title & \"#{@delimiter}\" & "\ " data_location\n"\ " return str\n"\ " end if\n"\ " on error\n"\ " return \"\"\n"\ " end try\n"\ "end tell" end |
#prompt_script ⇒ Object
16 17 18 19 20 |
# File 'lib/applescript.rb', line 16 def prompt_script "tell application \"iTunes\"\n"\ " display dialog \"Fetch?\"\n"\ "end tell" end |
#set_track_location(selection, local_file) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/applescript.rb', line 118 def set_track_location selection, local_file ret = %x(#{applescript_command(update_track_location_script selection, local_file)}).chomp if ret.empty? || ret == '0' puts "Could not update location of #{selection.values.join(':')} to #{local_file}" false else true end end |
#setup(config) ⇒ Object
5 6 7 |
# File 'lib/applescript.rb', line 5 def setup config @delimiter = config[:delimiter] end |
#update_track_location_script(selection, local_file) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/applescript.rb', line 22 def update_track_location_script selection, local_file "tell application \"iTunes\"\n"\ " try\n"\ " set data_tracks to "\ " (every track whose artist is \"#{selection[:artist].gsub(/'/){ %q('"'"') }}\" and "\ " album is \"#{selection[:album].gsub(/'/){ %q('"'"') }}\" and "\ " name is \"#{selection[:track].gsub(/'/){ %q('"'"') }}\")\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 |