Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Updating a user's picture from a script
declare -xr awk="/usr/bin/awk"
declare -xr sw_vers="/usr/bin/sw_vers"
declare -xr dsimport="/usr/bin/dsimport"
declare -xr id="/usr/bin/id"
export UserName="$1"
export UserPicture="/path/to/$UserName.jpg"
export OsVersion=`$sw_vers -productVersion | $awk -F"." '{print $2;exit}'`
# Add the LDAP picture to the user record if dsimport is avaiable 10.6+
if [ -f "$UserPicture" ] ; then
# On 10.6 and higher this works
if [ "$OsVersion" -ge "6" ] ; then
declare -x Mappings='0x0A 0x5C 0x3A 0x2C'
declare -x Attributes='dsRecTypeStandard:Users 2 dsAttrTypeStandard:RecordName externalbinary:dsAttrTypeStandard:JPEGPhoto'
declare -x PictureImport="/Library/Caches/$UserName.picture.dsimport"
printf "%s %s \n%s:%s" "$Mappings" "$Attributes" "$UserName" "$UserPicture" >"$PictureImport"
# Check to see if the username is correct and import picture
if $id "$UserName" &>/dev/null ; then
# No credentials passed as we are running as root
$dsimport -g "$PictureImport" /Local/Default M &&
echo "Successfully imported users picture."

acidprime commented Mar 21, 2012

A neat Cocoa way (indirectly) via the AddressBook API is mentioned here too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment