Skip to content

Instantly share code, notes, and snippets.

@uchcode
Created December 31, 2016 16:57
Show Gist options
  • Save uchcode/ab9c38d6f21b8a22a391feb55bfe615a to your computer and use it in GitHub Desktop.
Save uchcode/ab9c38d6f21b8a22a391feb55bfe615a to your computer and use it in GitHub Desktop.
JXA (JavaScript for Automation) StatuBar applet example.
ObjC.import('Cocoa')
ObjC.import('WebKit')
App = Application.currentApplication()
App.includeStandardAdditions = true
MyAction = SimpleSubclass('Action', {
quit(sender) {
App.quit()
},
menuWillOpen(menu) {
App.activate()
},
})
MyWebView = WebView('https://soundcloud.com/')
MyWebView.frame = $.NSMakeRect(0, 0, 1024, 560)
MyWebViewItem = MenuItem('custom view', null, null)
MyWebViewItem.view = MyWebView
MyMenu = $.NSMenu.alloc.init
MyMenu.addItem( MyWebViewItem )
MyMenu.addItem( MenuItem() )
MyMenu.addItem( MenuItem('Quit', 'quit:', MyAction) )
MyMenu.delegate = MyAction
MyStatusItem = StatusItem()
MyStatusItem.title = '音雲'
MyStatusItem.menu = MyMenu
//=====================================================================
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 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 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
}
@uchcode
Copy link
Author

uchcode commented Jan 2, 2017

if you want hide the dock icon, write to info.plist.

	<key>LSUIElement</key>
	<true/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment