Class: Osaka::RemoteControl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, base_location = Location.new("")) ⇒ RemoteControl

Returns a new instance of RemoteControl.



14
15
16
17
# File 'lib/osaka/remotecontrol.rb', line 14

def initialize(name, base_location = Location.new(""))
  @name = name
  @base_location = base_location
end

Instance Attribute Details

#base_locationObject

Returns the value of attribute base_location.



12
13
14
# File 'lib/osaka/remotecontrol.rb', line 12

def base_location
  @base_location
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/osaka/remotecontrol.rb', line 11

def name
  @name
end

Instance Method Details

#==(obj) ⇒ Object



19
20
21
# File 'lib/osaka/remotecontrol.rb', line 19

def ==(obj)
  @name == obj.name && base_location == obj.base_location
end

#activateObject



44
45
46
# File 'lib/osaka/remotecontrol.rb', line 44

def activate
  check_output( tell("activate"), "activate" )
end

#attributes(location = "") ⇒ Object



194
195
196
197
198
199
# File 'lib/osaka/remotecontrol.rb', line 194

def attributes(location = "")
  attributelist = get!("attributes", location)
  attributelist.split("of application process #{name}").collect { |attribute|
    attribute.match("attribute (.+?) .*")[1]
  }
end

#check_output(output, action) ⇒ Object



39
40
41
42
# File 'lib/osaka/remotecontrol.rb', line 39

def check_output(output, action)
  print_warning(action, output) unless output.empty?
  output
end

#click(element) ⇒ Object



142
143
144
145
# File 'lib/osaka/remotecontrol.rb', line 142

def click(element)
  activate
  click!(element)
end

#click!(element) ⇒ Object



136
137
138
139
140
# File 'lib/osaka/remotecontrol.rb', line 136

def click!(element)
  # Click seems to often output stuff, but it doesn't have much meaning so we ignore it.
  system_event!("click #{construct_location(element)}")
  self
end

#click_menu_bar(menu_item, menu_name) ⇒ Object



147
148
149
150
151
# File 'lib/osaka/remotecontrol.rb', line 147

def click_menu_bar(menu_item, menu_name)
  activate
  menu_bar_location = at.menu_bar_item(menu_name).menu_bar(1)
  click!(menu_item + at.menu(1) + menu_bar_location)
end

#construct_key_statement(key_keys) ⇒ Object



120
121
122
123
# File 'lib/osaka/remotecontrol.rb', line 120

def construct_key_statement(key_keys)
  return "return" if key_keys == :return
  "\"#{key_keys}\""
end

#construct_location(location) ⇒ Object



178
179
180
# File 'lib/osaka/remotecontrol.rb', line 178

def construct_location(location)
  form_location_with_window(location).to_s
end

#construct_modifier_statement(modifier_keys) ⇒ Object



114
115
116
117
118
# File 'lib/osaka/remotecontrol.rb', line 114

def construct_modifier_statement(modifier_keys)
  modified_key_string = [ modifier_keys ].flatten.collect! { |mod_key| mod_key.to_s + " down"}.join(", ")
  modifier_command = " using {#{modified_key_string}}" unless modifier_keys.empty?
  modifier_command
end

#construct_prefixed_location(location) ⇒ Object



182
183
184
# File 'lib/osaka/remotecontrol.rb', line 182

def construct_prefixed_location(location)
  form_location_with_window(location).as_prefixed_location
end

#convert_mac_version_string_to_symbol(version_string) ⇒ Object



258
259
260
# File 'lib/osaka/remotecontrol.rb', line 258

def convert_mac_version_string_to_symbol(version_string)

end

#current_window_invalid?(window_list) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/osaka/remotecontrol.rb', line 231

def current_window_invalid?(window_list)
  @base_location.to_s.empty? || window_list.index(current_window_name).nil?
end

#current_window_nameObject



225
226
227
228
229
# File 'lib/osaka/remotecontrol.rb', line 225

def current_window_name
  matchdata = @base_location.to_s.match(/window "(.*)"/)
  return "" if matchdata.nil? || matchdata[1].nil?
  matchdata[1]
end

#exists?(location) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/osaka/remotecontrol.rb', line 61

def exists?(location)
  system_event!("exists #{construct_location(location)}").strip == "true"
end

#focusObject



158
159
160
161
162
163
164
165
166
# File 'lib/osaka/remotecontrol.rb', line 158

def focus
  if (base_location.to_s.empty? || not_exists?(base_location))
    currently_active_window = window_list[0]
    currently_active_window ||= ""
    @base_location = (currently_active_window.to_s.empty?) ? Location.new("") : at.window(currently_active_window)
  end

  focus!
end

#focus!Object



168
169
170
# File 'lib/osaka/remotecontrol.rb', line 168

def focus!
  system_event!("set value of attribute \"AXMain\" of #{base_location.top_level_element} to true") unless base_location.top_level_element.to_s.empty?
end

#form_location_with_window(location) ⇒ Object



172
173
174
175
176
# File 'lib/osaka/remotecontrol.rb', line 172

def form_location_with_window(location)
  new_location = Location.new(location)
  new_location += @base_location unless new_location.has_top_level_element?
  new_location
end

#get!(element, location = "") ⇒ Object



186
187
188
# File 'lib/osaka/remotecontrol.rb', line 186

def get!(element, location = "")
  system_event!("get #{element}#{construct_prefixed_location(location)}").strip
end

#get_app!(element) ⇒ Object



190
191
192
# File 'lib/osaka/remotecontrol.rb', line 190

def get_app!(element)
  system_event!("get #{element}").strip
end

