uchcode
6/3/2016 - 3:18 AM

NSApp.servicesMenu in JXA

NSApp.servicesMenu in JXA

#!/usr/bin/osascript -l JavaScript

ObjC.import('Cocoa')

function MainMenu() {
    
    function MenuItem(title, action, key) {
       return $.NSMenuItem.alloc.initWithTitleActionKeyEquivalent(title, action, key)
    }
    
    function MenuItemSeparator() {
       return $.NSMenuItem.separatorItem
    }
    
    const appName = ''//$.NSProcessInfo.processInfo.processName.js
    
    const mainMenu = $.NSMenu.new
    
    const itemApp  = $.NSMenuItem.new
    const itemFile = $.NSMenuItem.new
    const itemEdit = $.NSMenuItem.new
    mainMenu.addItem(itemApp)
    mainMenu.addItem(itemFile)
    mainMenu.addItem(itemEdit)
    
    const menuApp  = $.NSMenu.alloc.initWithTitle(appName)
    const menuFile = $.NSMenu.alloc.initWithTitle('File')
    const menuEdit = $.NSMenu.alloc.initWithTitle('Edit')
    itemApp.submenu  = menuApp
    itemFile.submenu = menuFile
    itemEdit.submenu = menuEdit
    
    NSApp.servicesMenu = $.NSMenu.new
    const itemAppServices = $.NSMenuItem.new
    itemAppServices.title = 'Services'
    itemAppServices.submenu = NSApp.servicesMenu
    
    const itemAppHideOther = MenuItem('Hide Others', 'hideOtherApplications:', 'h')
    itemAppHideOther.keyEquivalentModifierMask = $.NSAlternateKeyMask | $.NSCommandKeyMask
    
    menuApp.addItem( MenuItem(`About ${appName}`, null, '') )
    menuApp.addItem( /*--------------------------------------------------*/ MenuItemSeparator())
    menuApp.addItem( MenuItem('Preferences...', null, ',') )
    menuApp.addItem( /*--------------------------------------------------*/ MenuItemSeparator())
    menuApp.addItem( itemAppServices )
    menuApp.addItem( /*--------------------------------------------------*/ MenuItemSeparator())
    menuApp.addItem( MenuItem(`Hide ${appName}`, 'hide:', 'h') )
    menuApp.addItem( itemAppHideOther )
    menuApp.addItem( MenuItem('Show All', 'unhideAllApplications:', '') )
    menuApp.addItem( /*--------------------------------------------------*/ MenuItemSeparator())
    menuApp.addItem( MenuItem(`Quit ${appName}`, 'terminate:', 'q') )
    
    menuFile.addItem( MenuItem('New', 'newDocument:', 'n') )
    menuFile.addItem( MenuItem('Open...', 'openDocument:', 'o') )
    
    menuEdit.addItem( MenuItem('Undo', 'undo:', 'z') )
    
    return mainMenu
}

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

NSApp = $.NSApplication.sharedApplication
NSApp.setActivationPolicy($.NSApplicationActivationPolicyRegular)
NSApp.activateIgnoringOtherApps(true)
NSApp.mainMenu = MainMenu()
NSApp.run