Python 如何在同一AppleScript命令中使用多个变量?
我正在使用applescript模块用python编写一个applescript函数,但在泛化以下函数时遇到困难:Python 如何在同一AppleScript命令中使用多个变量?,python,variables,python-2.7,applescript,keycode,Python,Variables,Python 2.7,Applescript,Keycode,我正在使用applescript模块用python编写一个applescript函数,但在泛化以下函数时遇到困难: scpt = applescript.AppleScript(''' on code() tell application "System Events" key code 123 using command down end tell end code ''') 这样,keycode和keydown变量可以
scpt = applescript.AppleScript('''
on code()
tell application "System Events"
key code 123 using command down
end tell
end code
''')
这样,keycode和keydown变量可以作为输入参数,如下所示:
scpt = applescript.AppleScript('''
on code(kc, extras)
tell application "System Events"
key code kc extras
end tell
end code
''')
但我得到以下运行时错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "example.py", line 28, in <module>
''')
File "build/bdist.macosx-10.7-intel/egg/applescript/__init__.py", line 49, in __init__
applescript.ScriptError: Expected end of line but found application constant or consideration. (-2741) range=410-414
我从命令行调用它,如下所示:
$ python
Python 2.7.1 (r271:86832, Aug 5 2011, 03:30:24)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import example as e
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "example.py", line 9, in <module>
''')
File "build/bdist.macosx-10.7-intel/egg/applescript/__init__.py", line 49, in __init__
applescript.ScriptError: Expected end of line but found identifier. (-2741) range=90-96
$python
Python 2.7.1(r271:868321911年8月5日03:30:24)
[GCC 4.2.1(基于苹果公司5658版本)(LLVM版本2335.15.00)]关于达尔文
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>将示例导入为e
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“example.py”,第9行,在
''')
文件“build/bdist.macosx-10.7-intel/egg/applescript/_init__.py”,第49行,in__init__
applescript.ScriptError:应为行尾,但找到标识符。(-2741)范围=90-96
其中第9行是模块的最后一行--'''')。我猜您传递了错误的变量类。我会这样做。我会告诉自己,我将始终传递字符串,然后让代码将变量转换为适当的类。我之所以这样做,是因为很难为系统事件传递命令/选项/控件类型键之类的适当类。它们有自己的类,只有系统事件才能理解。我不知道你怎么能通过考试 因此,使用所有字符串,一个简单的if语句可以处理其余的字符串。还要注意,我还添加了一个应用程序变量。当您发出击键时,击键会被发送到最前面的应用程序,因此,在执行击键之前,通过“激活”它来确保您要针对的应用程序是最前面的,这是一个很好的做法 下面是基本的applescript代码
on code(appName, theNum, theModifier)
tell application appName to activate
delay 0.2
set theNum to theNum as number
tell application "System Events"
if theModifier is "command" then
key code theNum using command down
else if theModifier is "option" then
key code theNum using option down
else if theModifier is "control" then
key code theNum using control down
else if theModifier is "shift" then
key code theNum using shift down
end if
end tell
end code
然后你可以用这样的东西运行它(注意我传递了所有的字符串)
py applescript模块提供了
AEType
和AEEnum
类,用于定义类
(类型)和常量
(枚举数)对象。没有术语支持,因此您需要从AppleScript编辑器导出应用程序的SDEF,并查找相应的四字符代码(例如,文档
->b“docu”
,Unicode文本
->b“utxt”
)。例如:
#!/usr/bin/python2.7
import applescript
# AppleScript 'constants' (four-char codes taken from 'System Events.sdef')
command_down = applescript.AEEnum("Kcmd")
control_down = applescript.AEEnum("Kctl")
option_down = applescript.AEEnum("Kopt")
shift_down = applescript.AEEnum("Ksft")
scpt = applescript.AppleScript('''
on code(kc, extras)
tell application "System Events"
key code kc using extras
end tell
end code
''')
scpt.call("code", 45, [command_down, option_down]) # Cmd-Opt-N
哪一行是28,你能告诉我你是如何调用这个函数的吗?
code("Safari", "123", "command")
#!/usr/bin/python2.7
import applescript
# AppleScript 'constants' (four-char codes taken from 'System Events.sdef')
command_down = applescript.AEEnum("Kcmd")
control_down = applescript.AEEnum("Kctl")
option_down = applescript.AEEnum("Kopt")
shift_down = applescript.AEEnum("Ksft")
scpt = applescript.AppleScript('''
on code(kc, extras)
tell application "System Events"
key code kc using extras
end tell
end code
''')
scpt.call("code", 45, [command_down, option_down]) # Cmd-Opt-N