Re: Setting parameters in Address Book
Re: Setting parameters in Address Book
- Subject: Re: Setting parameters in Address Book
- From: has <email@hidden>
- Date: Tue, 12 Feb 2008 21:36:39 +0000
On Feb 11, 2008, at 6:34 PM, Christopher Nebel apparently wrote:
This last suggestion, and some similar ones in this thread, work
just fine. Thanks!!! Now what I need is to store property and
element names (not values) in variables, then use the contents of
the variables when setting or retrieving property and element
values. Thus:
set HerTitle to "Queen"
set MyProperty to "title"
tell application "Address Book"
set foo to person id michellesid
tell foo
set its MyProperty to HerTitle
end tell
end tell
Setting 'title' to HerTitle works.
Attempting to substitute MyProperty for 'title' doesn't.
That's harder, depending on exactly what you've got in mind. If you
know ahead of time the property you want, you can get a reference to
it:
set HerTitle to "Queen"
tell application "Address Book"
set MyProperty to a reference to title of person id michellesid
set contents of MyProperty to HerTitle
end tell
The trick is that that third line sets MyProperty to not the value
of the title property, but a specifier for the title property
itself. You can then pass that around, and set (or get) the value
by referring to its "contents".
Now, if what you wanted was to pull the property name out of some
text somewhere, then you'll need some fairly nasty workarounds for
AppleScript. Languages with better introspection abilities such as
Ruby or Python don't have as much trouble. I could tell you how to
do it in Objective-C in Leopard; I'd have to look up answers for
Ruby or Python. I'm sure has could tell you immediately.
I'm not clear what it is the OP is actually trying to achieve, but as
far as referring to properties and elements using a string in Python
and Ruby, that's simple enough to demonstrate:
#!/usr/bin/python
from appscript import *
property_name = 'title'
value = 'Queen'
person = app('Address Book').people[1]
print getattr(person, property_name).set(property_name)
#!/usr/bin/ruby
require 'appscript'
include Appscript
property_name = 'title'
value = 'Queen'
person = app('Address Book').people[1]
puts person.send(property_name).set(value)
To do it in AppleScript requires rather more work, although since you
should already know all the Address Book property names in advance you
don't actually need to resort to nasty hacks as Chris suggests. Just
define a bunch of objects, each of which takes a person reference and
returns a reference to one of its properties, and use them as follows:
-------
-- define reference builder objects
using terms from application "Address Book"
script BuildReferenceToTitle
on buildRef(ref_)
return a reference to title of ref_
end buildRef
end script
script BuildReferenceToFirstName
on buildRef(ref_)
return a reference to first name of ref_
end buildRef
end script
-- etc...
end using terms from
-------
-- look up a reference builder object by string-based name
property kReferenceBuilderLookupTable : {{"title",
BuildReferenceToTitle}, {"first name", BuildReferenceToFirstName}}
on getReferenceBuilder(name_)
repeat with aRef in kReferenceBuilderLookupTable
if item 1 of aRef = name_ then return item 2 of aRef
end repeat
error "Name not found."
end getReferenceBuilder
-------
-- main code
set HerTitle to "Queen"
set MyProperty to "title"
tell application "Address Book"
set myRef to person "test"
set myRef to buildRef(myRef) of my getReferenceBuilder("title")
set contents of myRef to HerTitle
end tell
The Python/Ruby approach is certainly much shorter and simpler though.
HTH
has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
_______________________________________________
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