2

Background

If you need a new window of an app you can right-click the app in the dock and click New Window. This is very tedious and takes away precious time. I'd like to have a shortcut for that.

Currently I'm using iCanHazShortcut, which an run terminal commands from shortcuts. I currently use open /Applications/Firefox.app --new (Example). This opens a new instance of an application. I would generally have no problem with that if it wouldn't cripple the dock and the whole window manager over time. So:

Question

How can I open a new window of an application with a terminal command?

Constraints:

  • If no application is running the command should of course create an instance of the app
  • I'm not searching for a specific answer for my Firefox example in the top, but a generic approach (because I'm opening several different apps through shortcuts).
2
  • Do you want a different shortcut for each application? You should be abel to create a Service just using the AppleScript in the answer below, and then creating a key shortcut for that, without third-party apps or setting up new Terminal commands.
    – benwiggy
    Commented May 6, 2022 at 12:02
  • We probably have very different definition of "tedious" and "precious time". Opening or navigating to Terminal, entering 10+ characters is tedious in my vocabulary. Especially when alternatively one can use Spotlight to open any application by entering couple of characters from beginning of it's name and if app is already opened then just adding cmd + n after app window is activated. Commented May 6, 2022 at 15:23

1 Answer 1

4

Add the following line to your ~/.zshrc file. (Or, your ~/.bashrc file, if you are using bash.) If the file does not exist, then create the file.

openwin() { osascript -e "tell application \"$1\" to activate" -e 'tell application "System Events" to keystroke "n" using command down'; }

The next time you open a Terminal window, you will be able to use the above function to send the key combination +N to the application specified. For example, to open a new window in Firefox, enter the following.

openwin firefox

For Microsoft Edge, enter either of the following.

openwin "microsoft edge"
openwin microsoft\ edge

The key combination +N is the default in most applications to open a new window.

When the above function is first used, you may need to do the following.

  • Authorize the Terminal application to send Apple events to System Events. This can be found under Automation on the Privacy tab of the Security & Privacy pane of the System Preferences application.
  • Allow osascript to send keystrokes. This is authorized by add the Terminal application to Accessibility on the Privacy tab of the Security & Privacy pane of the System Preferences application.

An alternative to using a function would be to install a script. This would have the advantage of being customizable. Below is an example script which is named openwin.

#!/bin/zsh
pgrep -i -x -q -U $UID "$1"
result=$? # If application is running then result will equal 0.
script=()
case ${1:l} in
mail)
    script+=(-e "tell application \"$1\" to activate")
    if [[ result -eq 0 ]]; then
        script+=(-e 'tell application "System Events" to keystroke "n" using {command down, option down}')
    fi;;

"microsoft word")
    script+=(-e "tell application \"$1\" to activate")
    script+=(-e 'tell application "System Events" to keystroke "n" using command down');;

*)
    script+=(-e "tell application \"$1\" to activate")
    if [[ result -eq 0 ]]; then
        script+=(-e 'tell application "System Events" to keystroke "n" using command down')
    fi
esac
if [[ $#script -ne 0 ]]; then
    #printf "%s '%s'\n" $script # <-- Just for debugging.
    osascript $script
fi

Below are the commands to install the script. Here I assume /usr/local/bin is already in your PATH variable.

sudo mkdir -p /usr/local/bin
sudo cp openwin /usr/local/bin
sudo chown root:wheel /usr/local/bin/openwin
sudo chmod a+x /usr/local/bin/openwin

The default for the example script is to activate the application, then send the +N key combination only if the application is already running.

The example script has the following customizations.

  • For the Mail application, the +alt+N key combination is sent instead of the default +N.
  • For the Microsoft Word application, the +N key combination is sent regardless of whether the application is running.

References

4
  • Awesome! Note: I could not use the openwin command in iCanHazShortcut. I needed to write the full command there for each application I'm trying to open. E.g. osascript -e "tell application \"firefox\" to activate" -e 'tell application "System Events" to keystroke "n" using command down';.
    – DarkTrick
    Commented May 6, 2022 at 8:05
  • @DarkTrick The script version in this answer should work for iCanHazShortcut.
    – JPhi1618
    Commented May 6, 2022 at 14:37
  • Not for future reference: This commands leads to opening two windows of the application in some cases (perhaps, if it wasn't started already).
    – DarkTrick
    Commented May 10, 2022 at 0:47
  • Thanks for the script version in preference to the function from .zshrc or .bashrc. I always prefer standalone scripts to bloating up my shells with lots of unrelated functions, whenever possible.
    – jrw32982
    Commented May 10, 2022 at 20:13

You must log in to answer this question.

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