rubymacosapplescriptkeyboard-maestrosourceforge-appscript

Rewriting AppleScript to appscript-rb, setting variable in Keyboard Maestro


I need to set a variable in Keyboard Maestro, and the documentation states that this can be done with AppleScript:

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

I'm trying to transform this to appscript-rb notation, so far I've got

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

I've documented a lot of successful snippets here: http://reganmian.net/wiki/appscript, and many of them follow the pattern above, but this snippet does not work, it gives "unknown keyword parameter name".


Solution

  • You have the syntax of your command wrong (hard to imagine with AppleScript's clear and well defined syntax, I know!).

    The command should be something like:

    #!/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"});
    

    I found this draft book of Scripting Mac Applications With Ruby helpful in figuring out how to translate the AppleScript code to ruby.

    BTW, if you know the variable already exists, it's easier to use just the simple reference get/set commands:

    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;