在 AppleScript 和 JavaScript 中循环遍历列表
Loop through list in AppleScript and JavaScript
我目前正在使用 AppleScript 自动执行 Safari 中的一些任务。我有以下代码,它应该将文本框“ownerValue”的值设置为循环中的项目。每当我执行代码时,什么也没有发生。
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = country"
end tell
end tell
end repeat
当我将循环中的国家替换为实际国家值时,例如“BR”,然后将文本插入文本字段。
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = ‘BR'"
end tell
end tell
AppleScript 似乎也没有将国家/地区识别为循环中的项目,因为国家/地区不是绿色的。
关于如何解决此问题以便让 AppleScript 循环遍历 countryList 中的值的任何想法?
谢谢!
这将解决 变量 未被视为 变量 的问题:
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = '" & country & "';"
end tell
end tell
end repeat
但是,您在这里可能还有另一个问题,因为您在将新的变量的值输入到同一个输入字段。换句话说 JavaScript command 将首先输入 BR 并立即用 CN.
覆盖
请注意,上面代码中 country
的颜色并未显示为 绿色 ,但是,这是来自 Script Editor 的截图 并且您看到它是 绿色。
解决评论:
Could you please explain why it was previously not treated as a variable but now is?
当混合两种语言时,即本用例中的 AppleScript 和 JavaScript,并传递 AppleScript 变量到JavaScript,你需要构建出JavaScript command 使用串联,因此 variable 的 value 被扩展。
在您的代码中,它被视为一个固定的 值,因为 JavaScript 无法知道它是一个AppleScript 变量,因此连接,即& ... &
。在 AppleScript 中,&
是 连接字符 .
我目前正在使用 AppleScript 自动执行 Safari 中的一些任务。我有以下代码,它应该将文本框“ownerValue”的值设置为循环中的项目。每当我执行代码时,什么也没有发生。
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = country"
end tell
end tell
end repeat
当我将循环中的国家替换为实际国家值时,例如“BR”,然后将文本插入文本字段。
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = ‘BR'"
end tell
end tell
AppleScript 似乎也没有将国家/地区识别为循环中的项目,因为国家/地区不是绿色的。
关于如何解决此问题以便让 AppleScript 循环遍历 countryList 中的值的任何想法?
谢谢!
这将解决 变量 未被视为 变量 的问题:
set countryList to {"BR", "CN"}
repeat with country in countryList
tell application "Safari"
activate
tell document 1
do JavaScript "document.getElementById('ownerValue').value = '" & country & "';"
end tell
end tell
end repeat
但是,您在这里可能还有另一个问题,因为您在将新的变量的值输入到同一个输入字段。换句话说 JavaScript command 将首先输入 BR 并立即用 CN.
覆盖请注意,上面代码中 country
的颜色并未显示为 绿色 ,但是,这是来自 Script Editor 的截图 并且您看到它是 绿色。
解决评论:
Could you please explain why it was previously not treated as a variable but now is?
当混合两种语言时,即本用例中的 AppleScript 和 JavaScript,并传递 AppleScript 变量到JavaScript,你需要构建出JavaScript command 使用串联,因此 variable 的 value 被扩展。
在您的代码中,它被视为一个固定的 值,因为 JavaScript 无法知道它是一个AppleScript 变量,因此连接,即& ... &
。在 AppleScript 中,&
是 连接字符 .