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

9
  • Does this answer your question? tell application - string vs. string? Commented May 29, 2021 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. Commented May 29, 2021 at 17:05
  • Because activate is a global command which every application responds to, even if it lacks an AppleScript dictionary. Commented May 29, 2021 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. Commented May 29, 2021 at 17:16
  • And, is it only the tell application that has this problem? Commented May 29, 2021 at 17:18

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.