-
Notifications
You must be signed in to change notification settings - Fork 3
/
RunRubyScriptAction.rb
112 lines (95 loc) · 2.72 KB
/
RunRubyScriptAction.rb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# RunRubyScriptAction.rb
# RunRubyScriptAction
#
# Created by Jason Foreman on 12/26/07.
# Copyright (c) 2008 Jason Foreman. All rights reserved.
#
require 'osx/cocoa'
include OSX
require_framework 'ScriptingBridge'
require_framework 'Automator'
require 'RubyTextView'
class RunRubyScriptAction < AMBundleAction
def typeForDescriptor(desc)
desc[0].to_i << 24 | desc[1].to_i << 16 | desc[2].to_i << 8 | desc[3].to_i
end
def getSBApplication(appname)
bundleId = LSWrapper.bundleIdentifierForApplicationWithName(appname)
return SBApplication.applicationWithBundleIdentifier(bundleId)
end
def getObjectSource(source)
case source.descriptorType()
when typeForDescriptor('obj ')
return convertObject(source)
when typeForDescriptor('psn ')
return getSBApplication(source.stringValue())
end
end
def convertObject(object)
from = object.descriptorForKeyword_(typeForDescriptor("from"))
source = @sources[from.description.to_s] ||= getObjectSource(from)
want = object.descriptorForKeyword_(typeForDescriptor('want'))
elements = source.elementArrayWithCode(want.typeCodeValue())
seld = elements.objectWithID_(object.descriptorForKeyword_(typeForDescriptor('seld')))
return seld
end
def convertInput(input)
if input.class == NSAppleEventDescriptor then
case input.descriptorType
when typeForDescriptor('list')
ret = []
1.upto(input.numberOfItems()) do |i|
ret << convertInput(input.descriptorAtIndex(i))
end
return ret
when typeForDescriptor('obj ')
return convertObject(input)
when typeForDescriptor('utxt')
return input.stringValue()
else
return input
end
else
return input
end
end
def convertOutput(output)
case output
when Array, NSArray
list = NSAppleEventDescriptor.listDescriptor()
output.each_with_index do |o, i|
list.insertDescriptor_atIndex_(convertOutput(o), i)
end
return list
when SBObject
return output.qualifiedSpecifier()
when String, NSString
return NSAppleEventDescriptor.descriptorWithString_(output)
else
NSLog "warning: cannot convert output of type #{output.class}"
return NSNull # TODO return nil, output, something else?
end
end
def runWithInput_fromAction_error(input, action, error)
begin
@sources = {}
scriptSource = self.parameters['ruby script source']
input = convertInput(input)
klass = Class.new
klass.class_eval(scriptSource)
if klass.method_defined? :performAction then
result = klass.new.performAction(input)
else
result = nil
end
return convertOutput(result)
rescue
e = {
:OSAScriptErrorNumber => -1,
:OSAScriptErrorMessage => 'A Ruby exception occured.',
}
error.assign(NSDictionary.dictionaryWithDictionary_(e))
end
end
end