Posted on 08-06-2018 04:14 PM
hello collective, I'm hoping someone can help me with the following. I'm trying to make our deployment process as easy as possible and am looking for a way to differentiate between faculty/staff machines and shared student machines. in my script i have the following, but it just hangs then exits. any help would be appreciated.
#!/bin/sh
computerType=$(osascript -e 'Tell application "System Events" to return choose from list {"Students", "Faculty", "Staff"}' 2>/dev/null)
if (( $? ));
then exit 1; fi # Abort, if user pressed Cancel.
if [[ "$computerType" == "Students" ]] ; then
#sets computer department to student in JamfPro
echo $computerType
sudo jamf recon -department $computerType
else
userName=$(osascript -e 'Tell application "System Events" to display dialog "Enter Faculty/Staff User Name" default answer ""' -e 'text returned of result' 2>/dev/null)
echo $username
sudo jamf recon -endUsername $userName
fi
exit 0
Solved! Go to Solution.
Posted on 08-07-2018 12:58 PM
I've got it, the issue is that the list chooser is not associated with System Events, the correct syntax is:
osascript -e 'return choose from list {"Students", "Faculty", "Staff"}'
Posted on 08-06-2018 04:59 PM
Most likely because your Applescript calls require user interaction, not just standard dialogs, and Jamf Pro is running the script as root, it's failing to present the dialog to the user. It's not Jamf's fault. It's a protection in the OS that stops that from happening. It's a pretty common issue when talking about scripts and running them in Jamf Pro actually.
You'll need to do the choose from list dialog and the enter answer ones as the user, not root. There are a few methods to doing this.
You can try the method in the modified script below (launchctl asuser) to see if it works. I reworked a few items in it.
#!/bin/sh
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)
computerType=$(/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOF
/usr/bin/osascript -e 'tell application "System Events"
activate
choose from list {"Students", "Faculty", "Staff"}
end tell' 2>/dev/null
EOF)
#if (( $? ));
if [ "$computerType" == "false" ]; then
exit 1
fi # Abort, if user pressed Cancel.
if [[ "$computerType" == "Students" ]] ; then
#sets computer department to student in JamfPro
echo $computerType
/usr/local/bin/jamf recon -department $computerType
else
userName=$(/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOD
/usr/bin/osascript -e 'tell application "System Events"
display dialog "Enter Faculty/Staff User Name" default answer "" buttons {"Enter"}
text returned of result
end tell' 2>/dev/null
EOD)
echo $userName
/usr/local/bin/jamf recon -endUsername $userName
fi
exit 0
So first, both Applescript dialog calls are using the /bin/launchctl asuser method to call those lines as the logged in user, instead of root. This usually works, but you'll need to test it to see.
Second, I broke up the Applescript stuff into a few lines. This allows you to call System Events to the front. If you don't do that, the dialog appears, but is not in the front, meaning the first click for the user is only bringing the dialog into focus. It's just a peeve of mine when that happens, because it's kind of annoying to have to click, then click again to make a choice. Better to have it right in focus so they can make a choice right away.
Third, though sadly you can't set your buttons in the choose from list dialog (why I don't know), you can do it with the enter text dialog, so I set the only button there to "Enter" This doesn't prevent someone from entering a null value unfortunately, but at least they can't cancel it. This makes it a little less likely to get an empty result.
Lastly, I changed the line if (( $? ));
to something else as you'll see. I don't know if you tested that, but it doesn't cancel the script if you click Cancel. osascript still exits with a 0 response in that case, but it echoes back "false" so I changed it to check for that as a response, and exit if false
is returned to the first choose from list dialog.
Posted on 08-06-2018 05:15 PM
Thanks for the input! I'm working off of a script we already have deployed to set the computer name which works just fine when Jamf runs the script, no need to run as the current user:
#!/bin/sh
while :; do
computerName=$(osascript -e 'Tell application "System Events" to display dialog "Computer Name" default answer ""' -e 'text returned of result' 2>/dev/null)
if (( $? ));
then exit 1; fi # Abort, if user pressed Cancel.
computerName=$(echo "$computerName" | sed 's/^ *//' | sed 's/ *$//') # Trim leading and trailing whitespace.
computerName=$(echo "$computerName" | sed 's/ /-/g') # Replace spaces with -
if [[ -z "$computerName" ]]; then
# The user left the name blank
osascript -e 'Tell application "System Events" to display alert "You must enter a computer name. Please try again" as warning' >/dev/null
# Continue loop to prompt again.
else
# Valid input: exit loop and continue.
break
fi
done
I want to add an additional dialog to add user/location data into the inventory after I set the computer name.
Posted on 08-07-2018 12:58 PM
I've got it, the issue is that the list chooser is not associated with System Events, the correct syntax is:
osascript -e 'return choose from list {"Students", "Faculty", "Staff"}'
Posted on 04-25-2019 03:18 PM
New to this having some issues. What was the final script?