I have the below Apple Script that is part of a macro; specifically:
- The macro determines the X and Y co-ordinates of the Display Text window;
- The macro assigns the X and Y co-ordinates if the Display Text window to Local_DisplayTextWindowX and Local_DisplayTextWindowY respectively.
- The macro pauses for N seconds and the below Apple Script then closes the Display Text window.
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set winX to (getvariable "Local_DisplayTextWindowX" instance kmInst) as real
set winY to (getvariable "Local_DisplayTextWindowY" instance kmInst) as real
end tell
tell application "System Events"
tell application process "Keyboard Maestro Engine"
repeat with w in (every window whose name is "Keyboard Maestro - Display Text")
set wPos to position of w
if (item 1 of wPos) is (round winX rounding down) and (item 2 of wPos) is (round winY rounding down) then
tell (first button of w whose role description is "close button")
perform action "AXPress"
end tell
exit repeat
end if
end repeat
end tell
end tell
The problem is that I have used the Apple Script in a number of different macros and in one case (so far) i) two Display Text windows are created at (nearly) the same time ii) the two display text windows have the same pause and iii) only one of the Display Text windows is being closed.
The theory that I am working on is that the last of the two Display Text windows co-ordinates are overwriting the first Display Text Windows co-ordinates in the Apple Script thus only closing one of the Display Text windows.
While I know that I can change the Pause and likely get both Display Text windows to close, I am hoping / looking for a more robust solution.
What can de done to / with the Apple Script to get both Display Text windows to close while continuing to i) use the same variable names (I.e., Local_DisplayTextWindowX and Local_DisplayTextWindowY) and ii) the same Apple Script in all case so that I do not have to remember to change / tweak anything when using this in different macros.
Would for example something like the below be better because there is only 1 call for the co-ordinates where Local_DisplayTextWindowCoOrdinates contains the X|Y co-ordinates:
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
-- Get your pipe variable: "X|Y"
set windowCoords to getvariable "Local_DisplayTextWindowCoOrdinates" instance kmInst
end tell
set AppleScript's text item delimiters to "|"
set targetX to (text item 1 of windowCoords) as number
set targetY to (text item 2 of windowCoords) as number
set AppleScript's text item delimiters to ""
tell application "System Events"
tell application process "Keyboard Maestro Engine"
set allWindows to (every window whose name is "Keyboard Maestro - Display Text")
repeat with w in allWindows
set wPos to position of w
-- Rounding the coordinates to the nearest whole number for Screen 2
set curX to (round (item 1 of wPos))
set curY to (round (item 2 of wPos))
-- Compare whole numbers
if (curX is (round targetX)) and (curY is (round targetY)) then
try
tell (first button of w whose role description is "close button")
perform action "AXPress"
end tell
return "Success"
end try
end if
end repeat
end tell
end tell
Thank you.


