Skip to content

Instantly share code, notes, and snippets.

@ClementGre
Last active September 26, 2024 20:03
Show Gist options
  • Save ClementGre/9f422d96e716af67be51210d2210906b to your computer and use it in GitHub Desktop.
Save ClementGre/9f422d96e716af67be51210d2210906b to your computer and use it in GitHub Desktop.
Phoenix MacOS script to keep finder window on the desktop it was opened on (instead of switching between desks due to a bug of MacOS).
// Utils
function getWindowSpace(window){
return Space.all().find(s => s.windows().find(w => w.isEqual(window)))
}
function getSpaceFromHash(hash){
return Space.all().find(s => s.hash() === hash)
}
// Default space definition
Event.on('windowDidOpen', (window) => {
if(window.app().name() == 'Finder'){
let space = getWindowSpace(window)
if(space){
Storage.set(window.hash(), space.hash());
}
}
});
// Space definition with Control + Option + S
Key.on('s', ['option', 'control'], () => {
const window = Window.focused();
if(window.app().name() == 'Finder'){
let space = getWindowSpace(window)
if(space){
Storage.set(window.hash(), space.hash());
}
}
});
// Move window to default space
Event.on('spaceDidChange', () => {
// Iterate over all windows in all spaces
Space.all().forEach(space => space.windows().forEach(window => {
if(window.app().name() == 'Finder'){
let defaultSpace = getSpaceFromHash(Storage.get(window.hash()))
if(defaultSpace && !space.isEqual(defaultSpace)){
defaultSpace.moveWindows([window])
}
}
}))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment