【问题标题】:Running AppleScript from XPCOM从 XPCOM 运行 AppleScript
【发布时间】:2014-04-03 08:34:48
【问题描述】:

我一直在尝试通过 XPCOM 执行 applescript 代码,但无论我做什么,它似乎都不起作用,观察者告诉我该过程没有问题地完成,但没有任何反应。

var processArgs = ['-e','\'tell application "iTunes"\'','-e','\'activate\'','-e','\'end tell\''];

var file = Cc["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
file.initWithPath( '/usr/bin/osascript');

var process = Cc["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
process.init( file );

var observer = {
    observe: function( subject, topic ) {
        SiteFusion.consoleMessage('DONE ' + topic);
    }
}

process.runAsync( processArgs, processArgs.length, observer );

控制台中的输出是“DONE process-finished”,所以它应该可以工作,有谁知道为什么这个脚本拒绝打开 iTunes?

【问题讨论】:

    标签: applescript xpcom


    【解决方案1】:

    原因是您在参数中使用了转义的单引号,因此实际生成的 AppleScript 如下所示:

    'tell application "iTunes"'
    'activate'
    'end tell'
    

    这显然是无效的。相反,您的论点应如下所示:

    const processArgs = ['-e', 'tell application "iTunes"', '-e', 'activate', '-e', 'end tell'];
    

    或者,或者,

    const processArgs = ['-e', `
        tell application "iTunes"
            activate
        end tell`];
    

    或者,或者,

    const processArgs = ['-e', 'tell application "iTunes" to activate'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多