How-to: Get Computer's Serial Number (Mac OS X)


#1

Posting in case someone else needs to know how to do this :slight_smile:


So there are three terminal commands I’ve found that outputs your serial number of Mac OS X. One is SUPER slow and takes about 30+seconds to return a value which freezes your app. But found two that are instantaneous:

  • $ ioreg -l | grep IOPlatformSerialNumber
  • $ system_profiler SPHardwareDataType | grep Serial

Both return a value like so:

$ system_profiler SPHardwareDataType | grep Serial
// returns "      Serial Number (system): XX_YOUR_SERIAL#_XX"

So use ofSystem() to get this value and parse it:

string macSerial = ofSystem( "system_profiler SPHardwareDataType | grep Serial" );
int startIndex = macSerial.find_last_of( " " ) + 1;
int endIndex = macSerial.size() - 1 - startIndex;
macSerial = macSerial.substr( startIndex, endIndex );

cout << macSerial << endl;
// XX_YOUR_SERIAL#_XX

Boom!


I don’t have access to a PC right now but I’ll update this post with how to do this on PC unless someone else beats me to it. Thanks!