我想知道如何使用
NSScriptCommandDescription initWithSuiteName:commandName:dictionary:
,尤其是字典应该是什么样的。
很高兴看到一个带有NSString
类型的单个参数且类型为NSString
的返回值的命令的简单示例。
我认为有关字典应该是什么样子的文档可以找到here,特别是在“表B-8,命令字典”中。
但是,我正在尝试使用此示例,但它不起作用(返回nil
):
cmdDesc = NSScriptCommandDescription.alloc().initWithSuiteName_commandName_dictionary_(
"Standard Suite",
"execPython",
{
"CommandClass": "NSScriptCommand", # default behavior
"AppleEventCode": "expy", # 4-char code
"Type": "text", # return-type
"ResultAppleEventCode": "NSString", # return-type
"Arguments": [{
"Type": "NSString",
"AppleEventCode": "data"
}]
}
)
(注意:我真的很想知道这里;我不想知道如何注册脚本定义或者用这个问题做其他事情。)
答案 0 :(得分:0)
我唯一能看到不同意文档的是"Type"
的价值;虽然文档没有在命令上下文中显示"Type"
的示例,但在文档的其他上下文示例中的值为"NSString"
,而不是"text"
。因此,请尝试使用Cocoa类名称而不是AppleScript类名称。
答案 1 :(得分:0)
"Arguments"
部分对我来说仍然无法正常工作(我遇到了奇怪的崩溃)但我也可以省略它并仍然在运行时传递参数。
这是代码:
import objc
NSObject = objc.lookUpClass("NSObject")
sharedScriptSuiteReg = objc.lookUpClass("NSScriptSuiteRegistry").sharedScriptSuiteRegistry()
NSScriptCommandDescription = objc.lookUpClass("NSScriptCommandDescription")
sharedAppleEventMgr = objc.lookUpClass("NSAppleEventManager").sharedAppleEventManager()
NSAppleEventDescriptor = objc.lookUpClass("NSAppleEventDescriptor")
from PyObjCTools.TestSupport import fourcc
def register_scripting():
cmdDesc = NSScriptCommandDescription.alloc().initWithSuiteName_commandName_dictionary_(
"Chromium Suite",
"exec Python",
{
"Name": "exec Python",
"CommandClass": "NSScriptCommand", # default behavior
"AppleEventCode": "ExPy", # 4-char code
"AppleEventClassCode": "CrSu",
"Type": "NSString", # return-type
"ResultAppleEventCode": "ctxt", # return-type
"Arguments": {
#"----": {
# "Type": "NSString",
# "AppleEventCode": "comm"
#}
}
}
)
assert cmdDesc is not None
sharedScriptSuiteReg.registerCommandDescription_(cmdDesc)
sharedAppleEventMgr.setEventHandler_andSelector_forEventClass_andEventID_(
appScriptHandler, appScriptHandler.handleExecPy,
fourcc("CrSu"), fourcc("ExPy"))
def handleExecPy(self, ev, replyEv):
print "execPython called,",
cmd = ev.descriptorForKeyword_(fourcc("comm")).stringValue()
print "cmd:", repr(cmd)
res = eval(cmd)
res = unicode(res)
replyEv.setDescriptor_forKeyword_(NSAppleEventDescriptor.descriptorWithString_(res), fourcc("----"))
return True
try:
class AppScriptHandler(NSObject):
def handleExecPy(self, ev, replyEv):
try: return handleExecPy(self, ev, replyEv)
except: traceback.print_exc()
return
except:
AppScriptHandler = objc.lookUpClass("AppScriptHandler")
appScriptHandler = AppScriptHandler.alloc().init()
这与这个简单的客户端演示一起使用:
#!/usr/bin/python
import aem
fullpath = aem.findapp.byname("Google Chrome")
app = aem.Application(fullpath)
def execPy(cmd):
return app.event("CrSuExPy", {"comm": cmd}).send()
print execPy("app.bundle()")
def simple_shell():
import sys
try: import readline
except: pass # ignore
while True:
try:
s = raw_input("> ")
except:
print "breaked debug shell:", sys.exc_info()[0].__name__
break
s = s.strip()
if s: print execPy(s)
simple_shell()
答案 2 :(得分:0)
我知道这是一个非常古老的问题,但我偶然发现这个问题似乎这个statckoverflow问题是谷歌搜索如何使用带参数的NSScriptCommandDescription时最常见的结果。
接受答案中的代码是正确的,除了如何创建参数值。参数值必须是使用参数描述映射参数名称的字典。使用接受的答案代码作为示例,为了传递参数,我们的字典应如下所示:
cmdDesc = NSScriptCommandDescription.alloc().initWithSuiteName_commandName_dictionary_(
"Chromium Suite",
"exec Python",
{
"Name": "exec Python",
"CommandClass": "NSScriptCommand", # default behavior
"AppleEventCode": "ExPy", # 4-char code
"AppleEventClassCode": "CrSu",
"Type": "NSString", # return-type
"ResultAppleEventCode": "ctxt", # return-type
"Arguments": {
"firstArg": {
"Type": "NSString",
"AppleEventCode": "comm"
}
}
}
)
上面的字典适用于ObjectC(我不知道python代码是否有效)。