Applescript和"开始于"运算符

Vic*_*rez 2 applescript list startswith

有没有办法检查(在AppleScript中)列表(或html文本块)是否有starts with任意数量的值.

示例(检查单个值)

if {foobar starts with "<p>"} then
    -- do something awesome here
end if
Run Code Online (Sandbox Code Playgroud)

除了我想传递多个值来检查<p><h1><em>.

提前致谢.

Lri*_*Lri 6

on startswith(txt, l)
    repeat with v in l
        if txt starts with v then return true
    end repeat
    false
end startswith

startswith("abc", {"a", "d", "e"}) -- true
Run Code Online (Sandbox Code Playgroud)


dig*_*ula 5

如果你想保持 AppleScript 的“英语”风格,虽然比上面的例子长,你可以这样做:

if {foobar starts with "hello" or foobar starts with "goodbye"} then
Run Code Online (Sandbox Code Playgroud)

一个完整的例子是:

set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
    display dialog "found"
end if
Run Code Online (Sandbox Code Playgroud)

即使您更改,这也是正确的:

set foobar to "hello dude"
Run Code Online (Sandbox Code Playgroud)

到:

set foobar to "goodbye dude"
Run Code Online (Sandbox Code Playgroud)