使用 Applescript 查找两个常量之间的可变文本



我是Applescript的新手。我正在尝试通过 safari 将字符串从网页返回到变量。目标字符串会更改,但前后的单词是静态的。

示例:苹果 1293.34 USD

"

苹果"和"美元"永远不会改变,但我想要的只是介于两者之间的数字。

该元素没有 ID,我没有运气尝试使用 Javascript 上课。

谢谢:)

你应该首先找到你的元素。

var element = document.getElementById("elementID");

然后获取您的值:

 var value = parseFloat(element.innerText.split(" ")[1])

您可以尝试解析 Curl...

set myUrl to "http://stackoverflow.com/questions/18633200/finding-variable-text-between-two-constants-with-applescript"
set xxx to do shell script "curl " & quoted form of myUrl & " | grep -o Apple.*USD | sed 's/Apple\(.*\)USD/\1/'"

这是一个给定文本的苹果脚本方法。这并不假设苹果在文本中只被发现一次。这将查看并找到苹果后面跟着美元 2 个单词的位置,然后在它们之间获得适当的值。

set theText to "USD some words Apple USD more words Apple 1293.34 USD some words Apple USD"
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "Apple"}
set textItems to text items of theText
set AppleScript's text item delimiters to tids
set theValue to missing value
repeat with i from 2 to (count of textItems)
    set thisItem to item i of textItems
    try
        if word 2 of thisItem is "USD" then
            set theValue to word 1 of thisItem
            exit repeat
        end if
    end try
end repeat
return theValue