Question

I would like to do this:

tell application "Finder" to set appName to (application file id "com.google.Chrome") as text
using terms from application appName
    tell application appName to get URL of active tab of first window
end using terms from

This doesn't work because "using terms from" requires an application name as a string constant. If i substitute this line:

using terms from application appName

with this one

using terms from application "Google Chrome"

it works. However I don't want to rely on the target machine having the application named "Google Chrome". Using the bundle identifiers seems safer. Is there a better way to do this?

Edit

Following @regulus6633's advice I've tried the following:

NSAppleScript* script = [[NSAppleScript alloc] initWithSource:
    @"tell application \"Finder\" to set appName to (application file id \"com.google.Chrome\") as text"
    @"\nusing terms from application \"Google Chrome\""
    @"\ntell application appName to get URL of active tab of first window"
    @"\nend using terms from"];

Which works fine, but if I run the same (compiled) app on a different computer where "Google Chrome" is renamed "Chrome," I get a popup dialog asking where "Google Chrome" is. It seems as if NSAppleScript is compiled at runtime?

Was it helpful?

Solution

You're misunderstanding what "using terms from" does. This is a way for you to compile a script on your computer using an application and then not have the script be recompiled on a user's computer. In other words, once you compile on your computer with that line of code, then that line of code does nothing on a user's computer, and thus the user does not need the application you used to compile the script... so that line is exactly what you're looking for. Make sure to save your script as an "application" so it doesn't need to be recompiled on the user's computer.

This is what you actually want your code to look like:

-- here you determine what the user's browser is
set usersBrowser to "whatever"

using terms from application "Google Chrome"
    tell application usersBrowser
        -- here you do something in the user's browser
        -- you have to make sure that whatever command you use is applicable to both google chrome and whatever browser the user is using i.e. the command must work in both browsers
    end tell
end using terms from
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top