Skip to content

Instantly share code, notes, and snippets.

@ndrake
Created December 19, 2023 13:28
Show Gist options
  • Save ndrake/dac27169a24504a4c49deeb3f8c7a995 to your computer and use it in GitHub Desktop.
Save ndrake/dac27169a24504a4c49deeb3f8c7a995 to your computer and use it in GitHub Desktop.
// Name: Focus Code
// Description: Focus a specific VS Code Window
// Author: Nate Drake
import "@johnlindquist/kit"
if (!isMac) {
await div(md('## Sorry. I only work on Mac at the moment.'))
exit()
}
// TODO: Handle errors (like Code not running)
const getWindowsScript = `
tell application "System Events"
tell process "Code"
get the title of every window
end tell
end tell
`;
let w = await applescript(getWindowsScript)
const windows = w.split(',')
.map((win) => win.includes('—') ? win?.trim()?.split('—')[1]?.trim() : win?.trim())
.sort((a, b) => a.trim()?.localeCompare(b.trim()))
.map((win, idx) => {
return {
name: `[${idx + 1}] ${win}`,
value: win
};
});
let window = await arg(
{
placeholder: 'Pick VS Code Window'
},
windows
);
const focusWindowScript = `
tell application "System Events" to tell process "Code"
set frontmost to true
windows where title contains "${window}"
if result is not {} then perform action "AXRaise" of item 1 of result
end tell
`;
await applescript(focusWindowScript)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment