0

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".

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Stian Håklev
  • 1,240
  • 2
  • 14
  • 26

3 Answers3

1

You can use defaults write and defaults read to store your variables in your own plist.

1

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;
Peter N Lewis
  • 17,664
  • 2
  • 43
  • 56
0

So I would love a more elegant answer, but based on Phillipe Martin's comment, this is one way that works:

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"

and then

adding this to the Keyboard Maestro "execute shell command

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

It works, but it looks very unwieldy.

(The two things that would make this easier, one is being able to configure the shell that Keyboard Maestro uses to execute shell commands - this is not your default shell, does not respect the ~/.profile, and does not have the right codepage, path, etc - and the other is allowing definition of arbitrary variables in the settings, which could be inserted with %variable%).

Stian Håklev
  • 1,240
  • 2
  • 14
  • 26
  • You can specify the shell to execute the same way you always would, using a #! at the start of the script. The shell is invoked as a non-interactive, non login shell, and so the shell does not execute the .profile or any other settings files. See the INVOCATION section of "man sh". – Peter N Lewis Apr 07 '13 at 06:10