Re: accessing a property of an object in AppleScript
Re: accessing a property of an object in AppleScript
- Subject: Re: accessing a property of an object in AppleScript
- From: Paul Berkowitz <email@hidden>
- Date: Thu, 06 Nov 2008 19:37:06 -0800
- Thread-topic: accessing a property of an object in AppleScript
Title: Re: accessing a property of an object in AppleScript
It is a super basic question.
You should not be doing the first line as all-in-one-line if you're going to add more lines, since that completes the 'tell' block and anything that comes after is outside the System Events tell block. The only reason why 'name' works that way is that 'name' is a "reserved" word of AppleScript itself, so it compiles as a keyword. (It compiles as an AppleScript language keyword, but it functions as an application keyword since it understand System Events' 'name' property even outside the tell block.) Outside the tell block 'enabled' is no sort of keyword, so it compiles as a variable and has so meaning when you try to execute the line, so it errors. In fact 'properties' does too, so I think you must have give us a shortcut below and not what you actually scripted.
Anyway, there's no need to use 'get properties' when you only want one property - it just adds unnecessary complexity. This works fine:
tell application "System Events"
tell process "Safari" to set printMenuItem to menu item "Print…" of menu "File" of menu bar item "File" of menu bar 1
return enabled of printMenuItem
end tell
-- false
(It will also work with 'enabled of (get properties of prinMenuItem)' but why do it?)
--
Paul Berkowitz
From: Mark Sanvitale <email@hidden>
Date: Thu, 6 Nov 2008 19:05:50 -0800
To: AppleScript-Users <email@hidden>
Subject: accessing a property of an object in AppleScript
This should be a super basic question. Alas, I have spent many hours banging my head against this problem.
I am able to access an object (or element or whatever its official name is) for a menu item. Technically, I do this:
tell application "System Events" to tell process "Safari" to set printMenuItem to menu item "Print…" of menu "File" of menu bar item "File" of menu bar 1
This works. printMenuItem is a reference to the menu item I am after. The dictionary for System Events tells me that a "menu item" is a "UI element" and such things have a bunch of properties. If I execute "return properties of printMenuItem" I can see all the properties that should be there:
{position:{0, 280}, maximum value:missing value, name:"Print…", size:{259, 19}, subrole:missing value, class:menu item, minimum value:missing value, enabled:false, selected:false, role:"AXMenuItem", help:missing value, title:"Print…", value:missing value, entire contents:{}, description:"menu item", focused:missing value, orientation:missing value}
I want to access the enabled property. I eventually discover that I can access a property like so:
return name of (get properties of printMenuItem)
which correctly returns "Print…". Now, I replace name with enabled and I get an error. A few properties work in the above statement (e.g. class, size) but most do not, including my precious enabled property.
What am I doing wrong? Please help me.
You would think that asking such a basic question must mean that I have not attempted any search for the answer. Alas, I have read Apple docs on the AppleScript language, I have searched the mailing lists, I have done innumerable search variations on Google, and I have made more attempts to satisfy Script Editor and its inscrutable language requirements than I dare admit. What I will admit is that AppleScript makes me want to cry. You win AppleScript. You win.
Mark Sanvitale
(formerly believed to be a competent engineer)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden