Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active July 25, 2018 09:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/8015438 to your computer and use it in GitHub Desktop.
Save kjunichi/8015438 to your computer and use it in GitHub Desktop.

試してみたいCocoa API

iCloud関連

Event Kit

ブロック使うAPIだった。

Core Location

AV Kit

Social Framework

音を鳴らす

var snd = $.NSSound('soundNamed',$('Hero'));
snd('play');

NSLogをファイル出力させるには

var fs = require('fs')
var logfile = __filename + '.log'

fs.closeSync(2)
var fd = fs.openSync(logfile, 'w')

ボタンを付けるには

// This example adapted from Matt Gallagher's "Minimalist Cocoa Programming"
// blog article:
//    http://cocoawithlove.com/2010/09/minimalist-cocoa-programming.html
var $ = require('../../../../')

$.import('Cocoa')

var pool = $.NSAutoreleasePool('alloc')('init')
  , app  = $.NSApplication('sharedApplication')

app('setActivationPolicy', $.NSApplicationActivationPolicyRegular)

menuBar('addItem', appMenuItem)
app('setMainMenu', menuBar)

var appMenu = $.NSMenu('alloc')('init')
  , appName = $('Hello NodeJS!')
  , quitTitle = $('Quit "' + appName + '"')
  , quitMenuItem = $.NSMenuItem('alloc')('initWithTitle', quitTitle
                                        ,'action', 'terminate:'
                                        ,'keyEquivalent', $('q'))
appMenu('addItem', quitMenuItem)
appMenuItem('setSubmenu', appMenu)

var styleMask = $.NSTitledWindowMask
              | $.NSResizableWindowMask
              | $.NSClosableWindowMask
var window = $.NSWindow('alloc')('initWithContentRect', $.NSMakeRect(0,0,200,200)
                                ,'styleMask', styleMask
                                ,'backing', $.NSBackingStoreBuffered
                                ,'defer', false)
window('cascadeTopLeftFromPoint', $.NSMakePoint(20,20))
window('setTitle', appName)
window('makeKeyAndOrderFront', window)
window('center')

var button = $.NSButton('alloc')('initWithFrame',$.NSMakeRect(0,0,78,32));
button('setBezelStyle',$.NSRoundedBezelStyle);
var view = window('contentView');
view('addSubview',button);

// set up the app delegate
var AppDelegate = $.NSObject.extend('AppDelegate')
AppDelegate.addMethod('applicationDidFinishLaunching:', 'v@:@', function (self, _cmd, notif) {
  console.log('got applicationDidFinishLauching')
  console.log(notif)
})
AppDelegate.addMethod('applicationWillTerminate:', 'v@:@', function (self, _cmd, notif) {
  console.log('got applicationWillTerminate')
  console.log(notif)
})
AppDelegate.addMethod('myMethod:','v@:@',function (self,_cmd,notif) {
    console.log("oops!");
});

AppDelegate.register()

var delegate = AppDelegate('alloc')('init')
app('setDelegate', delegate)
//var button = $.NSButton('alloc')('initWithContentRect', $.NSMakeRect(0,0,20,20))
button('setTarget',delegate)
button('setAction',$.NSSelectorFromString($('myMethod:')))
button('setTitle',$("Hello"))

app('activateIgnoringOtherApps', true)
app('run')

Cocoa API関連のお勉強

fsモジュールが使えない

NodObjC固有の問題か? -> 直前で例外が発生していて、実行していると思ったfs.writeSyncが動いていなかっただけだった。orz

applicationDidFinishLaunching

sharedApplication

fsモジュールがモジュールモジュール

ブロック構文が開発中とはいえ使えるみたいなので

MacRubyで挫折したJavaScriptCoreの利用ができるのではなかろうか?

なにをもってCocoaアプリなのか?

自前のアイコン持っていればOK?!

通知センターを使うまで

残念ながらnode本体をCocoa化?してそこから動かさないと通知センターを取得できず、動かせない模様(2013年末)

var myNotification = $.NSUserNotification('alloc')('init')
myNotification('title',$("Hello, world!"))
myNotification('informativeText',$("Nice to meet you."))
$.NSUserNotificationCenter('defaultUserNotificationCenter')('deliverNotification:',myNotification)
delegate('hide')

参考コード

MacRuby

myNotification = NSUserNotification.alloc().init()
        myNotification.title = "Hello, world!"
        myNotification.informativeText = "Nice to meet you."
        NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(myNotification)
        
        #NSApp.terminate(self)
        NSApp.hide(self)

Obective-C

NSUserNotification *myNotification = [[NSUserNotification alloc] init];
myNotification.title = @"Hello, world!";
myNotification.informativeText = @"Nice to meet you.";
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:myNotification];

Link

AppleScriptに逃げる

display notification “無理やり通知する!” with title “node-webkit” subtitle “applescriptで”
// create an NSString of the applescript command that will be run
var command = $('display notification “無理やり通知する!” with title “node-webkit” subtitle “applescriptで”')

// create an NSAppleScript instance with the `command` NSString
var appleScript = $.NSAppleScript('alloc')('initWithSource', command)

// finally execute the NSAppleScript instance synchronously
var resultObj = appleScript('executeAndReturnError', null)

Link

関連記事

関連Gist

アクセス解析タグ

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