Skip to content

Instantly share code, notes, and snippets.

@acidprime
Created March 20, 2012 16:51
Show Gist options
  • Save acidprime/2138160 to your computer and use it in GitHub Desktop.
Save acidprime/2138160 to your computer and use it in GitHub Desktop.
Updating a user's picture from a script
#!/bin/bash
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."
fi
fi
fi
@acidprime
Copy link
Author

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

http://stackoverflow.com/questions/9718960/programmatically-changing-the-users-account-image-in-osx

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