Getting names and serial numbers from USB devices

Hi All,

The title says it all: is there a way to get names and serial numbers (or any other unique code) for attached USB devices?

S.

set USB to do shell script "system_profiler -xml SPUSBDataType"

Adam yer too quick for me I was going to post the same thing, but decided to try and figure out how to parse XML to get the relevant info LOL.

wow, that was fast. I would be very happy if you can help me with the parsing. I want to get the name and serial of only detachable USB drives, and preferable also their volume name. Is that possible to filter out from it?

Well screw parsing XML :lol:

This works with a few different usb keys I have that I tested mounting through a variety of methods (keyboard hub, direct, external hub).

It returns a list of lists like so.

USB_Drives {{serial number 1, mount point1}, {serial number 2, mount point2}, etc.}

set USB_Drives to {}
set USB to paragraphs of (do shell script "system_profiler SPUSBDataType -detailLevel basic")
repeat with i from 1 to (count of USB)
	if item i of USB contains "Removable Media: Yes" then
		set {end of USB_Drives, i} to process_usb_device(i, USB)
	end if
end repeat

on process_usb_device(i, USB)
	set current_item to item i of USB
	repeat until current_item = ""
		set i to i + 1
		set current_item to item i of USB
		if current_item contains "Serial Number:" then
			set serial_num to text ((offset of ": " in current_item) + 2) through -1 of current_item
		else if current_item contains "Mount Point:" then
			set mount_point to text ((offset of ": " in current_item) + 2) through -1 of current_item
		end if
	end repeat
	return {{serial_num, mount_point}, i}
end process_usb_device

You are a true master!!!

Many thanks.

:smiley:

Glad to be of help :smiley:

Amen, James. I started to parse the XML and decided it was easier to attack the text, but didn’t know what the OP wanted to find.

Nicely done. :cool:

Is there an alternative shell script that will grab just the Serial Number?

I’m really struggling with this.

Bob Stuart
Think Data Pty Ltd
Noosa, Qld
Australia

This grabs only the serial numbers of removable media


set USB_Drives to {}
set USB to paragraphs of (do shell script "system_profiler SPUSBDataType -detailLevel basic | grep 'Removable\\|Serial'")
set i to 1
repeat until i > (count USB)
	if item i of USB contains "Removable Media: Yes" then
		set serial to item (i + 1) of USB
		set end of USB_Drives to text ((offset of ": " in serial) + 2) through -1 of serial
		set i to i + 1
	end if
	set i to i + 1
end repeat

Thanks so much, StefanK. I couldn’t have figured that out given a millenium to do it :slight_smile:

It gives me an event log that reads:

tell current application
do shell script “system_profiler SPUSBDataType -detailLevel basic | grep ‘Removable\|Serial’”
" USB Serial Adaptor:
Serial Number: 00022285
Serial Number: 7060IK
Removable Media: Yes
Serial Number: 0831410EBF80F12F"
offset of “: " in " Serial Number: 0831410EBF80F12F”
32
end tell

How would I quote that as a result to poke it into a FileMaker Pro global?

tell me to set field “Global” to result

.doesn’t work. What declared variable should I tell it to store?

Forgive me, I’m a total beginner :confused:

Bob Stuart.

Sorry, I don’t use FileMaker.
The result of the script is the variable USB_drives, which contains a list of strings (the serial numbers)
or an empty list, if no match is found

StephanK, thanks very much. That did the trick!

Bob.

Just one more thing:

On the mac, my USB thumbdrive is identified as “USB Serial Number : 0831410EBF80F12F”

On a PC, if I check the “Volume Serial Number” it is “3005-A8CO”

Anyone know why it’s different, or are we looking at two different kinds of Serial Number?

Bob.

On the Mac it’s just the information displayed in System Profiler

I am pretty sure they are different kinds of serial numbers.

“Volume Serial Number” sounds like it is part of the disk boot-record/partition-table or the filesystem itself (I vaguely remember such numbers reported in CHKDSK). Reformating the device will probably cause it to change. Googled turned up several hits, among them one that says the number is based on the date and time that the disk was formatted.

If the number reported by system_profiler is based on the hardware, then there is no reason the two serial numbers should be similar.

I’m just running this in OS X Yosemite, but it skips the serial number.
How can I get this working please?

Thanks

Matt

Hi,

I am running this in macOS Sierra.

Getting Error: “The variable serial_num is not defined.”
Same as post above (I think), has this ever been resolved?

Someone, anyone please help.
Cheers

The script appears to have four main problems:

  1. It only pretends to get the serial numbers and mount points of attached USB drives, whereas the OP asked for the serial numbers and names of devices.
  2. It assumes there’s only one volume/mount point per drive.
  3. The cue used to start parsing the text lines for the drives currently comes after the serial numbers (where they exist), so the handler never finds those lines.
  4. It looks as if the scripter thinks that changing the value of i in the run handler repeat has some influence on the repeat index.

Parsing the System Profiler text isn’t currently as simple as it appears to have been back in 2009. :confused:

Nigel, I have worked on something similar…
Check it out at: http://macscripter.net/viewtopic.php?id=45403 and see if you could help me out.
Thanks in advance