0

I have exactly the same problem as in this question but I don't know how to apply that solution to my situation. My JavaScript looks like this:

var result = [];
var xpathResult = document.evaluate('" & xpathExpression & "', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node;
while (node = xpathResult.iterateNext()) {
    result.push(node);
}
result;

and when I execute it in the browser console it returns a string (or null) but how do I get that string to be returned to my AppleScript?

I have an simpler version of the JavaScript

var result = document.evaluate(''" & xpathExpression & "', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
result.singleNodeValue"

but it has the same problem - no return value. I can even do an alert() and the string is displayed in the browser but it seems impossible to get the string back to the AppleScript.

5
  • The other question's answer implies that the last global variable gets returned. So result needs to be declared without var. Commented Feb 11, 2024 at 0:09
  • @James Removed all var from the code, but it made no difference. Commented Feb 11, 2024 at 0:38
  • Removing all var from the first sample will definitely not work. You need to only remove var from the declaration of result. The final line, result; - doesn't make sense by itself in a javascript sense, try removing that too. Commented Feb 11, 2024 at 0:40
  • did you get it to work? I'm having a similar problem at stackoverflow.com/questions/67957456/… Commented May 28, 2024 at 14:57
  • @nyxaria Yes I think so. I have added an answer. Please test and confirm. Commented May 28, 2024 at 17:13

1 Answer 1

1

I think I got it to work:

set JSGetAllEvent to "var items = document.evaluate('" & xpAllEvents & "', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); items.snapshotLength"

and then

set eventCount to do JavaScript JSGetAllEvent
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.