Need Help with Script in Self Service; Prompt User for username

TomDay
Release Candidate Programs Tester

I am rsyncing data from a users old laptop to a newly issued laptop and I want to prompt the user with a script in Self Service that asks for the username and pass it as $nuser. Using that variable to rsync the correct folder. Here is some the script I built so far which works great to pull all user folders but I just want to the user folder for the 1 user who we get the input for:

#!/bin/sh

####################################################################################################
#
## May 2016
#
#############!/bin/sh



#echo "Checking for passed paramaters..."



echo "Remove Cache from Macintosh HD 1"


echo  "Remove user home folder cache..."
sleep 1
rm -rf /Volumes/Macintosh HD 1/Users/"$nuser"/Library/Caches/*
sleep 2
echo "Emptying user Trash..."
rm -rf /Volumes/Macintosh HD 1/Users/"$nuser"/.Trash/*
sleep 2


sleep 1
echo "Working..."

sleep 1
echo "Initializing Migration Assistant..."
sleep 2
echo "Transferring User Data....."
sleep 1

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"$nuser"/* /Volumes/Macintosh HD/Users/"$nuser"/
sleep 2
echo "Running  Transferring User Data......"
sleep 1

sleep 1
echo "Working..."

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"$nuser"/* /Volumes/Macintosh HD/Users/"$nuser"/
sleep 2
echo "Running  Transferring User Data..........."

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"$nuser"/* /Volumes/Macintosh HD/Users/"$nuser"/
sleep 1
echo "Adjusting File Permissions..."
/usr/bin/chmod -R 775 /Volumes/Macintosh HD/Users/"$nuser"
sleep 1
echo "Adjusting File Ownership..."
/usr/sbin/chown -R "$nuser":staff /Volumes/Macintosh HD/Users/"$nuser"
echo "Copy complete..."
sleep 1
echo "Transfer Completed Successfully..."




exit 0
#################################################################################

Found a helpful link on [https://jamfnation.jamfsoftware.com/discussion.html?id=15225](Jamf Nation) that got me one step further in my process but need some scripting expertise to grab the username, please!

1 ACCEPTED SOLUTION

mbezzo
Contributor III

I use osascript in my scripts so I don't have to also install CocoaDialog. It's a bit more limited, but usually meets my needs. Here's an example:

#!/bin/sh

#Function that generates the dialog
usernamePrompt(){
    # $1 = window title
    # $2 = prompt text
    # $3 = default answer
    su - "${loggedInUser}" -c osascript <<EOT
        tell application "System Events"
            with timeout of 8947848 seconds
                text returned of (display dialog "$2" default answer "$3" buttons {"OK"} default button 1 with title "$1" with icon caution)
            end timeout
        end tell
EOT
}

#Call the function and set the result as the variable
username="$(usernamePrompt 'Enter Username' 'Please enter the username you would like to modfy.' 'enter username')"

View solution in original post

9 REPLIES 9

millersc
Valued Contributor

CocoaDialog would work great in this scenario. We're doing something similar in DeployStudio with CD, thanks to @rustymyers I've started expanding options thanks to user input of CD.

Good example to get started with LINKY

mbezzo
Contributor III

I use osascript in my scripts so I don't have to also install CocoaDialog. It's a bit more limited, but usually meets my needs. Here's an example:

#!/bin/sh

#Function that generates the dialog
usernamePrompt(){
    # $1 = window title
    # $2 = prompt text
    # $3 = default answer
    su - "${loggedInUser}" -c osascript <<EOT
        tell application "System Events"
            with timeout of 8947848 seconds
                text returned of (display dialog "$2" default answer "$3" buttons {"OK"} default button 1 with title "$1" with icon caution)
            end timeout
        end tell
EOT
}

#Call the function and set the result as the variable
username="$(usernamePrompt 'Enter Username' 'Please enter the username you would like to modfy.' 'enter username')"

emily
Valued Contributor III
Valued Contributor III

Maybe this will help? I use it to prompt for a username, then assign the user to a device during enrollment when using a custom QuickAdd package.

#!/bin/sh

#
# prompts for username associated with machine
# adapted by emily
# 2015-03-11
#

# Set CocoaDialog Location
CD="/Library/Application Support/JAMF/bin/cocoaDialog.app/Contents/MacOS/cocoaDialog"

# Prompt to enter the computer name and the create $USER variable
rv=($("$CD" standard-inputbox --title "Username" --no-newline --informative-text "Enter the username of the user assigned to this machine (firstname_lastname)" --value-required))
USER=${rv[1]}

# Set user using variable created above
jamf recon -endUsername $USER

# Dialog to confirm that the user was assigned and what it was changed to.
tb=`"$CD" ok-msgbox --text "User Added!" 
--informative-text "The computer's associated user has been set to $USER." 
--no-newline --float`
if [ "$tb" == "1" ]; then
echo "User said OK"
elif [ "$tb" == "2" ]; then
echo "Canceling"
exit
fi

TomDay
Release Candidate Programs Tester

@millersc @emily Thanks I will give your suggestions with cocoa a try if I cant get it going with osascript! @mbezzo Think I am pretty close now with your suggestion. probably an easy fixing but the username I am getting, its not passing as a variable and copying the respective folder.

#!/bin/sh

####################################################################################################
#
## Created by Tom Day - May 2016
#
#############!/bin/sh


# Get username
username=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "What username do you want to rynsc?" default answer "" buttons {"Continue"})
end tell
END)





echo  "Remove user home folder cache..."
sleep 1
rm -rf /Volumes/Macintosh HD 1/Users/"username"/Library/Caches/*
sleep 2
echo "Emptying user Trash..."
rm -rf /Volumes/Macintosh HD 1/Users/"username"/.Trash/*
sleep 2

sleep 1
echo "Working..."

sleep 1
echo "Initializing Migration Assistant..."
sleep 2
echo "Transferring User Data....."
sleep 1

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"username"/* /Volumes/Macintosh HD/Users/"username"/
sleep 2
echo "Running  Transferring User Data......"
sleep 1

sleep 1
echo "Working..."

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"username"/* /Volumes/Macintosh HD/Users/"username"/
sleep 2
echo "Running  Transferring User Data..........."

/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"username"/* /Volumes/Macintosh HD/Users/"username"/
sleep 1
echo "Adjusting File Permissions..."
/usr/bin/chmod -R 775 /Volumes/Macintosh HD/Users/"username"
sleep 1
echo "Adjusting File Ownership..."
/usr/sbin/chown -R "username":staff /Volumes/Macintosh HD/Users/"username"
echo "Copy complete..."
sleep 1
echo "Transfer Completed Successfully..."

exit 0
########################################################################################

emily
Valued Contributor III
Valued Contributor III

I'm not a scripting expert by any means, but I'm guessing it's because you have it in the filepaths as "username" rather than $username or however else the variable is being defined.

mm2270
Legendary Contributor III

@TomDay In all instances in your script you aren't using "username" as a variable. Its just a string. For example:

rm -rf /Volumes/Macintosh HD 1/Users/"username"/Library/Caches/*

needs to be:

rm -rf /Volumes/Macintosh HD 1/Users/"$username"/Library/Caches/*

Note the $ sign on username to make it into a variable.

TomDay
Release Candidate Programs Tester

@emily @mm2270 Thanks, works perfectly now using a variable! How did I miss that, even as a scripting newb! Just need to clean it up a bit.

rustymyers
New Contributor II

I like CocoaDialog for prompting because it offers a pretty easy way to slip in variables. Here's a quick example that I would start from to add the function for choosing a user home. I prefer to ask which user, rather than expect the person to enter the correct Username:

for i in /Users/*; do
    myUsers=($i ${myUsers[*]})
done
echo $myUsers
userResponse=$(/Users/rzm102/Desktop/CocoaDialog.app/Contents/MacOS/CocoaDialog standard-dropdown --items ${myUsers[*]})
echo "$userResponse"
read returnvalue userselection <<< $userResponse
echo "Return code: $returnvalue"
echo "User Selected: ${myUsers[$userselection]}"

Using that before your code to set the path to the user folder you'd like to back up.
This is also the purpose of the BackupRestore scripts, which just got the ability to prompt for the user to back up. https://github.com/rustymyers/BackupRestore

Hope that helps!
Rusty

TomDay
Release Candidate Programs Tester

The osascript function worked perfectly. Simple yet perfectly effective.

Thanks to Everyone that joined the conversation. It's become very clear that I need to experiment with CocoaDialog!

-Tom