Help with Key Code command — Not Executing

Hey all. I’m trying to write a script for InDesign that incorporates a macro from Keyboard Maestro. Basically it’s goal is to expand the screen and set the workspace for a second monitor. Currently the script activates InDesign, but the key code doesn’t execute.

Here’s what I’m working with:


if application "Adobe InDesign CC 2017" is running then
	tell application "Adobe InDesign CC 2017"
		activate
		delay 0.1
		tell application "System Events" to key code 80
	end tell
end if

I’m a not very versed in this, so go gentle. Thanks in advance for any help.

Model: MBP
AppleScript: 2.5
Browser: Safari 537.36
Operating System: Mac OS X (10.12.5)

You failed to define the target process.

if application "Adobe InDesign CC 2017" is running then
	tell application "Adobe InDesign CC 2017"
		activate
		delay 0.1
		tell application "System Events" to tell process "Adobe InDesign" # I'm not sure of the exact name of the process
		set frontmost to true
		key code 80
		end tell
	end tell
end if

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 29 septembre 2017 10:57:14

Hi SFinSF.

As far as I can find out, key number 80 is the F19 key on an extended keyboard. I presume that this key exists on your keyboard and that it’s programmed to do something in inDesign when simply pressed with no modifier keys.

I’ve had no success myself this morning trying to operate function keys with keystroke or key code, so it may not actually be possible. But hopefully someone will be able to contradict me in this respect.

Hi Yvan.

Although key code and keystroke are listed under the Processes Suite in System Events’s scripting dictionary, it still seems to be the case in Sierra that keyboard events are sent to the frontmost process whatever that may happen to be at the time.

tell application "TextEdit" to activate
delay 2 -- To give time to let go of the Command key.
tell application "System Events" to keystroke "Hello"
tell me to activate
delay 2 -- To give time to let go of the Command key.
tell application "System Events" to tell application process "TextEdit" to keystroke "Hello"

Both commands still also work when UI elements enabled is false.