2015-11-27 14 views
2

如果實驗讀/通過ObjC橋設置OSX輸入語言,寫作就像片段:如何從OS X JXA中讀取/強制CFArray和CFString等值?

(function() { 
    'use strict'; 

    ObjC.import('Carbon'); 
    ObjC.import('stdio'); 

    var sourceList = $.TISCreateInputSourceList(null, false); 

    var current_source = $.TISCopyCurrentKeyboardInputSource(); 

    var cfs = $.TISGetInputSourceProperty(current_source, $.kTISPropertyInputSourceID); 
    var cfn = $.TISGetInputSourceProperty(current_source, $.kTISPropertyLocalizedName) 

    var sourceCount = $.CFArrayGetCount(sourceList) 

    return $.CFArrayGetValueAtIndex(sourceList, 0) 

})(); 

我們很快就會CF類型的OBJ參考返回值。在ObjC本身,這些可以被強制爲NS值。任何關於如何在JavaScript中實現應用程序的感覺?

(我正在從我還沒有成功地提取字符串或其它原始值CF對象引用的返回值)

回答

3

可以強迫一個CF類型由第一一NS型再結合CFMakeCollectable功能因此,它需要「無效*」,並返回「ID」,然後使用該函數來執行轉換:

ObjC.bindFunction('CFMakeCollectable', [ 'id', [ 'void *' ] ]); 

var cfString = $.CFStringCreateWithCString(0, "foo", 0); // => [object Ref] 
var nsString = $.CFMakeCollectable(cfString);   // => $("foo") 

爲了更方便您的代碼中使用,可以定義在參考一.toNS()功能原型:

Ref.prototype.toNS = function() { return $.CFMakeCollectable(this); } 

這裏是你將如何使用這個新功能與TIS *功能:

ObjC.import('Carbon'); 

var current_source = $.TISCopyCurrentKeyboardInputSource(); 
var cfs = $.TISGetInputSourceProperty(current_source, $.kTISPropertyInputSourceID); 

cfs.toNS() // => $("com.apple.keylayout.US") 
+0

優秀。非常感謝 ! – houthakker