Moving Safari Windows


I'm new to KM (as of today), and tried to create a few simple macros (which worked well), but I have a problem with one. I have three monitors and wanted to create a button that would move my opened Safari window from one monitor to another (ideally resizing the window, but let's not get too ambitious). The script (actually three scripts, but they are all the same), which simply says to move the frontmost window to the left monitor (or center or right), works fine with a Word document (though a bit slow in execution--about a second or two). But in Safari I get no response. So I wrote an AppleScript that would change the bounds of the window, and when I run it from Script Debugger, it works (and fast as I would expect). Strangely, though, when I run it from KM, it does nothing. I have Safari set to allow JavaScript from Apple Events, and I gave KM permission to control Safari in System Preferences -> Security.

What else am I missing?



1 Like

May we see source code to the scripts that you've referenced, please (or the one you want to troubleshoot) ? If the Word script is actually a macro, then screenshots are helpful if this is the thing needing fixed. If the AppleScript is the thing to be fixed, you can copy and paste the code and format it as code using the editimg toolbar.

Also, what could be useful is to run the KM macro again, making sure to save the output of the script to a variable. It may not appear as though the script does anything from within KM, but the value returned to and stored in the variable will tell us for sure (you can see as list of variables and their values in the Preferences dialog).

Thanks for your help!

Here's what I did:

It is the same for Word and Safari.

I suspect I'm using the variable wrong, but all I get is a message saying that it failed to evaluate.

By the way, from the moment I click the command, to its execution (on a Word document), it takes about 3 seconds. A Safari window never moves.

Yes. Your use of "VarName" does not make any sense:

  1. You did NOT define it before you used it
  2. I see no point in setting "VarName" to a KM Named Clipboard.

Here is a macro I have been using for at least 2 years that moves the frontmost window to the other screen. Perhaps you can use it to get started.
Please let us know if you have any questions.


MACRO:   Move Window to Other Monitor (Screen)

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/9/b/9bc0908a6d8a454ebfab3e85279c8b9fb406202a.kmmacros">Move Window to Other Monitor (Screen).kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/3X/8/2/825567260185ef7b50a0840274a226d60069fd7f.png" width="562" height="2410">
1 Like

Thank you. Yours worked, though I would love to know why mine (the AppleScript, not the variable) did not work in KM but it did in Script Debugger (with Safari). Anyway, thanks for the help. Now the script moves effortlessly among the three monitors (and resizes the windows to fit)!

Nothing immediately jumps out as being problematic with the script (info for is technically deprecated, but that means very little when it comes to AppleScript). If you want a clue what the problem is, create a separate macro, stick the AppleScript action in it, have it set to: save the output to a variable; notify on failure; and include errors in its output. Run it, then look up the contents of the variable in the preferences dialog. There must be an error being thrown to cause the script not to follow through, so that should then be saved in the variable.

Hey @mbust,

Next time please upload your macro, so we can directly test.

My recreation of your script works fine for me albeit a little slow.

Set Bounds of Front Window (Generic) -- Original.kmmacros
      (5.0 KB, Creator: KM 9.0.1)

A more streamlined implementation:

Set Bounds of Front Window (Generic) v1.00.kmmacros
      (5.0 KB, Creator: KM 9.0.1)

NOTE – These will NOT work in applications that don't support AppleScript and bounds.

Keyboard Maestro is a better choice for generic window movements, although AppleScripting System Events works fine too.

tell application "System Events"
   tell (first process whose frontmost is true and background only is false)
      tell (first window whose subrole is "AXStandardWindow")
         set {position, size} to {{0, 23}, {960, 1173}}
      end tell
   end tell
end tell

Ordinarily I'd use a reference to front window, but I had to filter this to get it to work with a normal window in Brave Browser. (Presumably Google Chrome is also affected.)



Thank you very much! I will be sure to post the macro next time.

1 Like