Class: AX::SystemWide

Inherits:
Element show all
Includes:
Accessibility::Keyboard
Defined in:
lib/ax/systemwide.rb

Overview

Represents the special SystemWide accessibility object.

Previously, this object was a singleton, but that apparently causes problems with the AXAPIs. So you should always create a new instance of the system wide object when you need to use it (even though they are all the same thing).

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#==, #actions, #ancestor, #ancestry, #application, #attribute, #attributes, #blank?, #bounds, #children, #description, #inspect, #inspect_subtree, #invalid?, #method_missing, #methods, #parameterized_attribute, #parameterized_attributes, #perform, #pid, #respond_to?, #screenshot, #set, #size_of, #to_h, #to_point, #to_s, #writable?

Methods included from Accessibility::PrettyPrinter

#pp_checkbox, #pp_children, #pp_enabled, #pp_focused, #pp_identifier, #pp_position

Constructor Details

#initializeSystemWide

Overridden since there is only one way to get the element ref.


42
43
44
# File 'lib/ax/systemwide.rb', line 42

def initialize
  super Accessibility::Element.system_wide
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AX::Element

Class Method Details

.desktopAX::Group

Find and return the group that represents the desktop

Returns:

  • (AX::Group)

19
20
21
# File 'lib/ax/systemwide.rb', line 19

def desktop
  AX::Application.finder.scroll_areas.first.groups.first
end

.status_itemsAX::MenuBarItem

Note:

This currently does not include spotlight or the notification center as they interact oddly with accessibility APIs and how AXElements handle errors

Find and return menu bar items for the system

That is, menu bar items that do not belong to the current app, but that belong to the system, such as the clock or wi-fi menu.

Returns:

  • (AX::MenuBarItem)

35
36
37
# File 'lib/ax/systemwide.rb', line 35

def status_items
  AX::Application.new('SystemUIServer').menu_bar.children
end

Instance Method Details

#element_at(point) ⇒ AX::Element?

Find the element in at the given point for the topmost appilcation window.

nil will be returned if there was nothing at that point.

Parameters:

Returns:


107
108
109
# File 'lib/ax/systemwide.rb', line 107

def element_at point
  @ref.element_at(point).to_ruby
end

#focused_applicationAX::Application

Find and return the application which is frontmost

This is often, but not necessarily, the same as the app that owns the menu bar.

Returns:


123
124
125
# File 'lib/ax/systemwide.rb', line 123

def focused_application
  AX::Application.frontmost_app
end

#hold_modifier(key) ⇒ Number?

Press the given modifier key and hold it down while yielding to the given block.

Examples:


hold_modifier "\\CONTROL" do
  drag_mouse_to point
end

Parameters:

  • key (String)

Returns:

  • (Number, nil)

73
74
75
76
77
78
79
80
81
# File 'lib/ax/systemwide.rb', line 73

def hold_modifier key
  code = EventGenerator::CUSTOM[key]
  raise ArgumentError, "Invalid modifier `#{key}' given" unless code
  KeyCoder.post_event([code, true])
  yield
ensure # if block raises the button might stuck, so ensure it is released
  KeyCoder.post_event([code, false]) if code
  code
end

#on_notification(*args) ⇒ Object

Raises an NoMethodError instead of (possibly) silently failing to register for a notification.

Raises:

  • (NoMethodError)

95
96
97
# File 'lib/ax/systemwide.rb', line 95

def on_notification *args
  raise NoMethodError, 'AX::SystemWide cannot register for notifications'
end

#search(*args) ⇒ Object

The system wide object cannot be used to perform searches. This method is just an override to avoid a difficult to understand error messages.

Raises:

  • (NoMethodError)

86
87
88
# File 'lib/ax/systemwide.rb', line 86

def search *args
  raise NoMethodError, 'AX::SystemWide cannot search'
end

#set_global_timeout(seconds) ⇒ Number

Set the global messaging timeout. Searching through another interface and looking up attributes incurs a lot of IPC calls and sometimes an app is slow to respond.

Parameters:

  • seconds (Number)

Returns:

  • (Number)

118
119
120
# File 'lib/ax/systemwide.rb', line 118

def set_global_timeout seconds
  @ref.set_timeout_to seconds
end

#type(string) ⇒ Boolean Also known as: type_string

Send keyboard input to the focused control element.

For details on how to format the string, check out the Keyboarding documentation.

Parameters:

  • string (String)

Returns:

  • (Boolean)

54
55
56
57
58
# File 'lib/ax/systemwide.rb', line 54

def type string
  keyboard_events_for(string).each do |event|
    KeyCoder.post_event event
  end
end