Created
April 20, 2023 22:46
-
-
Save stephancasas/df3d7f910bd36e38fb4d0000e16be9a3 to your computer and use it in GitHub Desktop.
Close all other VSCode windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env osascript -l JavaScript | |
function run(_) { | |
const pid = $.NSRunningApplication.runningApplicationsWithBundleIdentifier( | |
'com.microsoft.VSCode', | |
).firstObject.processIdentifier; | |
// Bring all VSCode windows to the current Mission Control view. | |
$.CGSProcessAssignToAllSpaces($.CGSMainConnectionID(), pid); | |
// Unset future windows from assignment to all Mission Control spaces. | |
$.CGSProcessAssignToSpace($.CGSMainConnectionID(), pid, 0); | |
// Create the HIServices-enabled application. | |
const vsCodeApp = $.AXUIElementCreateApplication(pid); | |
// Get all VS Code windows as instances of AXUIElement. | |
const $windows = Ref(); | |
$.AXUIElementCopyAttributeValue(vsCodeApp, 'AXWindows', $windows); | |
// Close each window that is NOT the main/current window. | |
$windows[0].js.forEach((window) => { | |
// Is this window the main window? | |
const $isMain = Ref(); | |
$.AXUIElementCopyAttributeValue(window, 'AXMain', $isMain); | |
if (!$isMain[0].js) { | |
// Get the close button. | |
const $closeButton = Ref(); | |
$.AXUIElementCopyAttributeValue(window, 'AXCloseButton', $closeButton); | |
// Press the close button. | |
$.AXUIElementPerformAction($closeButton[0].js, 'AXPress'); | |
} | |
}); | |
} | |
/** | |
* ----------------------------------------------------------------------------- | |
* CGSInternal Bindings | |
* ----------------------------------------------------------------------------- | |
* | |
* Private imports resolving window-space-display relationships. | |
* | |
*/ | |
/* prettier-ignore */ | |
(() => { | |
ObjC.bindFunction('CGSMainConnectionID', ['int', []]); | |
ObjC.bindFunction('CGSProcessAssignToAllSpaces', ['int', ['int', 'int']]); | |
ObjC.bindFunction('CGSProcessAssignToSpace', ['int', ['int', 'int', 'int']]); | |
})(); | |
/** | |
* ----------------------------------------------------------------------------- | |
* Accessibility Bindings | |
* ----------------------------------------------------------------------------- | |
* | |
* Import HIServices functions. | |
*/ | |
// prettier-ignore | |
(() => { | |
ObjC.import('Cocoa'); // yes, it's necessary -- stop telling me it isn't | |
ObjC.bindFunction('AXUIElementPerformAction', ['int', ['id', 'id']]); | |
ObjC.bindFunction('AXUIElementCreateApplication', ['id', ['unsigned int']]); | |
ObjC.bindFunction('AXUIElementCopyAttributeValue',['int', ['id', 'id', 'id*']]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment