uchcode
12/31/2016 - 7:14 AM

JXA (JavaScript for Automation) snippets for applet.

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)
  },
})