【问题标题】:Rewriting AppleScript to appscript-rb, setting variable in Keyboard Maestro将 AppleScript 重写为 appscript-rb,在 Keyboard Maestro 中设置变量
【发布时间】:2020-08-08 18:56:21
【问题描述】:

我需要在 Keyboard Maestro 中设置一个变量,文档说明这可以通过 AppleScript 完成:

tell application "Keyboard Maestro Engine"
  make variable with properties {name:"My Variable", value:"New Value"}
end tell

我正在尝试将其转换为 appscript-rb 表示法,到目前为止我已经有了

Appscript.app('Keyboard Maestro Engine').
  make(:variable, properties={:name=>'var1', :value => 'val1'})

我在这里记录了很多成功的 sn-ps:http://reganmian.net/wiki/appscript,其中许多都遵循上面的模式,但是这个 sn-p 不起作用,它给出了“未知关键字参数名称”。

【问题讨论】:

    标签: ruby macos applescript keyboard-maestro sourceforge-appscript


    【解决方案1】:

    您的命令语法有误(我知道,AppleScript 的清晰且定义明确的语法很难想象!)。

    命令应该是这样的:

    #!/usr/bin/ruby
    
    require "rubygems";
    require "appscript";
    
    kme = Appscript.app('Keyboard Maestro Engine');
    kme.make(:new => :variable, :with_properties => {:name => "My New Variable", :value => "New Value 2"});
    

    我发现this draft book of Scripting Mac Applications With Ruby 有助于弄清楚如何将 AppleScript 代码转换为 ruby​​。

    顺便说一句,如果你知道变量已经存在,那么只使用简单的参考 get/set 命令会更容易:

    kme = Appscript.app('Keyboard Maestro Engine');
    p kme.variables["My Variable"].value.get;
    kme.variables["My Variable"].value.set("Next Value");
    p kme.variables["My Variable"].value.get;
    

    【讨论】:

    • 非常感谢,我有相当多的将 AppleScript 重写为 AppScript 的经验,但这一次让我很难过。
    【解决方案2】:

    您可以使用默认写入和默认读取将变量存储在您自己的 plist 中。

    【讨论】:

      【解决方案3】:

      所以我想要一个更优雅的答案,但根据 Phillipe Martin 的评论,这是一种可行的方法:

      defaults write reganmian.net.researchr prelude "export LC_ALL=en_US.UTF-8;export LANG=en_US.UTF-8;declare -x LANG=en_CA.utf-8"
      defaults write reganmian.net.researchr ruby "/usr/local/bin/ruby -KU"
      defaults write reganmian.net.researchr path "/Users/Stian/src/folders2web"
      

      然后

      将此添加到键盘大师“执行外壳命令”

      `defaults read reganmian.net.researchr prelude`
      `defaults read reganmian.net.researchr ruby` `defaults read reganmian.net.researchr path`/dokuwiki.rb image
      

      它可以工作,但看起来很笨拙。

      (有两件事可以让这件事变得更容易,一个是能够配置 Keyboard Maestro 用来执行 shell 命令的 shell - 这不是您的默认 shell,不尊重 ~/.profile,以及没有正确的代码页、路径等 - 另一个是允许在设置中定义任意变量,可以使用 %variable% 插入)。

      【讨论】:

      • 您可以使用 #!在脚本的开头。 shell 作为非交互式、非登录 shell 调用,因此 shell 不执行 .profile 或任何其他设置文件。请参阅“man sh”的 INVOCATION 部分。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2015-04-22
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多