Finding me in the Address Book

Hi… Great looking BBS, BTW…

I’d like to programmatically find myself in the Address Book. This is actually so that my Mac can say “hello” to me by name when I return (carrying my phone) to it :)… see http://homepage.mac.com/jonassalling/Shareware/Clicker/.

I’m currently using this snippet:

set user to do shell script "whoami"
say "hello " & user using "Victoria"

… but I’d like the script to say my real name, not my login name.

The O’Reilly article “A Look Inside Address Book” shows how to retrieve the “Me” record using C. I’d like to do the equivalent using AppleScript, but can’t figure it out.

The most productive experiment I’ve done is below: but it doesn’t solve my problem.

tell application "Address Book"
	set ash to birth date of person "Ashley Ward" as text
	display dialog ash
end tell

I’d like to be able to say something like “set ash to name of person Me”.

Any hints?

This is not from the Address Book, but to get your “real name” try this…

set theFullName to do shell script "niutil -readprop . /users/" & (do shell script "whoami") & " realname"

Thanks Greg!

If it isn’t possible using AppleScript alone, then perhaps I should send a bug report / feature request to Apple. Hmmm.

Just one question about your solution: is ‘niutil’ part of the standard Mac OS X install? (I’m avoiding using ‘defaults’ in portable scripts as it is part of the optional BSD package for example).

Hmmm. I’ve used defaults in distributed scripts without knowing this. Is there a resource available that tells which utilities belong to BSD and which belong to a standard install?

– Rob (who’s now wondering how often his scripts have failed for users who didn’t bother to inform him)

(BTW: I’m working only on hearsay wrt this problem of using only compulsory install utilities in portable scripts. If you’ve seen any documentation giving recommendations, I’d like to see it).

I should think that most people install the BSD utilities anyway, so hopefully using ‘defaults’ wouldn’t cause a problem too often.

The best I’ve got is a script called ‘package-for’ which someone on ericssonclient@yahoogroups.com gave me earlier today.

#!/usr/bin/perl
# Locate the installer package a particular executable is part of.

foreach ( @ARGV ) {
        next if !chomp($path = `which $_`);
        $base = (split "/", $path)[-1];
        print "$_ ($path): ";
        @pkgs = split("n", `find /Library/Receipts -name "*.bom" -print0 | xargs -0 grep -lw $base`);
        foreach ( @pkgs ) {
                print "$1 " if (`lsbom -s "$_" | grep -l '$path$'` && m!/([^/]+).pkg/!);
        }
        print "n";
}

Unfortunately it seems to give a blank answer for niutil.

 [ip-226-97-dhcp:~] ashley% ./bin/package-for whoami
 whoami (/usr/bin/whoami): Essentials
 [ip-226-97-dhcp:~] ashley% ./bin/package-for defaults
 defaults (/usr/bin/defaults): BSD
 [ip-226-97-dhcp:~] ashley% ./bin/package-for ls
 ls (/bin/ls): BaseSystem
 [ip-226-97-dhcp:~] ashley% ./bin/package-for niutil
 niutil (/usr/bin/niutil):
 [ip-226-97-dhcp:~] ashley%

Ashley, I did install the BSD Utilities when I installed Mac OS X so I’m not sure that “niutil” is a part of the standard installation. Here’s another way of getting the ‘real name’…

set theFullName to do shell script "finger " & (system attribute "USER") & "| grep Login | colrm 1 46"

I would hope that ‘finger’ would be a standard, but then again, I’m not sure. Thanks for pointing that out.

Seems that finger, grep and colrm are all part of BSD, I’m afraid! :frowning:

[ip-226-97-dhcp:~] ashley% ./bin/package-for finger
finger (/usr/bin/finger): BSD 
[ip-226-97-dhcp:~] ashley% ./bin/package-for grep
grep (/usr/bin/grep): BSD 
[ip-226-97-dhcp:~] ashley% ./bin/package-for colrm
colrm (/usr/bin/colrm): BSD 

I’ve reported ‘Finding “me” in Address Book is not directly possible in AppleScript’ in Apple’s Bug Reporter, Problem ID: 3322035.

Ashley, here’s one more attempt at getting the ‘realname’…

set cmd to "perl -e "@p = getpwnam `whoami`;""
set cmd to cmd & " -e 'print "$p[6]";'"
do shell script cmd

Once again, I’m not sure if it will work without the BSD utilities???

(To get the result into a variable):

set cmd to "perl -e "@p = getpwnam `whoami`;""
set cmd to cmd & " -e 'print "$p[6]";'"
set fullName to (do shell script cmd)
display dialog fullName

Greg - thanks very much! That works splendidly and it seems that it’ll be good and portable:

[ip-226-97-dhcp:~] ashley% ./bin/package-for perl
perl (/usr/bin/perl): BaseSystem 
[ip-226-97-dhcp:~] ashley% ./bin/package-for whoami
whoami (/usr/bin/whoami): Essentials 

I got a reply from devbugs@apple.com this morning. The bug I reported “… is a known issue that is currently being investigated by engineering”.
Thanks for the help, everyone.

Just for kicks, it can be simplified a bit further…

set cmd to "perl -e "@p = getpwnam `whoami`;" -e 'print "$p[6]";'"
do shell script cmd
display dialog result

Glad it works, and thanks for sharing the bug report info.