Skip to content

Instantly share code, notes, and snippets.

@VincentSit
Created January 21, 2021 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VincentSit/1cd7ec6d3f2533f65a699bb5cbb6103e to your computer and use it in GitHub Desktop.
Save VincentSit/1cd7ec6d3f2533f65a699bb5cbb6103e to your computer and use it in GitHub Desktop.
AppleScript to Show or Hide Application. Launch it if not running.
set appName to "Xcode"
tell application appName
if it is not running then
activate
else
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if visible of application process appName is false or (not (exists (window 1 of process appName))) or (appName is not in activeApp) then
tell process "Dock"
tell UI element appName of list 1
click
end tell
end tell
set visible of application process appName to true
else
set visible of application process appName to false
end if
end tell
end if
end tell
@VincentSit
Copy link
Author

VincentSit commented Jan 21, 2021

Some special cases

Finder

set appName to "Finder"

tell application "System Events"
	set activeApp to name of first application process whose frontmost is true
	if visible of application process appName is false or (not (exists (window 1 of process appName))) or (appName is not in activeApp) then
		tell process "Dock"
			tell UI element appName of list 1
				click
			end tell
		end tell
		set visible of application process appName to true
	else
		set visible of application process appName to false
	end if
end tell

Visual Studio Code

set appName to "Visual Studio Code"
set processName to "Code"

tell application appName
	if it is not running then
		activate
	else
		tell application "System Events"
			set activeApp to name of first application process whose frontmost is true
			if visible of application process processName is false or (not (exists (window 1 of process processName))) or ("Electron" is not in activeApp) then
				tell process "Dock"
					tell UI element appName of list 1
						click
					end tell
				end tell
				set visible of application process processName to true
			else
				set visible of application process processName to false
			end if
		end tell
	end if
end tell

iTerm2

set appName to "iTerm2"
set dockName to "iTerm"

tell application appName
	if it is not running then
		activate
	else
		tell application "System Events"
			set activeApp to name of first application process whose frontmost is true
			if visible of application process appName is false or (not (exists (window 1 of process appName))) or (appName is not in activeApp) then
				tell process "Dock"
					tell UI element dockName of list 1
						click
					end tell
				end tell
				set visible of application process appName to true
			else
				set visible of application process appName to false
			end if
		end tell
	end if
end tell

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