What's wrong with my AppleScript?

I'm trying to maniplulate the Display Text window, as in this discussion. This is going in a macro I distribute, so I don't want to rely on the plug-in from that topic. I figured I'd just do it in AppleScript in the macro.

To make it easier to configure, though, I set it up in a subroutine, but I am having a heck of a time getting it to work. Using an alert in the AppleScript, I've confirmed it's getting my values, but the script throws an error when trying to move/size the window.

The subroutine has four local variables, localTop, localLeft, localWidth, and localHeight, and here's the script:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   set theTop to getvariable "localTop" instance kmInst
   set theLeft to getvariable "localLeft" instance kmInst
   set theWidth to getvariable "localWidth" instance kmInst
   set theHeight to getvariable "localHeight" instance kmInst
   display alert "L: " & theLeft & " T: " & theTop & " W: " & theWidth & " H: " & theHeight
   tell application "System Events"
      tell window "Keyboard Maestro - Display Text"
         set position to {theTop, theLeft}
         set size to {theWidth, theHeight}
         -- tell button "OK" to set focused to true
      end tell
   end tell
end tell

If I comment out the window manipulation bits, the display alert works:

But when I uncomment the position and size commands, I get this in Console:

default 22:43:37.957086 -0800 Keyboard Maestro Engine Execute an AppleScript failed with script error: text-script:494:502: execution error: System Events got an error: Can’t set window "Keyboard Maestro - Display Text" to {"900", "150"}. (-10006). Macro “Search the Web” cancelled (while executing Execute AppleScript).

If, on the other hand, I just hardcode values into the script, it works. What am I missing?

thanks;
-rob.

EDIT: just checked with code on my Mac - my suggestion won't help. See @ComplexPoint's post following...

You might want to try re-arranging your AppleScript like this:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set theTop to getvariable "localTop" instance kmInst
	set theLeft to getvariable "localLeft" instance kmInst
	set theWidth to getvariable "localWidth" instance kmInst
	set theHeight to getvariable "localHeight" instance kmInst
end tell

display alert "L: " & theLeft & " T: " & theTop & " W: " & theWidth & " H: " & theHeight
tell application "System Events"
	tell window "Keyboard Maestro - Display Text"
		set position to {theTop, theLeft}
		set size to {theWidth, theHeight}
		--tell button "OK" to set focused to true
	end tell
end tell

I’m not at my Mac right now, so I’ve not tried it myself.

Possibly type.

(KM variables are strings, and position, size are pairs of numbers)

But I think you may first need to fix your reference to the application process (rather than just application):

tell application "System Events"
    tell application process "Keyboard Maestro Engine"
        tell window "Keyboard Maestro - Display Text"

            set position to {theTop, theLeft}
            set size to {theWidth, theHeight}
            
        end tell
    end tell
end tell
2 Likes

I actually had something quite similar to that at first last night, but with the variable assignment piece in there, as that's the whole point of the subroutine. But when buitl that way, it bombs on the section where it assigns the AppleScript variables—it says the double-quotes around the KM variable name aren't expected, as it was expecting end of line. But when I removed the "process" from "application process," then that part worked, but the move/size bombed.

But your code snippet (thanks for that!)—and perhaps a bit of sleep—sparked a thought that seems to have worked: I separated the two sections, so the first refers to the application, and the second to the application process. I have no idea why it behaves differently with "process" and without "process," but the following works perfectly:

set kmInst to system attribute "KMINSTANCE"
    
    tell application "Keyboard Maestro Engine"
        set theTop to getvariable "localTop" instance kmInst
        set theLeft to getvariable "localLeft" instance kmInst
        set theWidth to getvariable "localWidth" instance kmInst
        set theHeight to getvariable "localHeight" instance kmInst
    end tell

    tell application "System Events"
        tell application process "Keyboard Maestro Engine"
            tell window "Keyboard Maestro - Display Text"
                set position to {theTop, theLeft}
                set size to {theWidth, theHeight}
            end tell
        end tell
    end tell

If anyone can enlighten me on the difference between "application process" and "application," I'd love to know. It just seems bizarre.

-rob.

System Events is an application which, inter alia, exposes a collection of
generic application processes. These have a set of standard properties which are independent of the particular applications.

application "Keyboard Maestro Engine" is a reference to an object model designed by @peternlewis

application process "Keyboard Maestro Engine" is a reference to some general properties which System Events can harvest about any application that is running.

1 Like

Thanks, that makes it much clearer—it's nice knowing not just that my snippet works now, but why it works now.

-rob.

AppleScript: The Definitive Guide, 2nd Edition

Perhaps a bit out of date ? The filter example at p153 of which it writes:

I consider that example to be the height of the AppleScript programmer’s art, so perhaps you’d like to pause a moment to admire it.

looks, to be honest, a bit clunky now, and soon cracks under stress ...

Given Anno Domini and the changed landscape (not least the existence of iOS scripting), I would personally suggest that learning JavaScript might be a more rewarding investment of time.

The reference was to System Events rather than the book itself.

Understood.

Perhaps then:

Script Editor > File > Open Dictionary... > System Events.app > Choose

and then, from the Language control, either AppleScript or JavaScript

followed by:

  • application process – Class, or for JS,
  • ApplicationProcess – Class

Perhaps then:

# Open System Events' sdef in Apple's Script Editor.app.
tell application "System Events" to ¬
   set sevApp to path to it

tell application "Script Editor"
   activate
   open sevApp
   set bounds of front window to {221, 23, 1261, 900}
end tell
1 Like