3

I'm trying to use Automator to create a system-wide shortcut to open a new Safari window. I'm using the method described in this answer.

I can get Safari to open a specific url, for example by running

$ open -a Safari "https://apple.stackexchange.com"

From the command line. But this opens a new tab (not a new window) and requires specifying a URL.

Automator also has an "Internet > Display Webpages" action that has the same problems.

enter image description here

Is there a way to get Safari to open a new, blank window?

3
  • 2
    Try using a Run AppleScript action, replacing the default code, with: tell application "Safari" to make new document Commented Nov 11, 2018 at 21:54
  • 1
    Does it have to be in Automator? If not, this is easily accomplished in Keyboard Maestro where you could make a macro to activate Safari and then do ⌘N (or select the File » New Window menu), and then assign a keyboard shortcut for that.
    – TJ Luoma
    Commented Nov 12, 2018 at 0:12
  • Thanks, the AppleScript action worked brilliantly. I didn't try Keyboard Masetro but that product also looks impressive. Thank you both!
    – bcattle
    Commented Nov 12, 2018 at 4:51

3 Answers 3

4

In Automator, create a Service workflow (pre macOS Mojave), or a Quick Action workflow in macOS Mojave.

Add a Run AppleScript action, replacing the default code, with:

tell application "Safari" to make new document

The above piece of AppleScript code by itself will open a new window in Safari however, it will not have focus. If you want the new window to have focus then use a tell block, e.g.:

tell application "Safari"
    make new document
    activate
end tell

You can then assign a keyboard shortcut in the usual manner per information in the link within your question.

3
  • 2
    This is brilliant, but unfortunately in Catalina one needs to grant permissions to open Safari individually to each app that might happen to be open when the shortcut is triggered. Will update if I find a solution... Commented Jan 3, 2020 at 18:32
  • @AntonioFavata I've devised another way to avoid that error. See my answer.
    – kubanczyk
    Commented Feb 18, 2020 at 13:26
  • That sounds brilliant, thanks @kubanczyk — I’ll try as soon as I get a chance Commented Feb 19, 2020 at 14:17
1

Assumptions

This assumes that:

  • Safari is configured not to open new pages in tabs
  • Safari is the default browser.

Howto

Press Cmd-Space then search and run Automator, create a Quick Action workflow (macOS Mojave and later, earlier that item was called Service).

Change the input method in the top right sidebar to no input.

Choose Library -> Utilities -> Run AppleScript action. Drag it to the main area. Replace the default code with:

on run {input, parameters}
    return "https://www.google.com"
end run

Now a second action, choose Library -> Internet -> Display Webpage, drag it to the main area below the first action.

Press Cmd-S to save, name the workflow "Webpage".

Now go to System properties -> Keyboard -> Shortcuts -> Services -> scroll down to find your workflow/service called "Webpage". Press Add shortcut.

Explanation

The contrived setup intends to avoid the problem with the first answer: "in Catalina one needs to grant permissions to open Safari individually to each app that might happen to be open when the shortcut is triggered". Somehow the "Display Webpage" is not impacted by the permission system. The output of the first action becomes the input of the second action.

3
  • Sorry for not testing this until now. It works as advertised, thank you! (Although what I was really after was a way to open a new window without any website...) Commented Mar 4, 2020 at 15:21
  • @AntonioFavata I googled "safari url for empty page" and it says to change https://whatever to about:blank
    – kubanczyk
    Commented Mar 5, 2020 at 18:32
  • That's good thinking, but unfortunately it doesn't seem to work with the workflow, which produces a warning: The action "Display Webpages" was not supplied with the required data. Thanks for your continued efforts though :) Commented Mar 6, 2020 at 11:54
1

Forget the hacks that will invariably break. Use an actual automation tool!

I use Hammerspoon (free, open-source), and the following config (AppleScript taken from the answer above):

hs.hotkey.bind({"cmd", "alt"}, "s", function()
  ok,result = hs.applescript([[
    tell application "Safari"
      make new document
      activate
    end tell
  ]])
end)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .