0

test_two() and test_three work(), but test_one() does not. Why is that? How do I pass the argument into the handler and use it as a local variable?

set the_application to "Safari"
test_one(the_application)
on test_one(the_application)
    tell application the_application
        make new document with properties {URL:"http://www.stackoverflow.com"}
    end tell
end test_one
# ----

test_two()
on test_two()
    tell application "Safari"
        make new document with properties {URL:"http://www.stackoverflow.com"}
    end tell
end test_two
# ----

set the_application to "Safari"
test_three(the_application)
on test_three(the_application)
    tell application the_application
        activate
    end tell
end test_three

NSGod
  • 22,699
  • 3
  • 58
  • 66
  • Does this answer your question? [tell application - string vs. string?](https://stackoverflow.com/questions/46854193/tell-application-string-vs-string) – vadian May 29 '21 at 16:57
  • I don't think so. In test_three I don't use a string literal for the argument in the 'tell application' statement, and it works fine. – Ryan B. Jawad May 29 '21 at 17:05
  • Because `activate` is a global command which every application responds to, even if it lacks an AppleScript dictionary. – vadian May 29 '21 at 17:10
  • Hmm. So, I can sometimes use a variable in the 'tell application' statement, and sometimes not, depending on what actions I use inside the tell block? So `activate` is a global command, but `make` is a ...local command? Is there a list of these somewhere? I have not been able to find very good documentation on applescript. – Ryan B. Jawad May 29 '21 at 17:16
  • And, is it only the `tell application` that has this problem? – Ryan B. Jawad May 29 '21 at 17:18
  • The global commands are `launch`, `activate`, `reopen`, `quit`, `version`, `is running` and `frontmost`. And yes, the reason is described in the linked answer. – vadian May 29 '21 at 17:23
  • Ok, thanks a lot. That gives me something to go off of. In the other answer, you mention that the `tell` statement is evaluated at compile time. Are there other statements for which this is true? – Ryan B. Jawad May 29 '21 at 17:33
  • Actually the entire code is going to be compiled. But the `tell application` statement is a special case because it evaluates the particular terminology of the target application therefore it cannot be a variable. And like in the other compiled languages for example you cannot create compound variable names. – vadian May 29 '21 at 17:42
  • I appreciate the help. Thank you. – Ryan B. Jawad May 29 '21 at 17:47

2 Answers2

1

Here is my other working solution, maybe, better than my first one:

set the_application to "Safari"
test_one(the_application)

on test_one(the_application)
    set theScript to "tell application \"" & the_application & "\"
make new document with properties {URL:\"http://www.stackoverflow.com\"}
    end tell"
    run script theScript
end test_one
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
0

When you want use some terms common to multiple applications, you can do it using terms from one of them. This terms will work for other applications as well. You can indicate in the code's first line the name of any application, which has command make new document with properties and property URL,

set the_application to "some Safari like application"
test_one(the_application)

on test_one(the_application)
    using terms from application "Safari"
        tell application the_application
            make new document with properties {URL:"http://www.stackoverflow.com"}
        end tell
    end using terms from
end test_one

Other example (creates new folder on the desktop):

set the_application to "Finder"
test_one(the_application)

on test_one(the_application)
    using terms from application "System Events"
        tell application the_application
            make new folder with properties {name:"TestFolder"}
        end tell
    end using terms from
end test_one
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • 2
    Note that other than a few basic terms from the Standard Suite, successful usage of terms and event codes between different applications would be entirely accidental/coincidental, as a developer can define these items at their whim (there are no standard naming conventions). – red_menace May 30 '21 at 03:19