Script for renamig mac

Hi there,

Does anyone know if there is script to read a serial number from Mac firmware and then read the text file which will have computer name associated to that serial number and use that computer name to rename mac machine.


This will pull the serial number

set SN to last word of paragraph -2 of (do shell script "system_profiler SPHardwareDataType")

Then depending on the files location and formatting will depend on how you get the computer name. From that point the name can be set with the shell command ‘/usr/sbin/scutil’

Hope that helps.

Hi James Nierodzik,

Let say that I have a text file in the system folder named compname.txt. And in that text file I have two columns named as:

Serial Number Computer Name
w3423434 testcom1

Space between those columns is one tab (not sure if needed this). With the current script that you have proved I can set SN equal to serial number but now how do tell my script to read the text file and then find the serial number that it has for SN and then read the computername that is associated with that serial number and use that number to set machine name.

This may or may not be so easy for Mac Pro like you and the others on this site but for me (new to mac), it is quite a difficult.


This should do what you are looking for. In this case the file is named serial-name.txt in the System folder (/System), but you could name it or move it anywhere you want. This assumes a tab between the serial number and machine name as you had specified. Hope this helps!

set SN to last word of paragraph -2 of (do shell script "/usr/sbin/system_profiler SPHardwareDataType")
set theName to do shell script "/usr/bin/grep " & SN & " /System/serial-name.txt | /usr/bin/awk '{print $2}'"

-- Your command to set the name here using '/usr/sbin/scutil'

And because I like to do things with the least amount of commands here’s a one-liner to determine the name.

set theName to do shell script "/usr/bin/grep `/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/grep \"Serial Number: \" | /usr/bin/awk '{print $3}'` /System/serial-name.txt | /usr/bin/awk '{print $2}'"

less amount :wink:

set theName to do shell script "/usr/bin/grep `/usr/sbin/system_profiler SPHardwareDataType |  /usr/bin/awk '/Serial Number:/ {print $3}'` /System/serial-name.txt | /usr/bin/awk '{print $2}'"

touche :smiley:

Hi Guys,

I have tried both scripts (using script Editor) and both work exactly the same. So far my understanding of this script is that this scripts reads the serial number numbers and matches computer name (from reading the serial-name.txt) and then creates a variable (SN) equal to that name. Is that correct? If this is then how about the next part of renaming machine?


Yep both versions are doing what as you said and they are essentially the same script. For the next portion you would then do something along the lines of this.

do shell script "/usr/bin/grep `/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number:/ {print $3}'` /System/serial-name.txt | /usr/bin/awk -F\"	\" '{print $2}' | /usr/sbin/scutil --set ComputerName"

Edit: Slight modification to account for spaces in the computer name

Hi James,

I have tried the new script and it names the machine using the value from the first script and this is just great. Do I have to combine those two together in order to do set the name as a variable and then use the variable as a machine name? Like this:

I have tried to run the script like this and got error below:

“AppleScript Error
Could not open prefs: No such key”

And second part of the script was highlighted.


No, you only have to use the second script… Slightly modified again

do shell script "/usr/bin/grep `/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number:/ {print $3}'` /System/serial-name.txt | /usr/bin/awk -F\"\\t\" '{print $2}' | /usr/sbin/scutil --set ComputerName"

That will find the serial, match it to the name, and then set the name. When you run that what is the feedback you receive? Are you on tiger or leopard?

Hi James,

I have tried this script and it does everything that I have wanted this to do. I have tried to run this script as a test several times and I have kept changing the text file with different computer name but this script will not change variable value and always keeps the same value (or at least, it shows me the same value under Applescript Editor result window). Is there way to way to flush the variable value so that I can run multiple test before I deploy this our lab.


Note: I am using both Tiger and Leopard.

Hi music55, Can you provide a little more detail please as I don’t think I’m following exactly what you are saying. Are you saying you ran it once and it successfully changed the computer name, but on subsequent runs, after changing the name in the text file, the computer name is not changing?

In that case I’m not quite certain. On my Leopard machine I changed the name a few times and everything worked. Were you testing on Leopard or on Tiger? Can you provide samples of the text files you tried using on your tests?

Also make sure you are using the last script I posted as it address an issue if your computer name had spaces in it. Finally are you positive that the fields are tab separated in your text file?

Hi James,

I have a text file name serial-name.txt under system folder. In that text file, I have two columns as following with titles:

Serial Name (one tab between these two) Computer Name
W4534ERT Test12345

First time I ran the last script using Applescript Editor, script showed me the proper name (Test12345) in the result windows. Then I have tried changing computer name (in the text file) to something different (TestABC) then ran the same script and it still shows me the previous name in the result window (Test12345). Do you know why script did not show me newer name?


Hmm, well if using the latest script you should never see a result in Script Editor at all. Use this script

do shell script "/usr/bin/grep `/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number:/ {print $3}'` /System/serial-name.txt | /usr/bin/awk -F\"\\t\" '{print $2}' | /usr/sbin/scutil --set ComputerName"

Then verify in the terminal what your computer name is with this command

scutil --set ComputerName

After you do that then change the entry in the text field and run the Applescript again followed by the above shell command to verify that it has been changed.

Thanks James, script works now. Another thing that I was doing wrong that when you gave me the first script that showed computer name in result window but once script was changed, all I saw was quotation marks and never checked sharing panel to see if new computer name has changed. With new script, computer name changes every time I have changed the text file.

Another thing that I was wondering about that how do I save this script and where would I place it so that it run once after imaging?


Well that depends entirely on how you want to handle it which is a much larger conversation that I welcome if you would like communicate off list.

You basically have three options. Handle this via the Imaging Process, Deployment Process, or Post Deployment. Here are a few examples

Imaging Process: Build the script as a self deleting script that runs once
Deployment Process: Run the script as a postaction when deploying with NetRestore
Post Deployment: Run as a batch command through ARD

Hi James,

I have tried saving this script using applescript (compname.scpt) and once I click on it after saving it will not show me any result and just opens Applescript. Do you know how can I save this script and run it by clicking on it?

Also I do want to know how to use this script via Imaging process and please let me know how can I find learn more about that. We have over 300 macs and imaging process is really painful compare to windows (using firewire drive and manual configuration, such as: naming the machine and joining domain). So I would love learn more about this process if there is a better way of imaging Macs.


To make the script run like an application you need to set the File Format to either Application or Application Bundle when you do the save. In regards to your other questions please contact me off the board and I’ll be glad to help you out.