问题
When an application uses the same name for a class and a property but different event codes, and I try to use the property in an object specifier, AppleScript interprets the identifier as the class rather than the property. For example:
tell application "Mail"
header of first rule condition of first rule
end
This results in the error:
Mail got an error: Can’t get header of rule condition 1 of rule 1.
The styling of header
in AppleScript Editor (blue italics) suggests it's a class name rather than a property. How can I specify the identifier is a property and resolve this naming collision explicitly?
I'm running OS X 10.6.8 and Mail.app 4.5.
Non-working solution
In "Applescript: The Definitive Guide", Matt Neuberg suggests that its
may be used:
The keyword
it
is needed when an application has defined a property with the same name as a class. [...] Sayingits
disambiguates.
However, this doesn't resolve the issue in my sample code above. After adding its
, header
is still styled as a class and the script results in the same error.
tell application "Mail"
its header of first rule condition of first rule
end
Applying the example from § 20.8.3. "Properties with Eponymous Classes" has the same result.
tell application "Mail"
tell first rule
tell first rule condition
get its header
end tell
end tell
end tell
Background
I'm attempting to write an AppleScript to extend Mail.app's rule conditions to support pattern matching. Some rule conditions in one of these extended rules are to contain information for the script, such as the pattern to match and the action to take if the pattern matches, rather than being conditions that Mail should match. I'd like to make use the header
property for these rule conditions.
Alternative ways of extending rules to allow for pattern matching are fine but not requested. I'd still like the particular question answered, since the issue can arise in cases other than this particular usage.
回答1:
Edit: I was able to get it to fail as you described by doing some rearranging. The Script Editor does seem to lock in to whatever its first guess was, so one solution would be to use run script to use the raw class at run time, for example:
tell application "Mail"
header of (get properties of first rule condition of first rule) -- fails
end tell
set myHeader to (run script "tell application \"Mail\"
return «class rhed» of (get properties of first rule condition of first rule)
end tell")
myHeader --> success
回答2:
From a comment on "Applescript et Mail.app, bug ou c'est moi", the header
property has a different event code than the class ("rhed" for the property, "mhdr" for the class). This appears to be both the actual cause of the error (header
compiles to «class mhdr»
) and offers a potential solution: you can use a raw event code designator to get the property in this particular case.
tell application "Mail"
«property rhed» of first rule condition of first rule
end
However, the first time you save the script, the raw code gets replaced by the name, and the second time you save the name is re-interpreted as a class rather than a property, requiring you to correct every occurrence of the property name each time you save. The amount of work to be done can be reduced by defining a handler to get the property outside of a block where Mail.app's terms are being used.
on hdr(rc)
return «property rhed» of rc
end hdr
tell application "Mail"
my hdr(first rule condition of first rule)
end
By defining the handler in a location where Mail.app's terms aren't being used, the raw code won't be replaced by an identifier.
This only partly solves the save-edit issue, as each separate property requires its own handler and (what's worse) handlers don't appear to work in filter forms (though loops could be used instead). As such, I cannot accept this answer and would appreciate a full solution, if someone can find one. As an example, the following
on hdr(rc)
return «property rhed» of rc
end hdr
tell application "Mail"
every rule condition of first rule where my hdr(it) is not ""
end
results in:
error "Mail got an error: Can’t get header." number -1728 from header
来源:https://stackoverflow.com/questions/9050273/how-can-i-access-a-property-that-has-the-same-name-as-class-but-different-event