Programmatically Changing a User Account Image in OSX

Is there any way to programmatically change the user account image on OSX?

I know that I can get it, but can I change it, how does the apple do it on the account page in the settings application?

+4
source share
3 answers

You can use the address book structure. You need to use the me method to get the entry for the current user, and then the setImageData: method to set the user image:

 #import <AddressBook/AddressBook.h> @implementation YourClass - (void)setUserImage:(NSImage*)anImage { ABPerson* me = [[ABAddressBook addressBook] me]; [me setImageData:[anImage TIFFRepresentation]]; } @end 

More details here in the docs .

+4
source

You can go to the file and merge it with the current record using the / usr / bin / dsimport command, which can be run from NSTask. Here is an example of how to do this with BASH as root, this can be done with the transmitted credentials as well

  export OsVersion=`sw_vers -productVersion | awk -F"." '{print $2;exit}'` declare -x UserPicture="/path/to/$UserName.jpg" # 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 

The essence of this code

+3
source

I am pretty sure that you can do this through the OpenDirectory interface. See This Guide:

https://developer.apple.com/library/mac/#documentation/Networking/Conceptual/Open_Directory/Introduction/Introduction.html#//apple_ref/doc/uid/TP40000917

Basically, you need to open an Open Directory Node (let's say / Search node), and then find ODRecord for your user, and then use:

 setValue:forAttribute:error 

in ODRecord to set the JPEGPhoto attribute.

If you request to use dscl from the command line for this attribute, you will see the attribute value:

 dscl /Search read /Users/luser JPEGPhoto 

I believe the dscl tool uses an Open Directory structure (or an older / harder to use / obsolete directory service infrastructure) to read and write user record attributes. You can read and write any other attributes using this tool and the corresponding structure. I see no reason why JPEGPhoto will be different.

The / Search Node MAY only be read (since it is a kind of meta node). Not quite sure. You may need to explicitly open the corresponding Node (for example, / Local / Default node) before you can write to the entry:

 dscl /Local/Default read /Users/luser JPEGPhoto 
+2
source

Source: https://habr.com/ru/post/1401579/


All Articles