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)


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

def self.debug_prints?
  @@debug_info_enabled
end

.disable_debug_printsObject



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

def self.disable_debug_prints
  @@debug_info_enabled = false
end

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



21
22
23
24
25
# File 'lib/osaka/scriptrunner.rb', line 21

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



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
92
93
94
# File 'lib/osaka/scriptrunner.rb', line 62

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

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



96
97
98
# File 'lib/osaka/scriptrunner.rb', line 96

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


51
52
53
54
55
56
57
58
59
60
# File 'lib/osaka/scriptrunner.rb', line 51

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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/osaka/scriptrunner.rb', line 35

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