Create a folder, and name it based off of Current User?

Spraynard
New Contributor

Hi all, 

so, pretty new to Jamf, but I work at a school where lots of random softwares are used frequently in specific labs. 

One software we're using is OpenToonz, an animation software, and a FFmpeg plug in, which I have to point to a file path for OpenToonz to find the folder for. Problem is, this preference file is stored in /Applications/OpenToonz/OpenToonz_stuff/profiles/layouts, and generates a folder called "settings.usernamehere"

 

So, I can create the preferences file and move it to a specific path on scoped computers, but what I can't seem to figure out is how to create a file based off the name of the current logged in user, and place it in that specific file path. (i.e settings.currentLoggedInUser)

 

I assume a script is the way to go, but my googling has failed me this time around, and I'm a little out of my depth. Either way, appreciate any help you can give! 

 

 

 

3 REPLIES 3

mschlosser
Contributor II

the below script will retrieve then currently logged in user, and create the needed path:

 

#!/bin/sh

# variable and function declarations

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# get the currently logged in user
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

# global check if there is a user logged in
if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then
  echo "no user logged in, cannot proceed"
  exit 1
fi
# now we know a user is logged in

# get the current user's UID
uid=$(id -u "$currentUser")

runAsUser() {  
  if [ "$currentUser" != "loginwindow" ]; then
    launchctl asuser "$uid" sudo -u "$currentUser" "$@"
  else
    echo "no user logged in"
    # uncomment the exit command
    # to make the function exit with an error when no user is logged in
    # exit 1
  fi
}
# main code starts here

runAsUser mkdir "/Applications/OpenToonz/OpenToonz_stuff/profiles/layouts/settings.$currentUser"

exit 0

 

 you'd still need to decide how you wanted to call the script to run; simply you could advertise it in self service for users to run on demand or wrap it in a .command and deploy for users to run as needed or tie it to a launchdaemon to run every time somebody logs in.

The script also assumes that all users have permission to write to the ..../layouts path; something you could fix with chmod if that wasn't the case.

Your choice really. Hope that helps. 

mschlosser
Contributor II

it just dawned on me that you said you wanted to create a file and not a folder; my apologies. the below version should make a file instead of a folder. Obviously remove the placeholder with the exact file contents:

#!/bin/sh

# variable and function declarations

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# get the currently logged in user
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

# global check if there is a user logged in
if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then
  echo "no user logged in, cannot proceed"
  exit 1
fi
# now we know a user is logged in

# get the current user's UID
uid=$(id -u "$currentUser")

runAsUser() {  
  if [ "$currentUser" != "loginwindow" ]; then
    launchctl asuser "$uid" sudo -u "$currentUser" "$@"
  else
    echo "no user logged in"
    # uncomment the exit command
    # to make the function exit with an error when no user is logged in
    # exit 1
  fi
}
# main code starts here

runAsUser cat << EOF > "/Applications/OpenToonz/OpenToonz_stuff/profiles/layouts/settings.$currentUser"

<contents of file goes here>

EOF

exit 0

Spraynard
New Contributor

Great, I'll give this a shot!

You were right the first time, in that I need to create a folder, and then move a file there, but I think I have what I need to do automate the last parts. The folder creation and naming itself were the big challenges, so this is insanely helpful. Thank you!!