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?, #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.



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

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:



112
113
114
# File 'lib/ax/systemwide.rb', line 112

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:



128
129
130
# File 'lib/ax/systemwide.rb', line 128

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_key "\\CONTROL" do
  drag_mouse_to point
end

Parameters:

  • key (String)

Returns:

  • (Number, nil)


78
79
80
81
82
83
84
85
86
# File 'lib/ax/systemwide.rb', line 78

def hold_modifier key
  code = EventGenerator::CUSTOM[key]
  raise ArgumentError, "Invalid modifier `#{key}' given" unless code
  @ref.post [[code, true]]
  yield
ensure # if block raises the button might stuck, so ensure it is released
  @ref.post [[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)


100
101
102
# File 'lib/ax/systemwide.rb', line 100

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)


91
92
93
# File 'lib/ax/systemwide.rb', line 91

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)


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

def set_global_timeout seconds
  @ref.set_timeout_to seconds
end

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

Note:

With the SystemWide class, using #type will send the events to which ever app has focus.

Generate keyboard events by simulating keyboard input.

See the Keyboarding documentation for more information on how to format strings.

Parameters:

  • string (String)

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/ax/systemwide.rb', line 59

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