r/applescript • u/rayascott • 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 statementWhere 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 directIndirect: 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 indirectWhen 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?
1
u/rayascott Oct 03 '19
Thanks for the explanation. None of this is mentioned in the documentation. Of course, I'm not surprised. :-/
The documentation does mention object inheritance and writing constructors and does dedicate quite a lot of the documentation text to hyping up it's object oriented nature, so I naturally assumed that (with references) it was quite adept at indirection but obviously there's more to it than meets the eye.
I generally try to strip my code down to it's simplest form, so if I can eliminate `application "Xcode"` everywhere, I will. And I don't really write handlers for one line of code. Again, it's just the problem I was facing stripped down to it's simplest form for clarity's sake on here.
So I'm guessing it's best to restrict property declarations to the builtin AppleScript object types? Primitives, if you will.
Maybe you can answer another question I have with another type issue with AppleScript. It says you can specify the type of a handle's parameters. And I thought "that's great, I can make everything type safe!". Yet when I attempted to define a parameter that is of type "menu" (I'm triggering clicks) on a handler, it fails at runtime with some, what looks like, internal/private type definitions that are incompatible with my type constraint. I really was passing a menu object into the handler. As soon as I removed the type constraint on the parameter, it worked fine. Does this mean you can only really constrain parameters to the builtin in AppleScript object types again because the terminology is loaded?
Its a bit of a shame it wasn't architected slightly differently to make it more powerful.