0
tell application "Safari"
set theVar to (do JavaScript "
var a =
document.getElementById('unitsdiv').getElementsByClassName('even');
var b = []
for (var i = 0; i < a.length; i++) {
b[i] = a[i].textContent;
};
console.log(b.length); \\ Returns 2
console.log(b[1]); \\ Returns item 2 of Array
console.log(b[0]); \\ Returns item 1 of Array
b; \\ Shows both items in Safari Console
") in document 1
end tell
log theVar -- Missing Value
該代碼在Safari控制檯中按預期工作。我也試着在運行do Javascript命令之前初始化一個列表set theVar to {}
無濟於事。任何幫助,將不勝感激。爲什麼我的JavaScript不能將數組返回給AppleScript列表?
這似乎返回值之一
tell application "Safari"
tell document 1
set theVar to (do JavaScript "
var a = document.getElementById('unitsdiv').getElementsByClassName('even');
var b = [];
for (var i = 0; i < a.length; i++)
{ b[i] = a[i].textContent; };
console.log(b.length);
console.log(b[1]);
console.log(b[0]);
b[0];")
end tell
end tell
log theVar
如果我嘗試在javascript代碼的結束使陣列選擇[0]
關閉的b
然後告訴AppleScript的到log theVar
它拋出一個錯誤,那theVar
沒有定義。
愛的解決方案。爲我節省了大量的工作。 –