Sorry for the complete newb question. I looked around a little, but didn’t see a simple answer. I’m wondering why this doesn’t work:
tell application "iPhoto"
class of selection
end tell
--produces "iPhoto got an error: Can't make class of selection into type reference"
However, this works:
tell application "iPhoto"
set junk to selection
class of junk
end tell
--produces "list"
The fact that I’m using iPhoto is irrelevant - I’ve seen this sort of thing happen with many applications; iPhoto just seemed like a reasonable candidate. I figure this is some fundamental thing that I just never figured out. Thanks!
AppleScript: Applescript 2.0
Operating System: Mac OS X (10.5)
In this case, and in nearly every other similar instance, the problem you encounter involves what it returned by the command you issue. For instance, selection returns a list, even if only 1 photo is selected. To learn more details about that selection, you have to address it specifically:
tell application "iPhoto"
set a to selection
class of (a's item 1)--gets the information of the first item in the list, which is fine for a single selection
end tell
If you are unsure of how many items are selected, you need to loop through the selection:
tell application "iPhoto"
set a to selection
repeat with aa in a
display dialog (class of aa as Unicode text)
end repeat
end tell
Yes, it took me a while to notice the difference between a single item and a list of items in my first few scripts, but my current problem seems to be more fundamental. In this example, I’m not curious about the class of the first item in the selection, I’m curious about the class of the selection itself. Which I would expect to be “list”. But it doesn’t seem to be.
I guess another way to ask my question is: why does my script not work work when I ask iPhoto’s selections for its class, but work fine when I introduce an otherwise unnecessary variable?
The short of it is that you need to insert an explicit get command if you want to use selection in a compound expression like that: class of (get selection)).
Breaking it up into two statements (storing the selection in a variable and then asking for the class of the object in the variable) lets AppleScript insert an implicit get command exactly where the explicit one would need to go.
The longer details (as I understand them) are:
A) In the context of iPhoto, AppleScript parses the expression class of selection as a single reference form that is passed off to iPhoto as a query.
B) iPhoto treats the selection property as an primitive value, so it does not know how to get the class of it.
C) Adding the explicit get allows AppleScript to ask iPhoto for a reference it CAN handle (just selection).
D) AppleScript knows how to get the class of the resulting object.
If you happen to have Late Night Software’s List & Record Tools OSAX installed, the AEPrint of command thereof can give you a clue, but only if you can read the AppleEvent syntax (I can barley make any sense of it myself).
tell application "iPhoto" -- v 2.0.1 here!
set a to AEPrint of class of selection
set b to AEPrint of class of (get selection)
{|no get|:a, |explicit get|:b}
--> {|no get|:"'obj '{ 'form':'prop', 'want':'prop', 'seld':'pcls', 'from':'obj '{ 'form':'prop', 'want':'prop', 'seld':'selc', 'from':''null''() } }", |explicit get|:"'list'"}
-- pcls is «class pcls» i.e. class
-- selc is «class selc» i.e. selection (in iPhoto)
-- list is «class list» i.e. list
end tell
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)