Safari Javascript works in Safari console, doesn't work when run from KM?

Hi, title say it, this is the code:

var myField = document.getElementById("search_query_field");
myField.value = "%Variable%searchTerm%";

Any suggestions? Many thanks, Terry

The problem is in the way in which you are trying to fetch a value from a KM variable.

See, for example: Execute Javascript in Safari using KM variable (SOLVED)

Thanks for the tip, no luck though with the following. I presume I'm still doing something stupid, but can't see what.

var myField = document.getElementById("search_query_field");
myField.value = eval(document.kmvar.searchTerm);

I don't think you will need the first line of that. (Object.keys etc)

and eval isn't what you need there either.

As long as there is a value in a KM variable named 'searchTerm', you should, I think, be able to obtain that value, as a string, by writing just:


Thanks, still no luck with:

var myField = document.getElementById("search_query_field");
myField.value = document.kmvar.searchTerm;

If I put the a value for the var (see below), and paste into console in Safari, the following works fine.

var myField = document.getElementById("search_query_field");
myField.value = "beep";

I've checked that the var searchTerm is getting correctly populated in KM.

Any further ideas?

Thanks, Terry

To be of further help, we need from you:

  1. URL of Page, or a broad section of HTML code for the page around the target area
  2. Your macro -- please upload your macro

Hi, thanks.

url is:

Macro attached.Search Pinboard.kmmacros (3.7 KB)

Your macro worked fine for me (I deleted the redundant second action, but even that did not stop the macro from working).

Hi Peter, thanks for looking into this. Gah! I guess that's good. Any idea how I might be able to debug what's going on at my end? If it runs fine in the Safari Console, and the syntax is ok, any idea what might be preventing the communication between KM and Safari?

Thanks, Terry

Put this at the top of your JavaScript:

That will invoke the Safari JavaScript debugger when the script is run.

Before you run it, open the web page and the Safari JavaScript console.
When the JavaScript is run, it will pause in the JavaScript Console, and you can step through it to see what happens.

For anyone else struggling with this, checking the log, showed:

execution error: Safari got an error: You must enable the 'Allow JavaScript from Apple Events' option in Safari's Develop menu to use 'do JavaScript'

Of course, doing this, and it worked fine.