Module: Osaka::ScriptRunner

Defined in:
lib/osaka/scriptrunner.rb

Constant Summary collapse

@@debug_info_enabled =
false

Class Method Summary collapse

Class Method Details

.debug_prints?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/osaka/scriptrunner.rb', line 28

def self.debug_prints?
  @@debug_info_enabled
end

.disable_debug_printsObject



24
25
26
# File 'lib/osaka/scriptrunner.rb', line 24

def self.disable_debug_prints
  @@debug_info_enabled = false
end

.enable_debug_prints(debug_info_format = :plain_text, filename = "") ⇒ Object



18
19
20
21
22
# File 'lib/osaka/scriptrunner.rb', line 18

def self.enable_debug_prints(debug_info_format = :plain_text, filename = "")
  @@debug_info_enabled = true
  @@debug_info_format = debug_info_format
  @@debug_info_script_filename = filename
end

.execute(applescript) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/osaka/scriptrunner.rb', line 59

def self.execute(applescript)
  script_commands = applescript.gsub("\"", "\\\"").split(';')
  escaped_commands = ""
  script_commands.each { |l|
    escaped_commands += " -e \"#{l.strip}\""
  }

  print_debug_info_for_escaped_commands(applescript, escaped_commands)

  output = ""
  begin
    output = CommandRunner::run("osascript#{escaped_commands}")
  rescue Osaka::SystemCommandFailed => ex
    if ex.message =~ /assistive devices/
      puts "        Osaka execution failed with the error: \#{ex.message}\n        The reason for this is probably that you didn't enable the acess for assistive devices.\n        Without this enabled, Osaka won't be able to execute applescript and thus won't work.\n        You can turn this on (in mountain lion) under:\n           system preferences -> Accessibility -> Enable access for assistive devices\n        If you are under snow leopard, it is under:\n           system preferences -> Universal Access -> Enable access for assistive devices\n\n        Osaka will not continue as it won't work without this enabled. Please enable it and re-run.\n      eom\n      exit\n      return\n    end\n    raise(Osaka::ScriptRunnerError, \"Error received while executing: \\\"\#{applescript}\\\" with message \\\"\#{ex.message}\\\"\")\n  end\n  print_debug_info_for_additional_output(output)\n  output\nend\n"

.execute_file(scriptName, parameters = "") ⇒ Object



93
94
95
# File 'lib/osaka/scriptrunner.rb', line 93

def self.execute_file(scriptName, parameters = "")
  CommandRunner::run("osascript #{scriptName} #{parameters}".strip)
end


48
49
50
51
52
53
54
55
56
57
# File 'lib/osaka/scriptrunner.rb', line 48

def self.print_debug_info_for_additional_output(output)
  if (!output.empty? && debug_prints?)
    if (@@debug_info_format == :plain_text)
      debug_output = "Output was: #{output}"
    elsif (@@debug_info_format == :short_html)
      debug_output = "Output: <b>#{output}</b><br>"
    end
    puts debug_output
  end
end


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osaka/scriptrunner.rb', line 32

def self.print_debug_info_for_escaped_commands(original_applescript_commands, escaped_commands)
  if (debug_prints?)
    if (@@debug_info_format == :plain_text)
      debug_output = "Executing: osascript#{escaped_commands}"
    elsif (@@debug_info_format == :short_html)
      debug_output = original_applescript_commands
      debug_output += "<br>"
    elsif (@@debug_info_format == :script)
      File.open(@@debug_info_script_filename, File::WRONLY|File::APPEND|File::CREAT, 0755) { |file|
        file.puts("osascript#{escaped_commands}")
      }
    end
    puts debug_output
  end
end