Script to change HostName, SED it, limit to 15 characters

Hi, Applescripters. I am working on giving the IT department for the College I work for some clickable apps that will allow routine configuration of Macs.

We have Macs that join a Windows AD domain and allow network access through those IDs. So, one of the first things necessary to do with a Mac is change the name from “Jon Doe’s MacBookPro” to a proper name that coincides with the AD name which it is eventually bound with.

It’s also necessary that AD names be entirely alphanumeric, except for hyphens – which are allowed. And it is necessary that empty spaces are removed. For legacy reasons, AD entries should be 15 characters or less.

I have found old scripts that have done parts of this. But I hadn’t found a complete script that did everything I needed.

One other thing I did was to change the name of the local hard drive to the name of the HostName with HD pasted on at the end. I have found that doing this makes it more intuitive for people when they are looking for the HD, and also one can immediately the computer name on the desktop (big help for remote support), and it even helps me when telling one “Mac HD” from the other in case of Target Disk booting.

Feedback welcome on whether I am going down the right path with this.



-- We are setting the Hostname to be the same as the ActiveDirectoryName


try
	set hostessKid to do shell script "scutil --get LocalHostName"
on error -- for example if LocalHostName is not set
	set hostessKid to computer name of (system info)
end try

-- this looks at your current computer's name and gives it back to you as variable hostessKid
display dialog "The current name of your computer is " & hostessKid with icon note buttons {"Change Name", "I'd prefer not to"} default button 1
if result = {button returned:"Change Name"} then
	try -- now it is accepting your input and it is going to verify that your new name is acceptable
		set countUp to 99 -- netbios limitations are still in effect.  names must be 15 characters or less
		repeat while countUp is greater than 15 -- we will loop through this until the name is 15 characters or less
			display dialog "Enter the Active Directory Name you want to give this Mac. Only alphanumeric characters and spaces will be accepted. Name MUST be 15 characters or less." default answer "currently " & hostessKid
			set newNameRaw to text returned of result -- the variable newNameRaw takes the input, and will process it to verify whether it has 15 characters or less
			set countUp to count (newNameRaw) -- this counts the characters in newNameRaw
			if countUp is greater than 15 then
				display dialog "must be 15 characters or less"
			end if
		end repeat
	end try
	-- if you have got this far, then newNameCooked will apply a SED command to remove all non-acceptable characters and empty spaces
	try
		set newNameCooked to do shell script ("<<<" & quoted form of newNameRaw & " sed -E 's/[[:space:]]*[^a-zA-Z0-9-]*//g'")
	end try
	
	display dialog "We're about to change the HostName, LocalHostName, and ComputerName to " & newNameCooked buttons {"do it", "cold feet"} default button "do it" cancel button "cold feet"
	do shell script "scutil --set ComputerName " & newNameCooked with administrator privileges
	do shell script "scutil --set HostName " & newNameCooked with administrator privileges
	do shell script "scutil --set LocalHostName " & newNameCooked with administrator privileges
	
	
	set hostessKid to computer name of (system info)
	display dialog "your Mac's name (i.e., HostName) is now " & hostessKid buttons {"groovy", "not impressed"} default button "groovy"
	
else
	display alert "Okay, then.  We'll stay as we are.  Good times!" --buttons {"Gotcha"} giving up after 5
end if


set hdName to hostessKid & "HD"
-- this will change the name of your HD volume to the name of the computer with "HD" tacked on the end
tell application "Finder"
	set name of (path to startup disk) to hdName
end tell


-- this changes the defaults of the Mac so that the local hard drive and connected servers show up, by default, on the desktop
-- dajames 7/29/14, 11:30 AM

tell application "Finder"
	set desktop shows hard disks of Finder preferences to true
	set desktop shows connected servers of Finder preferences to true
end tell

Oh, and as a note, this link helped me a lot with configuring the repeat loop correctly. I will credit it in all future updates of the code.

http://www.mactech.com/articles/mactech/Vol.20/20.12/RepeatLoops/index.html