macosbashtask-switching

How to programatically make a key shortcut?


I'm not even sure if bash is the most proper way to achieve this but, I need to, programmatically, switch between opened applications on OS X.

I'm thinking about doing a bash program to execute "cmd+tab".

I'm unable to find this on a regular search, so, perhaps someone can point me some directions here?


Solution

  • You can use Applescript to achieve this.

    To focus on a specific application:

    tell application "Finder" to activate
    

    And to emulate an actual Cmd+Tab input:

    tell application "System Events"
      key down command
      keystroke tab
      key up command
    end tell
    

    Applescript files (*.scpt) can be run through the command line with osascript file.scpt.