Applescript and “starts with” operator

2019-07-15 21:23发布

Is there a way to check (in applescript) if a list (or block of html text) starts with any number of values.

Example (checking for a single value)

if {foobar starts with "<p>"} then
    -- do something awesome here
end if

except i would like to pass multiple values to check <p> or <h1> or <em>.

Thanks in advance.

2条回答
迷人小祖宗
2楼-- · 2019-07-15 22:01

If you want to stay within the 'English' style of AppleScript, although longer than the example above, you can just do this:

if {foobar starts with "hello" or foobar starts with "goodbye"} then

A full example would be:

set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
    display dialog "found"
end if

That will be true even if you change:

set foobar to "hello dude"

to:

set foobar to "goodbye dude"
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-15 22:11
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
查看更多
登录 后发表回答