Computer Rename Prompt w/swiftDialog

Recently the need arose for our techs to be able to easily set device names. I wanted a script that I could deploy via Self-Service (or during enrollment) that would prompt the user and change the machine’s name based on the input.

swiftDialog was the obvious framework choice, but before I reinvented the wheel I wanted to see if someone else had already done the hard work.

Turns out, they had! I love when that happens.

Adam Codega has a great script that does exactly what I needed it to.

I have to call the icon out because it’s such an awesome touch. The script detects whether the device is a laptop or desktop and displays the appropriate icon. I’m a sucker for details like that!

I modified some of the GUI elements and added a bit of code to display the serial number to make it easier for the individual who is setting the name of the machine (since our naming convention includes the serial number).

I also added a Jamf Recon command to update the computer’s inventory record. This is completely optional, and if you don’t use Jamf you can easily remove that bit.

#!/bin/bash

# Credit to https://github.com/acodega

# Get serial number using system_profiler
serial_number=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

# Displays icon based on type of device
hwType=$(/usr/sbin/system_profiler SPHardwareDataType | grep "Model Identifier" | grep "Book")  
if [ "$hwType" != "" ]; then
  icon="SF=laptopcomputer"
else
  icon="SF=desktopcomputer"
fi 

dialogApp="/usr/local/bin/dialog"

title="Computer Name Prompter"
message="Please set the name of this device. \n\n Department Code + Serial Number + L/W (Laptop or Workstation). \n\n Your computer's serial number is: $serial_number"

dialogCMD="$dialogApp -p --title \"$title\" \
--icon \"$icon\" \
--message \"$message\" \
--messagefont "name=Arial,size=15" \
--small \
--button1text "Set" \
--button2 \
--ontop \
--moveable \
--textfield \"Computer Name\""

computerName=$(eval "$dialogCMD" | awk -F " : " '{print $NF}')

if [[ $computerName == "" ]]; then
  echo "Aborting"
  exit 1
fi

scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
scutil --set ComputerName "$computerName"

# Run Jamf binary command to update inventory record with new computer name
/usr/local/bin/jamf recon -setComputerName "$computerName"

# Echo new computer name for logging
echo "Computer Name is now $computerName"

exit 0

Note that this script doesn’t install swiftDialog or run any checks. The assumption is that it’s already setup and working. If you need something like that, Dan Snelson has some great code for pre-flight checks in his Setup-Your-Mac-via-Dialog script that you could easily co-opt.

You can find this script and more in my GitHub repo: https://github.com/trevoedwards/JamfStuff/

How are YOU using swiftDialog? I’d love to hear about it!

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Brad Stumpp
3 months ago

This was precisely what I was looking for. I am still learning swift dialog, and I had an old script that functioned, but was not as nice as what you produced. Thank you for sharing this!