Skip to content

Instantly share code, notes, and snippets.

@xcv58
Last active August 29, 2015 14:17
Show Gist options
  • Save xcv58/3a140fb77d3c63767b8d to your computer and use it in GitHub Desktop.
Save xcv58/3a140fb77d3c63767b8d to your computer and use it in GitHub Desktop.
global foundCount
global hideCount
global frontmostCount
on toggle(appPath)
if application appPath is running then
set foundCount to foundCount + 1
if frontmost of application appPath then
if hideCount > 0 then return
set hideCount to hideCount + 1
tell application "System Events"
set frontProcess to first process whose frontmost is true
set visible of frontProcess to false
end tell
else
set frontmostCount to frontmostCount + 1
tell application appPath to activate
tell application appPath
if windows is {} then reopen
end tell
end if
end if
end toggle
on run argv
set hideCount to 0
set foundCount to 0
set frontmostCount to 0
repeat with arg in argv
toggle(arg)
if frontmostCount > 0 then exit repeat
end repeat
if foundCount is 0 then tell application (item 1 of argv) to activate
return 0
end run
@xcv58
Copy link
Author

xcv58 commented Apr 1, 2015

Run

osascript toggle.scpt

Follow application path (prefer) / id / name

This command:

osascript toggle.scpt /Applications/Google\ Chrome.app /Applications/Safari.app

will toggle Chrome first, if Chrome is not running, then try to toggle Safari. If Safari is not running too, it will open Chrome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment