Question

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 -- ''').

Was it helpful?

Solution

My guess is that you are passing the wrong class of variable. I would do something like this. I would tell myself that I will always pass strings and then let the code convert the variable to the proper class. I would do this because it's difficult to pass the proper class for things like the command/option/control type keys to System Events. They have their own class that only System Events understands. I don't know how you would pass them otherwise.

So using all strings, a simple if statement can handle the rest. Notice also that I added an application variable too. When you issue keystrokes the keystrokes are issued to the frontmost application, so it's a good practice to ensure the application you want to target is frontmost by "activating" it before performing the keystrokes.

Here's the basic applescript code...

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

Then you could run it with something like this (notice I'm passing all strings)...

code("Safari", "123", "command")

OTHER TIPS

The py-applescript module provides AEType and AEEnum classes for defining class (type) and constant (enumerator) objects. There's no terminology support so you'll need to export the application's SDEF from AppleScript Editor and look up the corresponding four-char codes (e.g. document -> b"docu", Unicode text -> b"utxt"). Example:

#!/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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top