applescript to auto-generate names

My company has a specific naming policy for newly imaged (and re-imaged) macs. Since mac imaging can happen in any of 6 locations worldwide, they needed a way to ensure that all macs followed the same naming convention while obviously receiving unique, and sequential, names. I developed this applescript to solve such a dilemma and thought it might be useful to anyone in a similar situation. Essentially what the script does is read the last used name entry from a central file, increment it by one to give the next available name, attempt to write it to file, and if successful, outputs it to the user as the next name to use. The script has the ability to accept an integer and provide a range of available names if so desired, as is often the case at my company when we image 100 macs at a time. I guess you could just run the app 100 times, but I usually have better things to do than that, don’t you?

--########################################################### START HEADER
--## Use this script freely as long as you keep this header intact
--## GetNextMacName.scpt written by Andrew Caldwell, 9/2/2011
--## Although this script is tested and functional on 10.5 and 10.6, your mileage may vary and I accept NO responsibility for you
--## using this script on your own Mac(s)
--########################################################### END HEADER
--          GetNextMacName       --
 -- Written by Andrew Caldwell   --
 --           9/2/2011                --
 -- this script reads a file on a shared drive, finds the last name entry, increments it by one, and writes the new name to the end of the file. It then displays the new name to the user. The script assumes the comany's alphanumeric naming convention: <letter>0<number>.
 -- This script is provided as-is with no warranty either expressed or implied. Use at your own risk. I annot be held responsible for anything you choose to do with the code contained herein. Please make sure to give credit where credit is due if you choose to use this script.
-- variable initialization
set nextName to 0 as integer
 set nameRange to 0 as integer
 -- do you want the next available name or a range of available names (e.g.: Mac DAC)? If we put this dialog first, the user can quit the app if they opened it accidentally and it will not lock any files or attempt to mount server shares.
display dialog "" buttons {"Get next Mac name", "Get a range of Mac names"}
 set opMode to button returned of result
-- mount the server that contains the mac names file
 try
      mount volume "afp://<server name>/<share name>"
      delay 1
 on error
      try
           mount volume "afp://<server name>/<share name>"
           delay 1
      on error
           display dialog "There was an error mounting the server <server name>. The server may be unavailable at this time. Please inform the Network Administrator if this problem continues." buttons {"Ok"} default button 1 giving up after 10
 error number -124
          end try
 end try
-- specify the file that contains mac names
 set theFile to "Volumes:<share name>:MacNames.txt"
 -- Applescript locks the file when it opens, so there is no danger of another process editing the file while running this script. Likewise, Applescript cannot edit the file if another process has it locked, so there is no danger of Applescript editing it while another program has it open..
try
      set refNumber to (open for access file theFile with write permission)
 on error
      try
           set refNumber to (open for access file theFile with write permission)
      on error
           display dialog "The file MacNames.txt cannot be opened for write access by this script. The file may already be opened by another program. Please inform the ISD Desktop team if this problem continues." buttons {"Ok"} default button 1 giving up after 10
           error number -47
      end try
 end try
-- read the last line of the file. This is the last mac name used. This line will be incremented by 1 to give the next name
try
      set lastName to last paragraph of (read refNumber)
 on error
      try
           set lastName to last paragraph of (read refNumber)
      on error
           display dialog "Could not get the last item in the file MacNames.txt. The file may be busy. Please contact ISD Desktop support if this problem continues." buttons {"Ok"} default button 1 giving up after 10
           error number -51
      end try
 end try
-- file write operations depend on how many names are being requested
if (opMode = "Get next Mac name") then
 -- if we are only interested in the next available name, increment the last name by one, write it to the EOF, and display the available name
      set lastNameLetter to do shell script "echo " & lastName & " | cut -d 0 -f1 | awk '{print $1}'"
      set lastNameNum to do shell script "echo " & lastName & " | cut -d 0 -f2,3,4 | awk '{print $1}'" --this drops the leading zero, so make sure to add it back below
      set nextNameNum to lastNameNum + 1
      set nextName to lastNameLetter & "0" & nextNameNum
      try
           write return & nextName to refNumber
      on error
           display dialog "Could not write to the file. The file may be locked by another program. MacNames.txt is unchanged. Please contact the ISD Desktop team if this problem continues." buttons {"Ok"} default button 1 giving up after 10
           error number -61
      end try
      display alert "Next available name: " & nextName
 else
-- if we want a range of names, set up a repeat loop for the number of names we want (use a second repeat loop to make sure we get valid input), write them to the file, then output them as a list that can be copy/pasted.

      -- Start a repeat loop to keep asking the user for a range until a positive integer value is entered
 set gotRange to false
   repeat until gotRange is true
           display dialog "Please enter a positive, non-zero integer for how many names you want:" default answer ""
           try
                set nameRange to text returned of result as integer
                if (nameRange ≤ 0) then
                     display dialog "Integers less than 1 are invalid." buttons {"Ok"} default button 1 giving up after 10
                else
                     set gotRange to true
                end if
          on error
                display dialog "Invalid input. Enter positive integers only." buttons {"Ok"} default button 1 giving up after 5
          end try
 end repeat
-- set up a blank list to write each available name to
 set nameList to {}
      repeat nameRange times
           set lastNameLetter to do shell script "echo " & lastName & " | cut -d 0 -f1 | awk '{print $1}'"
           set lastNameNum to do shell script "echo " & lastName & " | cut -d 0 -f2,3,4 | awk '{print $1}'" --this drops the leading zero, so make sure to add it back below
           set nextNameNum to lastNameNum + 1
           set nextName to lastNameLetter & "0" & nextNameNum
-- append the nextName to the end of the list of available names
           try
                write return & nextName to refNumber
           on error
                display dialog "Could not write to the file. The file may be locked by another program. MacNames.txt is unchanged. Please contact the ISD Desktop team if this problem continues." buttons {"Ok"} default button 1 giving up after 10
                error number -61
           end try
           set nameList to nameList & nextName & return
-- an EOF error is generated if we try to read from the file a second time, but since we are incrementing the list by one each time for a specified number of times, we can simply advance the file by 1 using only varialbes. We are not required to read the file each time.
           set lastName to nextName
     end repeat
          display dialog "Next " & nameRange & " names: " & return default answer "" & nameList buttons {"OK"} default button 1
 end if
-- close access to the file
 close access refNumber

About this entry