Class: Appear::MacOs

Inherits:
Service show all
Defined in:
lib/appear/mac_os.rb

Overview

The MacOs service handles macOS-specific concerns; mostly running our companion macOS helper tool.

Constant Summary collapse

SCRIPT =

the “realpath” part is basically an assertion that this file exists.

Appear::TOOLS_DIR.join('macOS-helper.js').realpath.to_s

Instance Method Summary collapse

Methods inherited from BaseService

delegate, #initialize, require_service, required_services

Constructor Details

This class inherits a constructor from Appear::BaseService

Instance Method Details

#call_method(method_name, data = nil) ⇒ Any

call a method in our helper script. Communicates with JSON!

Parameters:

  • method_name (String, Symbol)

    check the source of macOS-helper.js for method names.

  • data (Any, nil) (defaults to: nil)

    json-able data to pass to the named method.

Returns:

  • (Any)

    json data returned from the helper

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/appear/mac_os.rb', line 27

def call_method(method_name, data = nil)
  command = [SCRIPT, method_name.to_s]
  command << data.to_json unless data.nil?
  output = run(command)
  result = JSON.load(output.lines.last.strip)

  if result["status"] == "error"
    raise MacToolError.new(result["error"]["message"], result["error"]["stack"])
  else
    result["value"]
  end
end

#has_gui?(process) ⇒ Boolean

Return true if the given process is a macOS GUI process, false otherwise.

@todo: ask Applescript if this a GUI application instead of just looking

at the path

Parameters:

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/appear/mac_os.rb', line 47

def has_gui?(process)
  executable = process.command.first
  executable =~ /\.app\/Contents\//
end