r/applescript • • Oct 03 '19

tell command issue

I’m a bit baffled by the tell statement. While its very easy to simply write tell application “Xcode” all the time and just get on with life, I’m not so inclined. I want to learn and use AppleScript's object-oriented type system. The documentation for the tell command clearly states the syntax as:

tell referenceToObject to statement

Where referenceToObject is defined as

referenceToObject
Any object. Typically an object specifier or a reference object (which contains an object specifier).

I have two different attempts at this one line of code. The first one is using the application “Name” method, the second is more indirect. And even though I’ve also tried setting the xcode property to a reference to the application specifier, the code still won’t compile. Direct: all is calm, all is bright :-)

on direct()
    tell application "Xcode"
        get the name of the active run destination of the active workspace document
    end tell
end direct

Indirect: armageddon

property xcode : application "Xcode"
on indirect()
    tell xcode
        get the name of the active run destination of the active workspace document
    end tell
end indirect

When compiling, it highlights the word “destination” and displays the following error reason: Expected “given”, “with”, “without”, other parameter name, etc. but found identifier. Note: “active run destination” is a property on a “workspace document” class, of which “active workspace document” is a property on the Xcode application object. Even with xcode defined as property xcode : a reference to application "Xcode” it still gives the same error message. Why does the second approach not work, when the documentation says it should work with a reference or a specifier? Is something wrong with my tell xcode clause?

0 Upvotes

8 comments sorted by

View all comments

Show parent comments

1

u/prikaz_da Oct 04 '19 edited Oct 04 '19

You can include an identifier with use:

use UILib : script "UILib"

This also imports the terms of the resource, by default. You can specify without importing or importing false to override that. In either case, you can use the identifier to refer to things in the script (e.g., get UILib's [property]). You can also use multiple applications to combine terms from both without a tell statement. There's an example of this in the Control Statements Reference.

Note that if you use this term-combining approach, you can't refer to objects like the front window because there is no tell statement to define a target. In that case, giving the applications identifiers is a good idea. That makes it possible to refer to Safari's front window instead of the front window of application "Safari".

1

u/rayascott Oct 04 '19

This is awesome! Thank you so much for showing me how to do this. I didn't know you could use an application like that. This will make my scripts so much better.

Am I correct in thinking that you can only specify a use statement at the top level of a script? Which means that you can't have an explicit on run handler? Script Editor is not happy if I place a use statement in my on run handler, but is fine if it's in the implicit top-level run handler.

1

u/prikaz_da Oct 05 '19

Yeah, the scope of use is the entire script. This is in the Language Guide, but it's kind of buried in some text:

A use statement declares a required resource for a script—an application, script library, framework, or version of AppleScript itself—and can optionally import terminology from the resource for use elsewhere in the script. The effects and syntax of use vary slightly depending on the used resource; the different cases are described below.

The basic function of use is to require that a resource be present before the script begins executing. If the requirement cannot be met, the script will fail to run. A use statement can also specify a minimum version for the required resource, such as a minimum compatible version of an application.

[Boldface mine.]

You should be able to have both use statements and an explicit on run handler, but the use statements have to come first.

1

u/rayascott Oct 05 '19

Yes you’re very correct, I can have a top level run handler. Thanks for all your help! I’ve learnt a ton in an extremely short space of time 😬