我想知道如何使用NSScriptCommandDescription initWithSuiteName:commandName:dictionary: (尤指)。字典应该是什么样的。
最好能看到一个简单的示例,用于一个具有NSString类型的单个参数和NSString类型的返回值的命令。
我认为有关字典应该是什么样子的文档可以找到这里,尤其是在“表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"
}]
}
)(注意:我真的想在这里准确地知道这一点;我不想知道如何注册脚本定义,也不想用这个问题做其他事情。)
发布于 2015-11-27 11:25:49
我知道这是一个很老的问题,但我偶然发现了这个问题,似乎这个统计溢出问题是谷歌搜索如何使用带有参数的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代码是否有效)。
发布于 2011-09-13 02:38:23
我看到的唯一与文档不一致的地方是"Type"的值;虽然文档没有在命令的上下文中显示"Type"的示例,但它在文档示例中用于其他上下文的值是"NSString",而不是"text"。因此,尝试Cocoa类名而不是AppleScript类名。
发布于 2011-09-14 14:34:31
对于我来说,"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()https://stackoverflow.com/questions/7395855
复制相似问题