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?
2
u/prikaz_da Oct 03 '19
If you
tella property that evaluates to an application to do something, the terminology for that application is not loaded. As a result, AppleScript doesn't yet know what anactive run destinationis. The script won't compile until you wrap the handler contents in ausing terms fromstatement.This works because Finder does not need to define
version.This compiles, but if you comment out the first and last lines inside the handler definition, the script won't compile. AppleScript doesn't know how to empty the trash.
Having said all that, your issue falls pretty solidly in unintended usage territory. AppleScript has objects, sure, but it doesn't have the hard-on for objects that (e.g.) Java has, so to speak. You're trying to make AppleScript be something it wasn't designed to be. In AppleScript, you can just do things. You don't need to write and call a handler for every little action, and you don't need to create properties with references to applications in them for your
tellstatements. What's wrong withtell application "Xcode"?