Skip to content

Instantly share code, notes, and snippets.

@uchcode
Last active September 10, 2023 19:16
Show Gist options
  • Save uchcode/c957bd90919e8f6c93551dbc6618f83f to your computer and use it in GitHub Desktop.
Save uchcode/c957bd90919e8f6c93551dbc6618f83f to your computer and use it in GitHub Desktop.
JXA (JavaScript for Automation) snippets for applet.
function MenuItem(title, action, target) {
if (!title && !action && !target) return $.NSMenuItem.separatorItem
let i = $.NSMenuItem.alloc.init
i.title = title
i.action = action
i.target = target
return i
}
function StatusItem() {
return $.NSStatusBar.systemStatusBar.statusItemWithLength($.NSVariableStatusItemLength)
}
function Window(x, y, width, height) {
let r = $.NSMakeRect(x, y, width, height)
let s = $.NSTitledWindowMask
s|= $.NSClosableWindowMask
s|= $.NSMiniaturizableWindowMask
let b = $.NSBackingStoreBuffered
let d = false
let w = $.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer(r, s, b, d)
w.releasedWhenClosed = false
if (x==-1 && y==-1) w.center
return w
}
function WindowDelegate() {
if (!$.WindowDelegate) ObjC.registerSubclass({
name:'WindowDelegate',
protocols: ['NSWindowDelegate'],
methods: {
'windowWillClose:' (notification) {
return $.NSApplication.sharedApplication.terminate(null)
},
},
})
return $.WindowDelegate.alloc.init
}
function Button(x, y, width, height) {
let r = $.NSMakeRect(x, y, width, height)
let b = $.NSButton.alloc.initWithFrame(r)
b.bezelStyle = $.NSRoundedBezelStyle
return b
}
function Label(x, y, width, height) {
let r = $.NSMakeRect(x, y, width, height)
let t = $.NSTextField.alloc.initWithFrame(r)
t.drawsBackground = false
t.bordered = false
t.editable = false
t.selectable = true
return t
}
function TextField(x, y, width, height) {
let r = $.NSMakeRect(x, y, width, height)
let t = $.NSTextField.alloc.initWithFrame(r)
return t
}
function Line(x, y, width, height) {
let r = $.NSMakeRect(x, y, width, height)
let b = $.NSBox.alloc.initWithFrame(r)
b.boxType = $.NSBoxSeparator
return b
}
function WebView(url) {
let z = $.NSZeroRect
let c = $.WKWebViewConfiguration.alloc.init
let w = $.WKWebView.alloc.initWithFrameConfiguration(z, c)
let u = $.NSURL.URLWithString(url)
let r = $.NSURLRequest.requestWithURL(u)
w.loadRequest(r)
return w
}
function WebViewWindow(url, x=-1, y=-1, width=1024, height=576) {
let web = WebView(url)
web.frame = $.NSMakeRect(0, 0, width, height)
web.autoresizingMask = $.NSViewWidthSizable | $.NSViewHeightSizable
let win = Window(x, y, width, height)
win.styleMask |= $.NSResizableWindowMask
win.delegate = WindowDelegate()
win.contentView.addSubview(web)
return win
}
function SimpleSubclass(_name, methods) {
if ($[_name]) return $[_name].alloc.init
let _methods = {}
for (let m in methods) {
_methods[m+':'] = {
types: ['void', ['id']],
implementation: methods[m],
}
}
ObjC.registerSubclass({
name: _name,
methods: _methods,
})
return $[_name].alloc.init
}
//==============================================================================
MyAction = SimpleSubclass('Action', {
hello(sender) {
console.log('hello')
},
quit(sender) {
$.NSApplication.sharedApplication.terminate(nil)
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment