Posted on 08-16-2023 03:08 AM
Hi,
We'd like to create an app/script that when run prompts the user to enter an asset tag and then automatically renames the device.
I.e. asset tag 12345 renames the device to AG-MAC-12345 so that "AG-MAC-" is already predefined?
Is this something that can be done with DEPNotify?
TIA.
Solved! Go to Solution.
08-16-2023 06:30 AM - edited 08-16-2023 06:54 AM
#!/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
Posted on 08-16-2023 04:49 AM
you'll be looking at swiftdialog for this.. https://github.com/bartreardon/swiftDialog
or.. go old school with AppleScript..
Posted on 08-16-2023 06:11 AM
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?
08-16-2023 06:30 AM - edited 08-16-2023 06:54 AM
#!/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
Posted on 08-16-2023 07:08 AM
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?
08-16-2023 07:20 AM - edited 08-16-2023 07:27 AM
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.
Posted on 08-17-2023 12:37 PM
@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.