2017-02-11 38 views
0

我執行使用的NodeJS這樣一個AppleScript應用:沒有輸出時的node.js調用AppleScript的應用

tell application "Final Cut Pro" to activate 
tell application "System Events" to keystroke "2" using command down 
tell application "System Events" 
    tell process "Final Cut Pro" 
     tell menu bar 1 
      tell menu bar item "File" 
       tell menu "File" 
        tell menu item "Export XML..." 
         click 
        end tell 
       end tell 
      end tell 
     end tell 
     tell its front window 
      click button "Save" 
      click button "Replace" of sheet 1 
     end tell 
    end tell 
end tell 

節點代碼從一個節點的webkit執行:

var child = require('child_process').exec('open myApp.app'); 

     child.on('exit', function (code) { 
      console.log('child process exited with code ' + code); 
     }); 
     child.stdout.on('end', function (data) { 
      console.log('end: ' + data); 
     }); 
     child.stdout.on('data', function (data) { 
      console.log('stdout: ' + data); 
     }); 
     child.stderr.on('data', function (data) { 
      console.log('stderr: ' + data); 
     }); 

AppleScript的(nwjs)應用程序。 Applescript應用程序在ScriptEditor中運行時以及在從nwjs應用程序調用時都可以工作。

我沒有得到任何東西在最後兩個事件處理程序。前兩個分別輸出:

undefined 
0 

通過有意寫錯誤的AppleScript代碼強制的錯誤不會導致在node.js的錯誤事件處理程序的任何輸出。我的問題是:我怎樣才能從AppleScript輸出回事件處理程序?改變下面的順序或者註釋掉前兩項並沒有幫助。

回答

0

您可以使用node-applescript。
首先用npm安裝它:$ npm install applescript
然後在你的JS文件中添加下面的代碼來運行你的腳本。

var applescript = require('applescript'); 


var script = 'tell application "iTunes" to get name of selection'; //you can copy and past your apple script here 

applescript.execString(script, function(err, rtn) { 
    if (err) { 
    // Something went wrong! 
    } 
    if (Array.isArray(rtn)) { 
    rtn.forEach(function(songName) { 
     console.log(songName); 
    }); 
    } 
}); 

節點AppleScript的文檔:https://github.com/TooTallNate/node-applescript

希望這有助於!

+0

我已經嘗試過該模塊,並且還嘗試了node-applescript,但是在第一次完全執行腳本之前需要10到15秒或更長時間,不知道爲什麼。將它放在一個applescript應用中要快得多,整個過程大約需要3秒鐘。 – jerry

+0

我的意思是[node-osascript](https://www.npmjs.com/package/node-osascript)作爲我嘗試的另一個。 – jerry

+0

好的,我不確定那是什麼問題:\抱歉。你也可以試試node-applescript,如果你還沒有。 – pudility

相關問題