Search code examples
shellapplescriptautomator

Bringing an OS X application to the front even if it's a different Mission Control space


What I'm trying to do is switch to an application reliably, even if it's open in a different space, using a shell script. For example, let's assume I'm in space 1, and there is a Safari window open in space 2 but not in space 1. I want to be able to switch to the appropriate space in order make make Safari active. Currently I'm using AppleScript:

osascript -e "
tell application \"Safari\"
    activate
end tell
"

But this only makes the menu bar switch to Safari, instead of moving to the space that contains the Safari window.


Solution

  • Check your System Preferences -> Mission Control

    enter image description here

    Have you checked the second checkbox? (When switching to ....) When yes - your applescript will work.

    Also, you can change it to:

    #!/usr/bin/osascript
    tell application "Safari"
        activate
    end tell
    

    e.g. just add the osascript as an shebang and make the script executable:

    chmod 755 ~/path/to/the/above/script_file