Change Computer Name

Hi everyone. I’m not new to Mac’s but I am new to the use of Apple Script to automate actions. I work in a fairly large environment where my department supports probably 200 to 300 macs with the number increasing rather rapidly. Currently we deploy our images via DeployStudio but set the computer name and bind it to the AD manually. I wanted to start working on a script to bind the machines to AD but starting with renaming the system would be a good intro.

Our current naming convention involves a 2 to 4 character department heading, depending on where the machine is deployed, followed by a hyphen and then the first 7 characters of the serial number. An example would be AAA-1234567.

I know a simple script to pull the serial number so I think that would come in handy, however here is what I’m thinking about and let me know if this is logical. The first thing that needs to happen is that the administrator needs to be prompted for the department heading that will be used (a popup box maybe). Once the heading is entered the script would pull the serial number, cut it down the seven characters, combine it with the selected heading, and enter it in the sharing pane for the computer name. Would this be doable using applescript? Anyone care to point me in the right direction? Thanks.

hi and welcome to the forum

This should help tot get you started, i’m not sure about entering the result into the sharing pane, as this may need to be done via UI scripting, there may be a better way though.

I did note that on my mac mini the

set _Name to (characters 1 thru -5 of theserial as text)

needed to be

set _Name to (characters 1 thru -6 of theserial as text)
-- OSX 10.7.5 MacBook Pro
set _Department to choose from list {"AA", "AAA", "AAAA"} with prompt "Select Department" default items {"AA"} OK button name "Ok" cancel button name "Cancel"
set theserial to do shell script "system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'"
set _Name to (characters 1 thru -5 of theserial as text)
set _FinalName to _Department & "-" & _Name as string
display dialog _FinalName

Hi.

This works in Mac OS 10.6.8.

set c to 0
-- Repeat until the user either enters a 2- to 4-character code or cancels.
repeat while ((c < 2) or (c > 4))
	set heading to text returned of (display dialog "Please enter your department heading (2-4 characters)" default answer "")
	set c to (count heading)
end repeat

-- In the following shell script, "system_profiler" gets the "Harware Overview" details,
-- "sed" parses out the first seven characters of the serial number and prepends the department heading obtained above,
-- "scutil" sets the computer name (not the host name) to the result. (Super-user access required.)
do shell script ("system_profiler SPHardwareDataType | sed -En 's/^.*Serial Number \\(system\\):[^[:alnum:]]*([[:alnum:]]{7}).*$/" & heading & "-\\1/p' | scutil --set ComputerName")

Thanks for all the help. I have the script working although I will say I sort of hacked together both your suggestions. I’m not trying to get it to write the DHCP client ID which I think would simply be a matter of knowing the field name that scutil would need to write to. Here is what I have so far. The display dialog lines will be removed once I can get the DHCP Client ID changed.


set _Heading to choose from list {"AHEC", "CFL", "COMP", "CRSO", "DCCRP", "DCRU", "DERM", "DFC", "DHC", "DLAB", "DLAR", "DMG", "DOM", "FRP", "GI", "GME", "IA", "IMMU", "INDP", "IPIDH", "IRB", "MCDEV", "MEDE", "MEH", "MRC", "OAWA", "OBGN", "OCRC", "OLV", "OOC", "OPS", "ORTHO", "PEDS", "PULM", "RA", "RON"} with prompt "Select Department" default items {"AA"} OK button name "Ok" cancel button name "Cancel"
set theserial to do shell script "system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'"
set _Name to (characters 1 thru -5 of theserial as text)
set _FinalName to _Heading & "-" & _Name as string
do shell script "scutil --set ComputerName " & _FinalName
set _EthernetClientID to _Heading & "-" & "e" & _Name as string
display dialog _EthernetClientID
set _AirportClientID to _Heading & "-" & "w" & _Name as string
display dialog _AirportClientID
do shell script "scutil --set EthernetClientID " & _EthernetClientID as string 
do shell script "scutil --set AirportClientID " & _AirportClientID as string

In those last two lines the --set EthernetClientID and AirportClientID are just place holders until I find if there is actually a field name to use. The client IDs are appended with a w to indicate a wireless connection and e to indicate ethernet.

