Posted on 03-20-2012 12:19 PM
This is a short and simple Extension Attribute we're starting to use for Display serials. I don't believe all third-party displays will report a serial via system profiler, but Apple displays and a few third-party displays will.
#!/bin/sh
serial=`system_profiler SPDisplaysDataType | grep -i "Display Serial Number" | sed -e 's/^[ ]*//' | cut -d " " -f 4`
echo "<result>$serial</result>"
Posted on 03-20-2012 04:37 PM
Cool, your example works on my system (MacBook Air with Thunderbolt Display)
If your interested here a two other ways to get the same value that I was inspired to share while looking at your post
#!/bin/bash
serial=`system_profiler SPDisplaysDataType | awk '/Display Serial Number/{print $NF;exit}'`
echo "<result>$serial</serial>"
and a little more complicated example that I have been working on for generically parsing system profiler content as an extension attribute (still work in progress , as each script needs to be modified for the given key ). Worth nothing in both examples that they take the first Display Found
#!/usr/bin/python
__author__ = 'Zack Smith @acidprime'
__version__ = '1.0'
import plistlib
import subprocess
system_profiler = '/usr/sbin/system_profiler'
spx_key = 'spdisplays_display-serial-number'
# Working on this to make it more generic
def genReport():
spx = {}
# This is our key value schema
SPDisplaysDataType = {
spx_key : spx_key,
}
_dataTypes = {
'SPDisplaysDataType': SPDisplaysDataType,
}
dataTypes = _dataTypes.keys()
# run the system_profiler command with data types
arguments = [system_profiler,"-xml"] + dataTypes
getspx = subprocess.Popen(arguments, stdout=subprocess.PIPE)
spxOut, err = getspx.communicate()
rootObject = plistlib.readPlistFromString(spxOut)
# Parse datatype top level keys below
for array in rootObject:
for _dataType in _dataTypes:
if array['_dataType'] == _dataType:
_dataTypeSchema = _dataTypes[_dataType]
for key in _dataTypeSchema:
for item in array['_items']:
for spdisplays_ndrv in item['spdisplays_ndrvs']:
if spx_key in spdisplays_ndrv.keys():
spx[spx_key] = spdisplays_ndrv[spx_key]
break
return spx
spx = genReport()
try:
print '<result>%s</result>' % spx[spx_key]
except KeyError:
print '<result></result>'
https://gist.github.com/2142589
This while much bigger does have one nice feature, if you change the key value it will pull the first value for that key , i.e.
spx_key = 'spdisplays_resolution'
would display something like
<result>1366 x 768</result>
Posted on 04-11-2012 11:56 AM
Hey Zack your bash one has a typo
echo "<result>$serial</serial>"
Should be echo "<result>$serial</result>"
Works good tho!!