Marius Miliunas
Marius Miliunas

Reputation: 1063

OSX Terminal - Change Application Focus

Is there a way using command line to change the focus to a specific application? If so, do I need the process id or what?

My goal is to run a process that compiles files, and then focuses on the browser window once the files have finished compiling.

Upvotes: 25

Views: 10625

Answers (3)

nicholaswmin
nicholaswmin

Reputation: 22949

Apple Silicon - MacOS Sonoma+

Set this in your .bash_profile

function frontmost () {
  osascript -e 'tell application "System Events" to tell process '"\"$1\"" \
            -e 'set frontmost to true' \
            -e 'end tell'
}

and use like so:

frontmost Chrome

Upvotes: 0

terrace
terrace

Reputation: 784

Another option is osascript -e 'activate application "Google Chrome"'.

Upvotes: 15

Amory
Amory

Reputation: 806

If I understand you correctly, this could be done quite easily with open -a. Opening an already-open application brings it to the forefront. The -a option allows you to specify the application you want, so pick your favorite:

open -a Google\ Chrome
open -a Safari
open -a Firefox

Upvotes: 33

Related Questions