The problem here is that I do not know what the name of the actual client ID field would be for scutil to write to. I have poked around on the internet a bit but haven’t come up with anything yet. Anyone have any suggestions. I’m going to keep working on it on my end.

Hi.

I can’t help with client ID fields (there’s no mention of them in scutil’s “man” page), but here are a few comments about the AppleScript code, if you’ll take them:

‘choose from list’, unlike other dialogs, returns ‘false’ if the user clicks the cancel button, so you need to deal with this possibility. If you want it to stop the script like any other dialog, simply generate a “User canceled.” error:

set _Heading to choose from list {"AHEC", "CFL", "COMP", "CRSO", "DCCRP", "DCRU", "DERM", "DFC", "DHC", "DLAB", "DLAR", "DMG", "DOM", "FRP", "GI", "GME", "IA", "IMMU", "INDP", "IPIDH", "IRB", "MCDEV", "MEDE", "MEH", "MRC", "OAWA", "OBGN", "OCRC", "OLV", "OOC", "OPS", "ORTHO", "PEDS", "PULM", "RA", "RON"} with prompt "Select Department" default items {"AA"} OK button name "Ok" cancel button name "Cancel"
if (_Heading is false) then error number -128 -- Stop the script if cancel button clicked.

‘characters 1 thru -5 of theserial as text’ should be ‘text 1 thru -5 of theserial’. The former is is a two-stage process which creates a list of characters and then coerces it to text, so the final result is influenced by the current value of AppleScript’s text item delimiters. ‘text 1 thru -5 .’ extracts the required text directly and is thus more efficient and not affected by text item delimiters.

A similar consideration applies in the three lines ending with ‘as string’ after that. ‘_Heading’ is a list (the result from ‘choose from list’). Concatenating text values to a list results in another list containing the original list’s contents and the concatenated values as separate items ” and coercing a list to string (or text), as I said above, is influenced by the value of AppleScript’s text item delimiters. It would be better, once you’ve established that ‘_Heading’ isn’t ‘false’, to extract the text from it then. That way, you’ll be concatenating text directly to text, the coercion will be unnecessary, and the delimiters won’t get a look in:

set _Heading to choose from list {"AHEC", "CFL", "COMP", "CRSO", "DCCRP", "DCRU", "DERM", "DFC", "DHC", "DLAB", "DLAR", "DMG", "DOM", "FRP", "GI", "GME", "IA", "IMMU", "INDP", "IPIDH", "IRB", "MCDEV", "MEDE", "MEH", "MRC", "OAWA", "OBGN", "OCRC", "OLV", "OOC", "OPS", "ORTHO", "PEDS", "PULM", "RA", "RON"} with prompt "Select Department" default items {"AA"} OK button name "Ok" cancel button name "Cancel"
if (_Heading is false) then error number -128 -- Stop the script if cancel button clicked.
set _Heading to item 1 of _Heading -- If not stopped, get the text from the returned list.

set theserial to do shell script "system_profiler SPHardwareDataType | grep 'Serial Number (system)' | awk '{print $NF}'"
set _Name to (text 1 thru -5 of theserial) -- Extract text directly.
set _FinalName to _Heading & "-" & _Name -- Concatenate text to text.
do shell script "scutil --set ComputerName " & _FinalName
set _EthernetClientID to _Heading & "-" & "e" & _Name -- Ditto.
display dialog _EthernetClientID
set _AirportClientID to _Heading & "-" & "w" & _Name -- Ditto.
display dialog _AirportClientID

In your last two lines, ‘as’ isn’t a coercion but an optional parameter of ‘do shell script’. It tells AppleScript how to understand the data returned by the shell script rather than telling it to coerce them to another form. To coerce a shell script result to string, you’d use parenthesis to separate the ‘as’ from the ‘do shell script’:

(do shell script "scutil --set EthernetClientID " & _EthernetClientID) as string

But ‘do shell script’ returns results as text by default anyway, so ‘as string’ is superfluous at best and may be wrong as a parameter in some cases.

Thanks for the reply. I will look into the changes you suggested. I’m still new to this so I just trying to get the hang of everything. The help is much appreciated.