Skip to content

Instantly share code, notes, and snippets.

@apizz
Last active July 19, 2021 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apizz/7606c95f2738c716bd6731a496696553 to your computer and use it in GitHub Desktop.
Save apizz/7606c95f2738c716bd6731a496696553 to your computer and use it in GitHub Desktop.
Script to manually set a Mac's hostname
#!/bin/bash
currentCompName=$(/bin/hostname)
getCompName() {
newCompName=$(/usr/bin/osascript <<AppleScript
display dialog "Current computer name: ${currentCompName}
Please enter the new computer name below.
Example: YOUR_EXAMPLE_HOSTNAME_HERE" with title "Set New Computer Name" default answer ""
set enteredCompName to text returned of result
AppleScript
/bin/echo "${newCompName}"
)
}
verifyCompName() {
reviewNewName=$(/usr/bin/osascript <<AppleScript
display dialog "Change computer name from ${currentCompName} to ${newCompName}?" with title "Verify New Computer Name" buttons {"Cancel", "Yes"} default button 1
set response to button returned of result
AppleScript
/bin/echo "${reviewNewName}"
)
}
successNewName() {
confirmation=$(/usr/bin/osascript <<AppleScript
display dialog "Successfully changed computer name to ${newCompName}!" with title "SUCCESS" buttons {"OK"} default button 1
AppleScript
)
}
failedNewName() {
confirmation=$(/usr/bin/osascript <<AppleScript
display dialog "Failed to change computer name to ${newCompName}!" with title "FAILED" buttons {"OK"} default button 1
AppleScript
)
}
##########################
######### SCRIPT #########
##########################
getCompName
if [ "${newCompName}" = "" ]; then
/bin/echo "Rename Cancelled"
exit 0
fi
verifyCompName
if [ "${newCompName}" != "" ] && [ "${reviewNewName}" != "" ]; then
/usr/sbin/scutil --set ComputerName "${newCompName}"
/usr/sbin/scutil --set HostName "${newCompName}"
/usr/sbin/scutil --set LocalHostName "${newCompName}"
newComputerName=$(/usr/sbin/scutil --get ComputerName)
newHostName=$(/usr/sbin/scutil --get HostName)
newLocalHostName=$(/usr/sbin/scutil --get LocalHostName)
else
/bin/echo "Rename Verify Cancelled"
exit 0
fi
if [ "${newComputerName}" = "${newCompName}" ] && [ "${newHostName}" = "${newCompName}" ] && [ "${newLocalHostName}" = "${newCompName}" ]; then
/bin/echo "Rename Successful!"
successNewName
else
/bin/echo "Rename Failed."
failedNewName
exit 1
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment