I’m trying to move the preferences window of Keystroke Pro using AppleScript + System Events, but the window does not move even though it comes to the front.
Here is the script I’m using:tell application "System Events"
tell application process "Keystroke Pro"
set frontmost to true
-- Open the Preferences window
perform action "AXPress" of menu item "設定…" of menu 1 of menu bar item 1 of menu bar 2
end tell
end tell
tell application "System Events" to tell application process "Keystroke Pro"
set position of group 1 of window 1 to {100, 0}
end tell
My intention is:
Open Keystroke Pro’s preferences window.
Immediately move that window to the position {100, 0}.
What actually happens is:
The preferences window opens and comes to the front,
but the position does not change at all.
I suspect I’m targeting the wrong UI element (I’m currently setting the position of group 1 of window 1), or that I need to wait until the window has fully appeared, or maybe Keystroke Pro’s preferences are presented as some kind of sheet / non‑standard window.
What is the correct way to:
Identify the preferences window of Keystroke Pro, and
Move it to a specific position via UI scripting?
If anyone could show an example script or explain how to inspect the UI structure (AX elements) for this app and adjust my script, I’d really appreciate it.
I suspect you need to target the window, not a group within that window.
Since you've just opened the Settings window that should be active and so will be window 1. So all you should need is:
tell application "System Events"
tell application process "Keystroke Pro"
set frontmost to true
-- Open the Preferences window
perform action "AXPress" of menu item "設定…" of menu 1 of menu bar item 1 of menu bar 2
set position of window 1 to {100, 0}
end tell
end tell
Note that {100, 0} is "100 pixels in from the left edge of the main screen, 0 pixels down from the top" -- but the menu bar takes up the top 25-ish pixels so the window will be positioned up against that.
I tried the script you suggested, but unfortunately in my setup the window still doesn’t move — it only comes to the front.
For now I’ve managed to work around it using a different method outside pure AppleScript UI scripting, so I can get the Preferences window where I want it. That said, it would be great if this could be done entirely via AppleScript someday, so if anyone has further ideas or refinements I’d love to hear them.
Why? Unless this is part of a much longer script (one that'll require 20+ KM Actions to replicate) it'll be quicker to "Select a Menu Item" to open the Preferences window then "Manipulate a Window" to position it because any AppleScript has a non-zero initialisation time (40-70ms on my machines, depending on processor and activity).
If you do want to use AppleScript you'll have to do some digging yourself. Open the Preferences window and then run:
tell application "System Events"
tell application process "Keystroke Pro"
return properties of window 1
end tell
end tell
That'll let you check that it is indeed the position property that you need to set, that it is actually a window and, importantly, the window name -- the fix may be as simple as waiting for the window to arrive before sending the reposition command:
tell application "System Events"
tell application process "Keystroke Pro"
set frontmost to true
-- Open the Preferences window
perform action "AXPress" of menu item "設定…" of menu 1 of menu bar item 1 of menu bar 2
-- Wait for the window
repeat until exists window "Name Of Window"
delay 0.1
end repeat
set position of window 1 to {100, 0}
end tell
end tell