Class: Henchman::AppleScript

Inherits:
Object
  • Object
show all
Defined in:
lib/applescript.rb

Instance Method Summary collapse

Instance Method Details

#applescript_command(script) ⇒ Object



193
194
195
# File 'lib/applescript.rb', line 193

def applescript_command(script)
  "osascript -e '#{script}' 2> /dev/null"
end

#fetch?(buttons = []) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
271
# File 'lib/applescript.rb', line 268

def fetch? buttons = []
  resp = %x(#{applescript_command(prompt_script buttons)}).chomp
  resp.split(',').first.split(':').last.split('Download ').last rescue ''
end

#get_active_appObject



273
274
275
# File 'lib/applescript.rb', line 273

def get_active_app
  %x(#{applescript_command(get_active_app_script)}).chomp
end

#get_active_app_scriptObject



11
12
13
14
15
16
# File 'lib/applescript.rb', line 11

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



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/applescript.rb', line 250

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*2
  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



74
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
# File 'lib/applescript.rb', line 74

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*2}\"\n"\
  "      end if\n"\
  "    end repeat\n"\
  "    return str\n"\
  "  on error\n"\
  "    return 0\n"\
  "  end try\n"\
  "end tell"
end

#get_playlistObject



225
226
227
228
229
230
231
232
233
# File 'lib/applescript.rb', line 225

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_scriptObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/applescript.rb', line 101

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) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/applescript.rb', line 235

def get_playlist_tracks playlist
  tracks = Array.new
  tmp_tracks = %x(#{applescript_command(get_playlist_tracks_script playlist)}).chomp
  tmp_tracks = tmp_tracks.split @delimiter*2
  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) ⇒ Object

def get_playlist_tracks_script playlist, offset, size

"tell application \"iTunes\"\n"\
"  try\n"\
"    set playlist_tracks to every track in playlist \"#{playlist}\"\n"\
"    if (#{offset} + 1) * #{size} is less than (count of playlist_tracks) then\n"\
"      set max to (#{offset} + 1) * #{size}\n"\
"    else\n"\
"      set max to count of playlist_tracks\n"\
"    end if\n"\
"    set min to (#{offset} * #{size}) + 1\n"\
"    display dialog min\n"\
"    display dialog max\n"\
"    set str to \"\"\n"\
"    repeat with n from min to max\n"\
"      set data_track to item n of playlist_tracks\n"\
"      set data_artist to artist of data_track as string\n"\
"      set data_album to album of data_track as string\n"\
"      set data_title to name of data_track as string\n"\
"      set data_id to database ID of data_track as string\n"\
"      set data_location to POSIX path of (location of data_track as string)\n"\
"      set str to str & data_artist & \"#{@delimiter}\" "\
"                     & data_album & \"#{@delimiter}\" "\
"                     & data_title & \"#{@delimiter}\" "\
"                     & data_id & \"#{@delimiter}\" "\
"                     & data_location & \"#{@delimiter*2}\n"\
"    end repeat\n"\
"  on error\n"\
"    return 0\n"\
"  end try\n"\
"end tell"

end



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/applescript.rb', line 146

def get_playlist_tracks_script playlist
  "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"\
  "      if data_location is equal to \"missing value\" 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 data_id to database ID of playlist_track as string\n"\
  "        set str to str & data_artist & \"#{@delimiter}\""\
  "                       & data_album  & \"#{@delimiter}\""\
  "                       & data_title  & \"#{@delimiter}\""\
  "                       & data_id     & \"#{@delimiter*2}\"\n"\
  "      end if\n"\
  "    end repeat\n"\
  "    return str\n"\
  "  on error\n"\
  "    return 0\n"\
  "  end try\n"\
  "end tell"
end

#get_selectionObject



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/applescript.rb', line 197

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_scriptObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/applescript.rb', line 52

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_locationObject



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/applescript.rb', line 211

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*2
  tmp_tracks.each do |track|
    next if track.empty?
    tmp_track = track.split @delimiter
    tracks.push( {:id   => tmp_track[0],
                  :date => DateTime.parse(tmp_track[1]),
                  :path => tmp_track[2]} )
  end
  tracks
end

#get_tracks_with_location_scriptObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/applescript.rb', line 171

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*2}\"\n"\
			"      end if\n"\
"    end repeat\n"\
"    return str\n"\
  "  on error\n"\
"    return 0\n"\
 "  end try\n"\
  "end tell\n"\
end

#prompt_script(buttons = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/applescript.rb', line 18

def prompt_script buttons = []
  buttons = buttons.slice(0..1).map { |b| "Download #{b.gsub(/'/){ %q('"'"') }}" }
  "tell application \"iTunes\"\n"\
 "  display dialog \"\""\
  "    buttons {"\
  "             \"Cancel\""\
  "             #{(buttons.length > 0) ? ",\"#{buttons.first}\"" : ''}"\
  "             #{(buttons.length > 1) ? ",\"#{buttons.last}\""  : ''}"\
  "            }"\
  "    with title \"Henchman 🏃\""\
  "    cancel button \"Cancel\""\
  "    default button \"#{(buttons.length > 0) ? buttons.last : 'Cancel'}\""\
  "    giving up after 60"\
  "    with icon note\n"\
  "end tell"
end

#set_track_location(selection, local_file) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/applescript.rb', line 277

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'
    puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|"\
         "Could not update location of #{selection.reject{|k,v| k == :path || k == :id}.values.join(':')} to #{local_file}"
    false
  else
    true
  end
end

#setup(config) ⇒ Object



7
8
9
# File 'lib/applescript.rb', line 7

def setup config
  @delimiter = config[:delimiter]
end

#update_track_location_script(track_id, local_file) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/applescript.rb', line 35

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