Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created April 6, 2023 21:51
Show Gist options
  • Save SKaplanOfficial/45c4e8c7b63f2ecd5a9477f0fdf2ef60 to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/45c4e8c7b63f2ecd5a9477f0fdf2ef60 to your computer and use it in GitHub Desktop.
Raycast command that uses AppleScriptObjC to create a floating window.
import { closeMainWindow } from "@raycast/api"
import { runAppleScript } from "run-applescript"
const displayWindow = async () => {
console.log(await runAppleScript(`use framework "Foundation"
use scripting additions
set theWindow to missing value
-- Can only display windows on the main thread
my performSelectorOnMainThread:"showWindow" withObject:(missing value) waitUntilDone:true
-- Create and display the window
on showWindow()
global theWindow
set theContentRect to current application's NSMakeRect(0, 0, 500, 250)
set theWindowStyle to (get current application's NSWindowStyleMaskTitled) + (get current application's NSResizableWindowMask) + (get current application's NSFullSizeContentViewWindowMask) + (get current application's NSWindowStyleMaskClosable) + (get current application's NSWindowStyleMaskMiniaturizable) + (get current application's NSWindowStyleMaskResizable)
set theWindow to current application's NSWindow's alloc()'s initWithContentRect:theContentRect styleMask:theWindowStyle backing:(current application's NSBackingStoreBuffered) defer:false
theWindow's setDelegate:me
theWindow's setOpaque:false
theWindow's setMovableByWindowBackground:true
theWindow's setTitlebarAppearsTransparent:true
set closeButtonFrame to current application's NSMakeRect(100, 100, 100, 25)
set closeButton to current application's NSButton's alloc's initWithFrame:closeButtonFrame
closeButton's setTarget:me
closeButton's setAction:"closeWindow:"
theWindow's setLevel:(current application's NSFloatingWindowLevel)
theWindow's contentView()'s addSubview:closeButton
current application's NSApp's runModalForWindow:theWindow
end showWindow
-- Stop the modal (and associated processes) when the window is closed
on closeWindow:sender
current application's NSApp's stopModal()
theWindow's |close|()
end closeWindow:
on windowWillClose:theNotification
current application's NSApp's stopModal()
end windowWillClose:
-- Keep the window on top
on windowDidBecomeKey:theNotification
global theWindow
theWindow's setLevel:(current application's NSFloatingWindowLevel)
end windowDidBecomeKey:
on windowDidResignKey:theNotification
global theWindow
theWindow's setLevel:(current application's NSFloatingWindowLevel)
end windowDidResignKey:`))
}
export default async function Command() {
await closeMainWindow()
await displayWindow()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment