How do you pass a variable to script and then get a dictionary value from the response?

I'm attempting to create a record, pass a variable, and open that record in Airtable. Where I'm running into issues is passing the variable to the record and then grabbing the record id after the response. I've read the using Keyboard Maestro variables section in this link but still can't seem to pass the variable. It just passes $KMVAR_Title, instead of what's in the variable and I also can't figure out how to grab a certain dictionary value from the response after the post, mainly the id: value.

I've posted the macro, script, and response below.

Any help would be greatly appreciated.

MACRO

SCRIPT

    curl -v -XPOST https://api.airtable.com/v0/app19kMDrY743YACk/Wiritng%20Log \
      -H "Authorization: Bearer YOUR_API_KEY" \
      -H "Content-Type: application/json" \
      --data '{
      "fields": {
        "Title":  "$KMVAR_Title",
        "Min": 10,
        "Max": 20
      }
    }'

RESPONSE

{"id":"recipUHnncOaDPd8l","fields":{"Title":"$KMVAR_Title","Min":10,"Max":20}

Most likely it is because the "$KMVAR_Title" is also between single-quotes, which will prevent all Bash variables, include KM Variables, from expanding.

You can try changing the syntax in the Execute Shell Script, but I'd suggest that you compose the entire command in a KM Variable, and then just pass that Variable to the Shell Script.
When you are using a KM Action to set a variable, you don't have to worry about quotes, single or double. You can reference a KM Variable by simply using the variable token, as in:
"Title": "%Variable%Title%",

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

==UPDATED==: 2019-05-04 14:10 GMT-5

==Do NOT use the below method -- it will NOT work.==
See:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's an example where I have replaced your curl command with echo, just to demo:

Example Results

image


MACRO:   Build Execute Shell Script Command as KM Variable [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/1/4/14e82d5b850a2c6ec4b24d6b05d8f1779d91a640.kmmacros">Build Execute Shell Script Command as KM Variable [Example].kmmacros</a> (2.4 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

![image|467x793](upload://nmbbwgUpwwtgx7VYLypfhpqJ8Nm.png)

That makes sense but for some reason it still won't work. I changed echo to curl in your example and entered the API key but it would not post nor give me a response.

I don't think it's because the variable is not expanding because does display with results with echo but after I change it to curl it doesn't do anything.

I have expanded the section on quoting strings on the wiki:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Quoting_Strings

The latter part of that should answer your question.

Basically replace the "$KMVAR_Title" with "'"$KMVAR_Title"'"

That is:

  • a double quote (which will be pasted to curl as part of the --data parameter)
  • a closing single quote which closes the single quoted string started after --data
  • an opening double quote which the shell will process
  • $KMVAR_Title which the shell will expand within this double quoted string
  • a closing double quote
  • an opening single quote
  • a double quote (which will be pasted to curl as part of the --data parameter)

@peternlewis, do you know why my method would not work?

Because splitting and variable interpolation and de-quoting happens at the same time - and so any quotes within a variable are meaningless.

For example:

% ls "a b" "c d"
ls: a b: No such file or directory
ls: c d: No such file or directory

The parameters to ls are:

  • ls
  • a b
  • c d

Where as:

% v='ls "a b" "c d"'
% echo $v
ls "a b" "c d"
% $v
ls: "a: No such file or directory
ls: "c: No such file or directory
ls: b": No such file or directory
ls: d": No such file or directory

Not what you want.

You can use eval:

% eval $v
ls: a b: No such file or directory
ls: c d: No such file or directory

But honestly, I don’t really understand what eval does so I don’t know why it works.

See: I'm trying to put a command in a variable, but the complex cases always fail!.

1 Like

Thanks, Peter.

Thanks for this reference. Very valuable. :+1:

@peternlewis what you suggested worked, I appreciate your help and the clear explanation.

@JMichaelTX I appreciate your help as well.