iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐰

Setting up macOS to open new app windows with Cmd+F1

に公開

I will show you how to set up an environment on macOS to instantly open a new window for any app using a combination of the ⌘ key and function keys. This setup allows you to instantly display a new window of the target app in the foreground of your current desktop, which is convenient for tasks like searching the web, pasting text into a text editor, or running commands in the Terminal.

Useful Use Case Examples

Safari / Chrome

When you are working in another app and want to search for something on the web, you can quickly launch a new browser window.

Terminal

When you are working in another app and want to perform command-line operations, you can quickly open a Terminal window.

CotEditor

When you are working in another app and want to paste text or perform minor editing, you can quickly open a CotEditor text window.

Prerequisites

  • Use macOS
  • Use Karabiner Elements
  • Use AppleScript
  • Use function keys around F1–F12

I adopted the combination of ⌘+F1 and onwards for the hotkeys to invoke new windows. This is because few apps use this combination and it is easy to operate with only the left hand.

Defining Rules in Karabiner Elements

Basically, we use Karabiner Elements.

In the Karabiner Elements settings, add a rule like the following to Complex Modifications. Since I regularly use Safari, here is an example of opening a new browser window for Safari. I used ⌘+F3 for the hotkey, but you can change this as you like.

{
    "description": "command + F3 ▸ New Safari Window",
    "manipulators": [
        {
            "from": {
                "key_code": "f3",
                "modifiers": {
                    "mandatory": [
                        "command"
                    ]
                }
            },
            "to": [
                {
                    "shell_command": "osascript -e 'tell application \"Safari\"' -e 'make new document' -e 'activate' -e 'end tell'"
                }
            ],
            "type": "basic"
        }
    ]
}

The key point is using shell_command to execute AppleScript via osascript.
Karabiner Elements detects a specific hotkey -> executes a shell command -> executes AppleScript via the osascript command... and so on.

In the case of Safari

Since osascript uses the -e option to add each line of instruction, multi-line scripts that are not connected by to will follow this concatenation format.

osascript -e 'tell application "Safari"' -e 'make new document' -e 'activate' -e 'end tell'

This is the following AppleScript rewritten for osascript syntax.

tell application "Safari"
    make new document
    activate
end tell

In the case of Google Chrome

For a new window in Google Chrome, rewrite the osascript as follows. The difference from Safari is that the command for a new window is new window instead of new document.

osascript -e 'tell application "Google Chrome"' -e 'make new window' -e 'activate' -e 'end tell'

This is the following AppleScript rewritten for osascript syntax.

tell application "Google Chrome"
    make new window
    activate
end tell

In the case of CotEditor

A new window for CotEditor can be achieved with almost identical syntax.

osascript -e 'tell application "CotEditor"' -e 'make document' -e 'activate' -e 'end tell'

In the case of Terminal

A new window for Terminal can be achieved with a similar approach. I implemented it by executing an empty shell script.

osascript -e 'tell application "Terminal"' -e 'do script ""' -e 'activate' -e 'end tell'

Notes

Individual function key features remain active

Since the Karabiner rules do not overwrite the individual functions of F1, F2, F3, etc., features like F1's "Decrease display brightness" can still be used as they are.

Potential conflicts if ⌘F1, etc., are already used by specific apps

Some apps may already use keyboard shortcuts like ⌘+F1, so functional conflicts may occur. (Karabiner will likely take precedence.)

Privacy/Security permissions may be required

Since macOS may restrict the execution of AppleScript, please handle any necessary privacy or security permissions as required.

Window order cannot be perfectly maintained

Maintaining the order of all currently open windows while bringing only a new window of a specific app to the foreground seems difficult to achieve using AppleScript. By design, all windows belonging to that app move to the front as soon as "activate" is executed.

If you remove "activate," you will no longer be able to activate the app, and the new window will be opened in the background.

Discussion