uchcode
12/31/2016 - 6:56 AM

JXA (JavaScript for Automation) Window applet template.

JXA (JavaScript for Automation) Window applet template.

ObjC.import('Cocoa')

function UserInterface() {
  this.okButton = Button(300, 10, 90, 26); {
    this.okButton.title  = 'OK'
  }
  this.cancelButton = Button(210, 10, 90, 26); {
    this.cancelButton.title  = 'Cancel'
    this.cancelButton.keyEquivalent = '\u{1b}'
  }
  this.window = Window(-1, -1, 400, 225); {
    this.window.title = 'Untitled'
    //this.window.frameAutosaveName = 'main'
    this.window.contentView.addSubview(this.okButton)
    this.window.contentView.addSubview(this.cancelButton)
    this.window.defaultButtonCell = this.okButton.cell
  }
  return this
}

App = Application.currentApplication()
App.includeStandardAdditions = true

MyAction = SimpleSubclass('Action', {
  hello(sender) {
    App.displayAlert('hello 😀')
  },
  quit(sender) {
    App.quit()
  },
})

MyUI = UserInterface()
MyUI.okButton.target = MyAction
MyUI.okButton.action = 'hello:'
MyUI.cancelButton.target = MyAction
MyUI.cancelButton.action = 'quit:'
MyUI.window.delegate = WindowDelegate()
MyUI.window.makeKeyAndOrderFront(null)

/*
function reopen() {
  MyUI.window.makeKeyAndOrderFront(null)
}

function run(argv) {
  MyUI = UserInterface()
  MyUI.okButton.target = MyAction
  MyUI.okButton.action = 'hello:'
  MyUI.cancelButton.target = MyAction
  MyUI.cancelButton.action = 'quit:'
  MyUI.window.makeKeyAndOrderFront(null)
}
*/

//=====================================================================

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