I'm writing an applescript function in python using the applescript module and I'm having trouble generalizing the following function:
scpt = applescript.AppleScript('''
on code()
tell application "System Events"
key code 123 using command down
end tell
end code
''')
so that the keycode and the keydown variables can be input parameters, like so:
scpt = applescript.AppleScript('''
on code(kc, extras)
tell application "System Events"
key code kc extras
end tell
end code
''')
But I get the following run time error:
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
so I assume there's something wonky with my syntax.
I'm using Mac 0SX 10.7.5, python 2.7.1.
EDIT
This code is in a python module named example.py, here is the code again exactly as it is in the module:
import applescript
scpt = applescript.AppleScript('''
on code(kc, extras)
tell application "System Events"
key code kc extras
end tell
end code
''')
I am invoking it from the command line as follows:
$ 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
where line 9 is the last line of my module -- ''').