Skip to content

Instantly share code, notes, and snippets.

@Michael-F-Ellis
Created February 21, 2017 18:40
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 Michael-F-Ellis/0e7136e2470d0db17ddbd28d31a23411 to your computer and use it in GitHub Desktop.
Save Michael-F-Ellis/0e7136e2470d0db17ddbd28d31a23411 to your computer and use it in GitHub Desktop.
AppleScript for "spring-loaded" jump to a frequently used app.
#!/usr/bin/osascript
-- OS X script for "spring-loaded" app switching
-- Useful if you have a preferred app for jotting notes and ideas.
-- I use MacVim with Vimwiki, but you can use whatever by changing one line in this script.
-- To make this useful, you need to assign a hotkey.
-- OS X won't let you directly assign a hotkey to a script.
-- You can get around that by creating an Automator Service that runs this script.
-- See http://apple.stackexchange.com/questions/175215/how-do-i-assign-a-keyboard-shortcut-to-an-applescript-i-wrote for how to do that.
-- I use Ctrl-Cmd-B because its not commonly used by other apps. You may need something else.
-- With a hotkey in place, you can press it from within any app and jump to your target app.
-- When you're done, the same hotkey takes you back to your prior app.
global frontAppName, targetAppName
tell application "System Events"
set targetAppName to "MacVim" -- Change to your preferred target
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
-- display dialog "front app name: " & frontAppName -- debug
end tell
if ((frontAppName as string) is equal to targetAppName) then
set apps to {}
set pathToSaved to (path to scripts folder as text) & "LastFrontApp.txt"
set apps to paragraphs of (read file pathToSaved)
set lastApp to (first item of apps)
-- display dialog "last app: " & lastApp -- debug
tell application lastApp to activate
else
set outputFile to (path to scripts folder as text) & "LastFrontApp.txt"
try
set fileReference to open for access file outputFile with write permission
set eof fileReference to 0 -- truncate it
write frontAppName to fileReference
close access fileReference
on error
try
close access file outputFile
end try
end try
tell application targetAppName to activate
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment