Toggling a Window Position

I have two simple macros for moving an application’s frontmost window. One moves the app from my second display to the center of the primary, and the other moves it back from there to a fixed position on the secondary. How can I make that into a single macro that toggles it back and forth? An if statement doesn’t have the right handles, I think, but the app I’m moving is not AppleScriptable.

Set a global variable that indicates the current position. Then an IF/THEN should work fine to toggle the macros, or actions.

Ahh. I’ll have to learn how to use a global variable – I usually resort to AppleScript and get the macro to run it if variables are required.

Actually, with KM you don't need AppleScript for most window/screen/menu stuff. It's much easier in KM.

I said global variable to distinguish it from the new local and instance variables. Any KM variable that does NOT start with either "Local" or "Instance" is a global variable.

Here is one of my macros that toggles a window position between two monitors. Maybe it will give you some ideas:

###MACRO:   Move Window to Other Monitor (Screen)

~~~ VER: 1.0    2017-10-07 ~~~

####DOWNLOAD:
Move Window to Other Monitor (Screen).kmmacros (20 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


3 Likes

Extremely helpful – I’ll re-read the manual and get that sorted out. Thank You. I normally just use screen position to move things around, rather than specifying a screen.

At the end of the day for expedience’s sake I went with running this AppleScript from a macro
(pardon the loss of formatting):

use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions

activate application "Tweetbot"
tell application "System Events"
tell application process "Tweetbot"
set P to position of window 1
if item 1 of P is equal to 2565 then
set P to {966, 206}
else
set P to {2565, 30}
end if
set position of window 1 to P
end tell
end tell

That’s one of the great things about KM – you can choose the method that works best for you, including running a script.

For future ref:
###How to Put Script in a Forum Code Block
Just insert your script between the lines with the triple backquotes:

 ```applescript
 -- Your Script Here

If your script is another language, like JavaScript, then use the keyword in lower case for that language, like:
`javascript`

Here is a macro that will paste the script on the clipboard into the forum in the proper format:
####[MACRO: KM Forum -- Paste Script Block](https://forum.keyboardmaestro.com/t/paste-script-block-in-km-forum/4047)