Pull info from AD into AppleScript

jubei
New Contributor II

Hi all,
I've got the bones of an AppleScript that pulls in info to update the Outlook ME Contact

Below I have what's working and what's not as far as dscl queries. I tried all different kinds of dscl commands I've found on the forum but these are not returning values even though when I use the dscl tool in interactive mode, the info is there. Any help would be awesome!

-- working
set loggedInUser to do shell script "/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'"
set emailAddress to do shell script "dscl . -read /Users/" & loggedInUser & " EMailAddress | awk 'BEGIN {FS=": "} {print $2}'"
set firstName to do shell script "dscl . -read /Users/" & loggedInUser & "| grep FirstName | awk '{print $2}'"
set LastName to do shell script "dscl . -read /Users/" & loggedInUser & "| grep LastName | awk '{print $2}'"
set phonenumber to do shell script "dscl . -read /Users/" & loggedInUser & "| grep PhoneNumber | awk '{print $2}'"
set Country to do shell script "dscl . -read /Users/" & loggedInUser & "| grep Country | awk '{print $2}'"
set Zip to do shell script "dscl . -read /Users/" & loggedInUser & "| grep PostalCode | awk '{print $2}'"
set State to do shell script "dscl . -read /Users/" & loggedInUser & "| grep State | awk '{print $2}'"
set City to do shell script "dscl . -read /Users/" & loggedInUser & "| grep City | awk '{print $2}'"

-- not working
set jobtitle to do shell script "dscl . -read /Users/" & loggedInUser & "| grep JobTitle: | cut -c 11-"
set streetaddress to do shell script "dscl . -read /Users/" & loggedInUser & "| grep Street | awk '{print $2}'"
set officeaddress to do shell script "dscl . -read /Users/" & loggedInUser & "dsAttrTypeNative:physicalDeliveryOfficeName | head -2 | tail -1 | cut -c2-"

I think there are two things at play.

1.) Job Title & Street Address have spaces in them and when I do a DSCL query, it appears on a "second" line.
2.) officeaddress - dsAttrTypeNative:physicalDeliveryOfficeName shows correctly in the Directory Editor but I can't grep it when I run DSCL from the command line. I found this link that explains a good regex but that's not working for me:

http://macscripter.net/viewtopic.php?id=38267

1 REPLY 1

joshuasee
Contributor III

Prefixing the do shell script with "last paragraph of" should work. In fact, AppleScript could probably grab the data without do shell script or dscl by using its built in properties list tools to parse items out of /var/db/dslocal/nodes/Default/users/username.plist

Alternately, for less code tweaking on your part, cut and dscl themselves can handle the cleaning on the *nix shell side of things:

dscl . -read /Users/someuser RealName | cut -sd' ' -f2-
dscl . -read /Users/someuser EMailAddress | cut -sd' ' -f2-

AD here doesn't have a physicalDeliveryOfficeName for me to test, but in most cases you can simply omit dsAttrTypeNative: in dscl rather than worry about parsing the colon.