you'll be looking at swiftdialog for this.. https://github.com/bartreardon/swiftDialog
or.. go old school with AppleScript..
You can use bash for an input box as well it will just store the data to a temp file, which could be useful to store the temp input of the entered text if its a computer name. Any reason why you would use a prompt instead of just automatically naming the computers?
#!/bin/bash
# Prompt user for computer name
computerName=$(/usr/bin/osascript -e 'text returned of (display dialog "Please enter the computer name in the format:
AG-MAC-12345" default answer "AG-MAC-" buttons {"OK"} default button 1)')
# Set the local computer name to match the room name
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
scutil --set ComputerName $computerName
# Run the jamf binary and update inventory with the new computer name
/usr/local/bin/jamf recon -setComputerName "$computerName"
# Echo the new computer name for logging purposes
echo "New Computer Name is $computerName"
exit 0
Worked a treat @jtrant .
I don't suppose you know how to restrict the user input to numbers only with a maximum of 5 numbers? (i.e. 12345) with an error if submitted incorrectly?
Worked a treat @jtrant .
I don't suppose you know how to restrict the user input to numbers only with a maximum of 5 numbers? (i.e. 12345) with an error if submitted incorrectly?
I don't, sorry. You could look at Setup-Your-Mac for this, but you'd probably need to make quite a few changes to the script, or adapt only the parts you need to your own script.
Worked a treat @jtrant .
I don't suppose you know how to restrict the user input to numbers only with a maximum of 5 numbers? (i.e. 12345) with an error if submitted incorrectly?
@infrase2020 You can try this script. It should only accept up to 5 numbers maximum. Anything else will generate an error and reprompt. It also does not prefix the AG-MAC- part of the computer name in the dialog. It only asks for the asset tag and adds the prefix to the computer name later.
#!/bin/zsh
msgText="Please enter the asset tag number for this Mac:"
function askForInput ()
{
assetTag=$(/usr/bin/osascript << EOD
set Message to "$msgText" as string
text returned of (display dialog Message buttons {"OK"} default answer "" default button 1)
EOD
)
/bin/echo "Asset tag entered: $assetTag"
lengthCheck="${#assetTag}"
numericCheck=$(echo "$assetTag / $assetTag" | bc 2>/dev/null)
if [[ "$lengthCheck" -le 5 ]] && [[ "$numericCheck" = "1" ]]; then
/bin/echo "Correct asset tag format was entered"
else
/bin/echo "String contains either non-numeric characters or too many characters"
msgText="Error! Enter only numbers, and no longer than 5 numbers long.
Please enter the asset tag number for this Mac:"
askForInput
fi
}
askForInput
## Set the new computer name string
computerName="AG-MAC-${assetTag}"
## Set the computer name, local hostname and hostname
/usr/sbin/scutil --set HostName $computerName
/usr/sbin/scutil --set LocalHostName $computerName
/usr/sbin/scutil --set ComputerName $computerName
## Update Jamf with the new name
/usr/local/bin/jamf recon -setComputerName "$computerName"
# Echo the new computer name for logging purposes
/bin/echo "New computer name assigned: $computerName"
I should note that Applescript itself has ways of checking the string entered to see if it matches a set of criteria, but I don't know how to do that, so the checking is all done in the shell.
Finally, YMMV with getting this script to pop up a message, depending on how it's being called. Sometimes scripts using osascript that are called as root don't work well. You might want to look into some other utilities if you run into issues with this, such as swiftDialog or IBM Notifier, both of which can take user input and give you more flexibility over the style, size, position and so forth of the dialog.
Hello - I know this is real late of a reply.. but here is what I had luck with. You can run/tweak it in Code Runner to see how you like it first.
**************************
#!/bin/bash
# Network ID
UserName=$(osascript -e 'Tell application "system events" to display dialog "Please enter your network User ID" default answer "" buttons {"Next"} default button "Next"')
echo $UserName
# Full Name
FullName=$(osascript -e 'Tell application "system events" to display dialog "Please enter your First and Last name" default answer "" buttons {"Next"} default button "Next"')
echo $FullName
# Email
emailSubmitted=$(osascript -e 'Tell application "system events" to display dialog "Please enter your email address" default answer "@honorhealth.com" buttons {"Next"} default button "Next"')
echo $emailSubmitted
# Phone Numer
Phone=$(osascript -e 'Tell application "system events" to display dialog "Please enter your work phone number" default answer "" buttons {"Next"} default button "Next"')
echo $Phone
# Campus
Building=$(osascript -e 'Tell application "system events" to display dialog "Please enter your primary campus name" default answer "" buttons {"Next"} default button "Next"')
echo $Building
# Asset Tag
ComputerName=$(osascript -e 'Tell application "system events" to display dialog "Please enter your computer Asset Tag" default answer "W" buttons {"Finish"} default button "Finish"')
echo $ComputerName
#Convert the results from user inputs to a form usable by Jamf Pro.
Username=$(echo $Username | sed 's/button returned:Next, text returned://')
echo $Username
#Convert the results from user inputs to a form usable by Jamf Pro.
FullName=$(echo $FullName | sed 's/button returned:Next, text returned://')
echo $FullName
Email=$(echo $Email | sed 's/button returned:Next, text returned://')
echo $Email
#Convert the results from user inputs to a form usable by Jamf Pro.
Phone=$(echo $Phone | sed 's/button returned:Next, text returned://')
echo $Phone
#Convert the results from user inputs to a form usable by Jamf Pro.
Building=$(echo $Building | sed 's/button returned:Next, text returned://')
echo $Building
ComputerName=$(echo $ComputerName | sed 's/button returned:Finish, text returned://')
echo $ComputerName
# Uploads data to device record in JSS
sudo jamf recon -username $UserName
sudo jamf recon -FullName $FullName
sudo jamf recon -email $Email
sudo jamf recon -phone $Phone
sudo jamf recon -building $Building
sudo jamf recon -ComputerName $ComputerName
#End of script
*************************************
Good luck!
--Jack