-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.rb.example
42 lines (36 loc) · 1.39 KB
/
init.rb.example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Put this init script directly into the Alfred script window and
# it will load main.rb with console logging enabled for Ruby exceptions
# Add workflow directory to ruby path
$: << File.join(File.dirname(__FILE__))
# Add workflow gem repository to gem path
ENV['GEM_PATH'] = File.join(Dir.pwd, 'ruby-1.8')
# Provide a facility to log messages to Console.app:
def console_log(msg)
escape = proc{ |m| m.gsub("'", "'\\\\''") }
`logger -t 'Alfred Workflow' '#{escape[msg]}'`
end
# Yay! A way to debug Alfred scripts. You can use either method:
# raise "I need to explain how this happened…"
# OR
# Alfred.log "parachutes away!"
#
# Note: to see the messages, open up Console.app. If there is a backtrace,
# you'll need to press the little "right arrow" next to the log line.
# Load and evaluate a Ruby file in the current bundle's directory
def load_ruby_file(filename)
ruby_code = IO.read(filename)
eval(ruby_code, binding, File.expand_path(filename))
end
begin
# Load the Alfred singleton object
load_ruby_file("alfred.rb")
Alfred = AlfredInit.new("{query}")
# Load your code from main.rb:
load_ruby_file("main.rb")
rescue Exception => e
slice = -1
# Ignore final 2 lines of backtrace that always come from this file
slice = -3 if e.backtrace.size > 2
bt = e.backtrace[0..slice].join("\n ")
console_log "#{e.message} (#{e.class}) [query: {query}]\n " + bt
end