Skip to content

Instantly share code, notes, and snippets.

@palmerc
Forked from acidprime/userpic.sh
Last active May 26, 2024 10:44
Show Gist options
  • Save palmerc/5e8dfb37ed46b01c9a6f56ecd58727a7 to your computer and use it in GitHub Desktop.
Save palmerc/5e8dfb37ed46b01c9a6f56ecd58727a7 to your computer and use it in GitHub Desktop.
Updating a user's picture from a script
#!/bin/bash
set -e
declare -x USERNAME="$1"
declare -x USERPIC="$2"
declare -r DSIMPORT_CMD="/usr/bin/dsimport"
declare -r ID_CMD="/usr/bin/id"
declare -r MAPPINGS='0x0A 0x5C 0x3A 0x2C'
declare -r ATTRS='dsRecTypeStandard:Users 2 dsAttrTypeStandard:RecordName externalbinary:dsAttrTypeStandard:JPEGPhoto'
if [ ! -f "${USERPIC}" ]; then
echo "User image required"
fi
# Check that the username exists - exit on error
${ID_CMD} "${USERNAME}" &>/dev/null || ( echo "User does not exist" && exit 1 )
declare -r PICIMPORT="$(mktemp -t ${USERNAME}_dsimport)" || exit 1
trap 'rm -f ${PICIMPORT}' EXIT
printf "%s %s \n%s:%s" "${MAPPINGS}" "${ATTRS}" "${USERNAME}" "${USERPIC}" >"${PICIMPORT}"
${DSIMPORT_CMD} "${PICIMPORT}" /Local/Default M &&
echo "Successfully imported ${USERPIC} for ${USERNAME}."
@leoheck
Copy link

leoheck commented Nov 2, 2021

Yeap. You are right, I am also investigating, with no success still.

@leoheck
Copy link

leoheck commented Nov 2, 2021

If you go to the iCloud page. It has a picture there. and even if adding a picture manually, sometimes it is removed.

Maybe the pictures are removed when I run something like this:

sudo dscl . delete /Users/${USER} JPEGPhoto
sudo dscl . delete /Users/${USER} Picture

So, it means that it is possible to change this picture. Now what is making me crazy is how we can set it back. haha

image

@leoheck
Copy link

leoheck commented Nov 2, 2021

Actually, after finishing typing the message above, the image was reset again!! And this time I didn't do anything.

image

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