#keystroke(key, modifier_keys = []) ⇒ Object



130
131
132
133
134
# File 'lib/osaka/remotecontrol.rb', line 130

def keystroke(key, modifier_keys = [])
  activate
  focus
  (modifier_keys == []) ? keystroke!(key) : keystroke!(key, modifier_keys)
end

#keystroke!(key, modifier_keys = []) ⇒ Object



125
126
127
128
# File 'lib/osaka/remotecontrol.rb', line 125

def keystroke!(key, modifier_keys = [])
  check_output( system_event!("keystroke #{construct_key_statement(key)}#{construct_modifier_statement(modifier_keys)}"), "keystroke")
  self
end

#launchObject



48
49
50
# File 'lib/osaka/remotecontrol.rb', line 48

def launch
  check_output( tell("launch"), "launch" )
end

#mac_versionObject



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

def mac_version
  @mac_version_string ||= Osaka::ScriptRunner.execute("system version of (system info)").strip
  @mac_version ||= case @mac_version_string
    when /^10.6.*/
      :snow_leopard
    when /^10.7.*/
      :lion
    when /^10.8.*/
      :mountain_lion
    when /^10.10.*/
      :yosemite
    else
      :other
    end

end

#mac_version_stringObject



252
253
254
255
# File 'lib/osaka/remotecontrol.rb', line 252

def mac_version_string
  mac_version
  @mac_version_string
end

#not_exists?(location) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/osaka/remotecontrol.rb', line 65

def not_exists?(location)
  system_event!("not exists #{construct_location(location)}").strip == "true"
end


35
36
37
# File 'lib/osaka/remotecontrol.rb', line 35

def print_warning(action, message)
  puts "Osaka WARNING while doing #{action}: #{message}"
end

#quitObject



52
53
54
# File 'lib/osaka/remotecontrol.rb', line 52

def quit
  keystroke("q", :command)
end

#running?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/osaka/remotecontrol.rb', line 31

def running?
  ScriptRunner::execute("tell application \"System Events\"; (name of processes) contains \"#{@name}\"; end tell").strip == "true"
end

#set(element, location, value) ⇒ Object



201
202
203
204
# File 'lib/osaka/remotecontrol.rb', line 201

def set(element, location, value)
  activate
  set!(element, location, value)
end

#set!(element, location, value) ⇒ Object



153
154
155
156
# File 'lib/osaka/remotecontrol.rb', line 153

def set!(element, location, value)
  encoded_value = (value.class == String) ? "\"#{value}\"" : value.to_s
  check_output( system_event!("set #{element}#{construct_prefixed_location(location)} to #{encoded_value}"), "set")
end

#set_current_window(window_name) ⇒ Object



221
222
223
# File 'lib/osaka/remotecontrol.rb', line 221

def set_current_window(window_name)
  @base_location = at.window(window_name)
end

#standard_window_listObject



213
214
215
216
217
218
219
# File 'lib/osaka/remotecontrol.rb', line 213

def standard_window_list
  window_list.collect { |window|
    if get!("subrole", at.window(window)) == "AXStandardWindow"
      window
    end
  }.compact
end

#system_event(event) ⇒ Object



56
57
58
59
# File 'lib/osaka/remotecontrol.rb', line 56

def system_event(event)
  activate
  system_event!(event)
end

#system_event!(event) ⇒ Object



27
28
29
# File 'lib/osaka/remotecontrol.rb', line 27

def system_event!(event)
  ScriptRunner::execute("tell application \"System Events\"; tell process \"#{@name}\"; #{event}; end tell; end tell")
end

#tell(command) ⇒ Object



23
24
25
# File 'lib/osaka/remotecontrol.rb', line 23

def tell(command)
  ScriptRunner::execute("tell application \"#{@name}\"; #{command}; end tell")
end

#wait_until(locations, action) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/osaka/remotecontrol.rb', line 69

def wait_until(locations, action)

  begin
    Timeout::timeout(10) {
      while(true)
          locations.flatten.each { |location|
            return location if yield location
        }
        action.() unless action.nil?
      end
    }
  rescue Exception
    raise Osaka::TimeoutError, "Timed out while waiting for: #{locations.join(", ")}"
  end
end

#wait_until_exists(*locations, &action) ⇒ Object Also known as: until_exists



85
86
87
88
# File 'lib/osaka/remotecontrol.rb', line 85

def wait_until_exists(*locations, &action)
  activate
  wait_until_exists!(*locations, &action)
end

#wait_until_exists!(*locations, &action) ⇒ Object Also known as: until_exists!



90
91
92
93
94
# File 'lib/osaka/remotecontrol.rb', line 90

def wait_until_exists!(*locations, &action)
  wait_until(locations, action) { |location|
    exists?(location)
  }
end

#wait_until_not_exists(*locations, &action) ⇒ Object Also known as: until_not_exists



99
100
101
102
# File 'lib/osaka/remotecontrol.rb', line 99

def wait_until_not_exists(*locations, &action)
  activate
  wait_until_not_exists!(*locations, &action)
end

#wait_until_not_exists!(*locations, &action) ⇒ Object Also known as: until_not_exists!



104
105
106
107
108
# File 'lib/osaka/remotecontrol.rb', line 104

def wait_until_not_exists!(*locations, &action)
  wait_until(locations, action) { |location|
    not_exists?(location)
  }
end

#window_listObject



206
207
208
209
210
211
# File 'lib/osaka/remotecontrol.rb', line 206

def window_list
  windows = get_app!("windows").strip.split(',')
  windows.collect { |window|
    window[7...window =~ / of application process/].strip
  }
end