0

I sometime use Safari, sometime Safari Technology Preview, I was thinking that would be nice to just refer it once in a variable a bit like this

set SafariBuild to "Safari Technology Preview"

tell application SafariBuild to ¬
    do JavaScript ¬
        "document.querySelectorAll(\"[class=\\\"icon-chevron-left\\\"]\")[0].click();" in tab 2 of window 1

Obviously that's not working, but is there something in the same spirt that would refer to an app without changing all the tell application ?

2
  • The target for the tell statement is needed at compile time.
    – red_menace
    May 4, 2019 at 19:51
  • 1
    On a side note, I have found that using a single-quote ' inside if the double-quotes " of do JavaScript " ... ;" in many cases will keep from having to use \\\". You might want it give it a try, as all of that escaping with multiple backslash is such a pain. May 4, 2019 at 23:20

1 Answer 1

1

As already mentioned in a comment by red_menace, "The target for the tell statement is needed at compile time."; however, here is an alternative method:

Example AppleScript code:

set SafariBuild to "Safari Technology Preview"

if (SafariBuild's contents = "Safari") then
    tell application "Safari" to do JavaScript ¬
        "document.querySelectorAll(\"[class=\\\"icon-chevron-left\\\"]\")[0].click();" in tab 2 of window 1
else if (SafariBuild's contents = "Safari Technology Preview") then
    tell application "Safari" to do JavaScript ¬
        "document.querySelectorAll(\"[class=\\\"icon-chevron-left\\\"]\")[0].click();" in tab 2 of window 1
end if


Or you could use choose from list, as in this example AppleScript code:

choose from list {"Safari", "Safari Technology Preview"}
if result is not false then
    set SafariBuild to result as text
else
    return
end if

if (SafariBuild's contents = "Safari") then
    tell application "Safari" to do JavaScript ¬
        "document.querySelectorAll(\"[class=\\\"icon-chevron-left\\\"]\")[0].click();" in tab 2 of window 1
else if (SafariBuild's contents = "Safari Technology Preview") then
    tell application "Safari Technology Preview" to do JavaScript ¬
        "document.querySelectorAll(\"[class=\\\"icon-chevron-left\\\"]\")[0].click();" in tab 2 of window 1
end if



Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

1
  • I was just wondering if it was possible without if statement but there is thousand of lines in my script, I'll just go with a search / replace for now then. Thank you
    – Kevin
    May 5, 2019 at 5:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .