Applescript and "starts with" operator
Asked Answered
H

2

5

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.

Hunsinger answered 8/5, 2011 at 3:36 Comment(0)
G
9
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
Galbraith answered 8/5, 2011 at 20:42 Comment(3)
works great on its own Lri, but how can i implement it within this script. I'm not fluent in applescript at all. Heres a link to it on pastebin pastebin.com/mS3wyYQXHunsinger
@Hunsinger That would need to be another question. Here's something similar though: wrap_lines_with_tag.applescriptGalbraith
thanks lri, ill try to work with one you gave me, if it doesnt work out ill post another question related to the one i currently have. +1Hunsinger
S
8

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"
Sutherlan answered 11/5, 2011 at 7:8 Comment(1)
interesting info. i posted another question related to this problem in another page. @Galbraith was kind enough to create a tailor fit solution to the problem hereHunsinger

© 2022 - 2024 — McMap. All rights reserved.