How Do I Get and Set KM Variables in Browser JavaScript?

###How Do I Get and Set KM Variables in Browser JavaScript?
2016-06-07 00:44 CT (Tue)

This is very frustrating.  I have searched both the KM wiki and forum, and have been unable to find this common need.  The KM wiki is of NO help:
[Using Variables in Scripts](https://wiki.keyboardmaestro.com/Variables#Using_Variables_in_Scripts)

The KM "Documentation" states:
> JavaScript in web browsers can **access** the variable values by using the document.kmvar dictionary, like document.kmvar.Variable_Name (spaces are converted to underscores).

What does "access" mean?  Read Only, or Read/Write?
I usually take it to mean Read/Write unless explicitly stated otherwise.

But I don't see any way to SET a KM Variable from a Browser JavaScript.

This test did NOT work (the KM variable was NOT created, nor was it updated if it existed):

```javascript
document.kmvar.TEST_from_JS = 'Set by Browser JavaScript'
```

<img src="/uploads/default/original/2X/3/3d019b03cea14c4d35fb5c2fddd6481ff65a5ed5.png" width="495" height="135">

The whole KM Wiki sections on scripting and variables needs a major review/overhaul.  But I don't have the facts.  @peternlewis, can you please provide?

Thanks.

Yes, access means readonly.

There is no way to set a Keyboard Maestro variable from a browser JavaScript.

The only output from a browser JavaScript is the result, which you can then parse into parts if desired. There is no other way to get variables set from JavaScript in a browser (as opposed to JXA which can set variables easily).

Yes, it is on my list of things to do as part of the move of the documentation to the wiki. Part of unifying the Scripting and manual/Scripting pages involves overhauling the contents in both of them.

1 Like

So, I suppose the solution is to use a JXA script as the primary script, issue Browser JavaScript from there, and process the output into KM variables.

I'm sure I can figure out, but any examples would be great.

@peternlewis, using your updated info, and one of my real world examples, I have updated the KM Wiki:

See Execute JavaScript in Browser

Please advise and/or correct if you find any errors or omissions.

1 Like

Wiki additions look good.

So, do I understand from this thread that I cannot successfully accomplish the teal colored macro action below? If not, can one of you please demonstrate how to get the results from Safari web page into the targeted KM variable? I'm not actually familiar with JavaScript, so I may simply have the syntax incorrect.

Keyboard Maestro “Get Drive Time From Safari TravelMath Test Macro” Macro

Get Drive Time From Safari TravelMath Test Macro.kmmacros (5.9 KB)

Change it to this: [edited]

document.getElementById('drivetime').innerText;

You need to use “.innerText” to get the content.

1 Like

Dan, are you sure about this? I'm a real neophyte here, so I'm really asking for information.

I found that this works:

So, I'm not sure when we need a function() in the KM Execute JavaScript in Safari, and when it is not necessary.

You know more than I do in this area. This was the first time I ever tried it.

So my answer is “No, I’m not sure.” Here’s what happened:

I changed the original macro to use “return”, thinking that might be the problem. And now that I think about it, you probably can’t use “return” without being inside a function. So that’s what led to the use of a function. I finally figured out the problem was the missing “innerText”, and at that point it worked, so I figured everything else was necessary also.

I love troubleshooting. :stuck_out_tongue:

1 Like

Edited my answer. Thanks!

Dan & JMichaelTX,

That modification worked!!

Thank you very much

1 Like

Great! Please pick Dan's post as the one that "Solved" your question.

JMichaelTX,

I’m sorry, but I don’t see how I can mark his post as the one that solved my question.

See:

I hate to be so "thick" about this, but I don't see how to select a check mark on Dan's post to indicate my query was solved by his post. See screenshot:

I don't see an ellipsis as noted by CCStone.

It's already marked as "Solved". Not sure why or how, but here it is:

So there's nothing left to do!

Dan,

Oh duh, I now see the “Solved” check mark on the original post!! I previously was only looking at your post back to me and trying to see how I could mark your post as having solved my question. I now better understand JMichaelTX’s recommendation and that one can only mark the original poster’s message as having been solved.

Thanks

Here's a reply to a topic I started:

See the checkbox? If I move my mouse over it like this:

I can click it and mark this reply as the answer to my question. That's how it works.

The main reason you want to do this is for questions that someone else might come across in a search, and if they see that your question was answered, then they know 3 things:

  1. Someone on the forum actually responded to your question, so they know questions don't get ignored.
  2. If they have the same question, they know there's an answer, and...
  3. The answer is in the topic somewhere.

Dan,

I understand and appreciate those points regarding the use and benefit. However, when I go to the forum post from you regarding my question, there’s not box to check! I can, however, see the checked box on the original post from JMichaelTX and that Peter solved his question. I’m wondering if my pinned forum Safari window is a problem or my connection to the forum is somehow different or something else.

Oh, I am an idiot. I didn’t go to the first post. Dang, sorry for the confusion.

You didn’t start this topic. Only the person who starts the topic can mark it “answered”. Sheesh. At least now I understand your confusion!

Again, sorry about that. :blush: