Microsoft Excel

Ron de Bruin
Excel Automation

Microsoft MVP Program

Mac unique indentifier

This code can be useful if you want your code only to work on one machine. From what I read UUID used in the macro below was added in Leopard (Mac OS X 10.5). So if you have a old Mac the code below will not work, if correct the second macro example will work in older versions also.

Sub MachineUniqueIndentifierMac_1()
'Get the machine unique indentifier of a Mac
    Dim ScriptToRun As String
    Dim Answer As String

    ScriptToRun = _
    "set uuid to do shell script ""system_profiler SPHardwareDataType" & _
                " | awk '/Hardware UUID:/ {print $NF}'"""

    On Error Resume Next
    Answer = MacScript(ScriptToRun)
    On Error GoTo 0

    MsgBox Answer

    'Or add it to a cell in your worksheet so you can test
    'if your workbook is running on the correct machine
    'Sheets(1).Range("A1").Value = Answer

End Sub

Another idea suggested by Nigel Garvey in the Apple script forum which seems to work is to read the UUID from the name of one of the files in ~/Library/Preferences/ByHost/: This seems also work in older versions of Mac OS (Tiger, Jaguar and Panther) also, please give feedback if that is true or not.

Sub MachineUniqueIndentifierMac_2()
'Get the machine unique indentifier of a Mac
    Dim ScriptToRun As String
    Dim Answer As String

    ScriptToRun = _
    "set uuid to (do shell script ""ls -t ~/Library/Preferences/ByHost" & _
                " | sed -En '1,1 s/^.+[.]([^.]+)[.]plist.*$/\\1/p'"")"

    On Error Resume Next
    Answer = MacScript(ScriptToRun)
    On Error GoTo 0

    MsgBox Answer

    'Or add it to a cell in your worksheet so you can test
    'if your workbook is running on the correct machine
    'Sheets(1).Range("A1").Value = Answer

End Sub