• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Setting parameters in Address Book
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting parameters in Address Book


  • Subject: Re: Setting parameters in Address Book
  • From: has <email@hidden>
  • Date: Tue, 12 Feb 2008 22:10:54 +0000

On 12 Feb 2008, at 20:41, Robert R. Horning wrote:

Thanks a million for the suggestions! As you may have guessed, I'm trying to avoid having to include much of the code at the end of the Import Addresses script, found in /Library/Scripts/Address Book Scripts, e.g.:

if (fieldName begins with "First Name") then theScript's setFirstName(newEntry, thisItem)
else if (fieldName begins with "Last Name") then theScript's setLastName(newEngtry, thisItem)
else if ...


and the accompanying code in Import Helper.

Mff, looks like it must've changed in 10.5. I can probably guess what the old script was like, although can't be bothered trawling through the new one to see what it does (I've never found Apple-supplied sample scripts to be terribly clear or concise).



To this end, I just want to be able to specify Address Book property and element names at run time, using strings stored in a variable. These strings come from column headers in an Excel worksheet. (In other words, I make sure that the worksheet column headers are identical to the names of Address Book properties.) So my idea was for 'MyProperty', below, to contain one of these strings, or a manipulated version of it, whenever the 'tell foo' command is executed. I gather that is not feasible.

It's doable in AS - it's just that without introspection, you have to resort to crude and potentially unsafe code generation hacks to make it work. Example:


on makeReferenceBuilder(property_name)
	return run script "
	script
		on buildRef(ref_)
			tell application \"Address Book\"
				return a reference to " & property_name & " of ref_
			end tell
		end buildRef
	end"
end makeReferenceBuilder

tell application "Address Book"
	set myRef to person "test"
	set myRef to buildRef(myRef) of my makeReferenceBuilder("title")
	set contents of myRef to "Mr"
end tell

(If you've a lot of person objects to get through, you'll want to create all the script objects you'll need up-front and store them in a lookup table as demonstrated earlier since invoking 'run script' lots of times will be very slow.)

Honestly though, I wouldn't fret over code length if I were you. If you can achieve the desired goal safely and reliably using a bit more code - and it doesn't need nearly as many lines as that Apple script suggests (told you they weren't long-winded) - then that's the best way to do it:

on getReferenceToProperty(ref_, property_name)
	tell application "Address Book"
		if property_name = "title" then
			return a reference to title of ref_
		else if property_name = "first name" then
			return a reference to first name of ref_
		else if ...
			...
		else
			error "Unknown property name."
		end
	end
end getReferenceToProperty

tell application "Address Book"
	set myRef to person "test"
	set myRef to getReferenceToProperty(myRef, "title")
	set contents of myRef to "Mr"
end tell


As I told Chris, I've never seen Python or Ruby, and I did my last professional programming in 1993 using VMS Fortran, so perhaps you could illustrate (in painful detail) how to include in my Applescript either the Python or Ruby script that will accomplish what I hoped might work and how to call, and pass variables to/from it. I really appreciate you patience.


Calling into Python/Ruby from AppleScript would be slow and clumsy, and if you want to go the Python/Ruby route then the best thing would be to port your existing AppleScript over wholesale. They are much better languages than AppleScript and it's well spending a few days learning the basics of one or the other; as well as being useful tools in their own right, expanding your knowledge of programming in general will help to make you a better AppleScripter too. However, if all you're looking for is a quick fix to your current problem then I'd recommend forgetting about introspection and code generation and other excessively "clever" solutions and just go with the slightly longwinded but simple and reliable approach as demonstrated by the getReferenceToProperty handler above.

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
References: 
 >Setting parameters in Address Book (From: "Robert R. Horning" <email@hidden>)

  • Prev by Date: Re: Help with understanding matrices
  • Next by Date: pivot table
  • Previous by thread: Re: Setting parameters in Address Book
  • Next by thread: Unble to run Script based application
  • Index(es):
    • Date
    • Thread