System-wide shortcut to open a new window in Safari?
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.
Is there a way to get Safari to open a new, blank window?
Solution 1:
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.
Solution 2:
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.