Reputation: 12105
In order to store an object's values for external (outside AS) access, I need to be able to get every property of that object, and then I'd try to coerce it to text and store it somehwere.
How do I get the list of properties that an object holds. As an example, I can write this:
tell me
get properties
end tell
That works for a script object.
But for many other objects, I simply get an error such as "descripter type mismatch", like here:
tell application "iTunes"
get properties of file track 1
end tell
Now, I know the excellent Script Debugger can do it (it can show any object's entire set of properties), so it should be possible in written AppleScript as well. What's the secret to this?
Upvotes: 13
Views: 37280
Reputation: 1878
Many words are said here, but the main thing is not said. Why properties might work for some objects but might not work for others?
So, whether a Cocoa application is scriptable or not, it responds to commands and properties from the Standard Suite. In the Standard Suite, the properties property is a property of the "item" class of the Standard Suite and is available for objects (items) of the following classes:
application, attribute run, character, word, paragraph, color, text, document, window
In addition to the objects listed above, some scripted applications have an additional properties property, which is a property of the "item" class of the Application's Suite. The availability of this property depends on the specific application. For example, the Music.app application, in addition to the objects listed above, allows you to request for the properties of the following objects:
Airplay device, artwork, encoder, EQ preset, playlist, source, track, visual.
Finder.app has additional properties property of "item" class of Finder items suite + properties property of "item" class of Windows classes suite.
That is, the answer is this: you can get in the script the properties of those objects that the application allows in the Standard and additional suites together.
Upvotes: 0
Reputation: 9
https://i.sstatic.net/9FTz0.jpg
info for
is "deprecated" but has some useful properties like alias
and type identifier
Upvotes: 0
Reputation: 12105
Mark Alldritt, the author of Script Debugger, was so kind to explain the "secret" to me.
Script Debugger uses some special AppleScript API functions, including OSAGetPropertyNames(), to get to this information.
Hence, if I write a wrapper in, for instance, C, I can probably get to this, too.
Update
The Cocoa Scripting API has a dedicated classes for this (NSScriptSuiteRegistry
and NSScriptClassDescription
) - the framework builds this information from reading an app's scripting definition (.sdef) file. With that, all the available class and their properties can be learned quite easily.
Upvotes: 9
Reputation: 1347
The ability for an app to return a "properties" property has always existed, but it took considerable more work pre-Cocoa than after. Pre-Cocoa, the developer had to build an AEList structure populated with keys and values for each property, then return it in an typePropertyList descriptor. Many developers did not bother. With Cocoa Scripting, you basically get this for free AS LONG AS you use KVC-compliant names for all of the properties of your class AND you get the terminology and cocoa-keys in your SDEF file properly configured.
BTW, in 2016, iTunes 12.3.3,
tell application "iTunes" to get properties of file track 1
correctly returns a long list of properties.
Upvotes: 4
Reputation: 51
There is a trick you can use, because you can force Applescript to tell you the error, and this text includes the properties of the object that was the target.
set myThing to {FirstName:"Fred", LastName:"Smith"}
ListProperties(myThing)
on ListProperties(MyObject)
try
get properties of MyObject
on error errText number errNum
set pStart to offset of "{" in errText
set structure to text pStart thru ((length of errText) - 2) of errText
set TIDL to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set fields to text items of structure as list
set myMessage to ""
repeat with f from 1 to count of fields
set AppleScript's text item delimiters to ":"
set theseItems to text items of (item f of fields) as list
set itemPropName to text 2 thru length of item 1 of theseItems
set itemValue to item 2 of theseItems
set myMessage to myMessage & "Property Label: " & itemPropName & tab & "Value: " & itemValue & linefeed
end repeat
set AppleScript's text item delimiters to TIDL
display dialog myMessage
end try
end ListProperties
Upvotes: 5
Reputation: 5055
Script Debugger is Applescript, just with a bunch of programming tools placed around it. But a "descriptor type mismatch" really shouldn't enter into it. Can you show your code, because this works just fine in Script Editor:
tell application "Finder"
set theFile to choose file
get properties of theFile -- the "return" keyword also works here as well
end tell
Different applications will behave different, but without example code, there are too many variations to say definitively.
Update per comment and updated question:
Again, different applications behave differently. An application actually has to have a properties
property in order to get a record returned to you (though sometimes this is different from other information that can be gained from an object). Typically, this is implemented at a root class—item
in most cases; iTunes doesn't allow for this. Not even Script Debugger can get around that.
Upvotes: 6