Applescript and “starts with” operator

2019-07-15 21:53发布

问题:

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.

回答1:

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


回答2:

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"