Plist serial number extraction to text file or spreadsheet

I need to get the serial numbers for all the iPods that are being plugged into my machine so that they can be logged. Currently we log them all by reading them off for verification, but This would allow it to be a one person operation. Also if i could some how manage to also get size and model that would be awesome, but I just need some help because I am at a stopping point mentally. Thanks in advanced for all the help.

set t to do shell script “plutil -convert xml1 -o - ~/Library/Preferences/com.apple.iPod.plist”
set extract to extractBetween(t, “Serial Number
”, “”)

to extractBetween(SearchText, startText, endText)
set tid to AppleScript’s text item delimiters – save them for later.
set AppleScript’s text item delimiters to startText – find the first one.
set liste to text items of SearchText
set AppleScript’s text item delimiters to endText – find the end one.
set extracts to {}
repeat with subText in liste
if subText contains endText then
copy text item 1 of subText to end of extracts
end if
end repeat
set AppleScript’s text item delimiters to tid – back to original values.
return extracts
tell application “Finder”

end tell
set t to ((desktop as text) & "test.txt")

end extractBetween

Hi,

System Events can parse property list files quite comfortable.
As far as I can see in my iPod file there is no size key.
This is a version which gathers serial number and device class.
The result deviceInfo is a record


set iPodPlist to (path to preferences folder as text) & "com.apple.iPod.plist"
tell application "System Events"
	set iPodPlistFile to property list file iPodPlist
	set devices to property list item "Devices" of contents of iPodPlistFile
	set deviceInfo to {}
	repeat with aDevice in property list items of devices
		tell aDevice to set {serialNumber, deviceClass} to {value of property list item "Serial Number", value of property list item "Device Class"}
		set end of deviceInfo to {|Device Class|:deviceClass, |Serial Number|:serialNumber}
	end repeat
end tell


That gets me half way there and really helps out. Do you have any ideas on how to bring this into a table or a text file that could be separated out line by line for inventory purposes?

this writes the result “ one device per line and the data tab separated - into a file iPodData.txt on desktop


set iPodPlist to (path to preferences folder as text) & "com.apple.iPod.plist"
set logFile to ((path to desktop as text) & "iPodData.txt")
try
	set fileReference to open for access file logFile with write permission
	tell application "System Events"
		set iPodPlistFile to property list file iPodPlist
		set devices to property list item "Devices" of contents of iPodPlistFile
		repeat with aDevice in property list items of devices
			tell aDevice to set {serialNumber, deviceClass} to {value of property list item "Serial Number", value of property list item "Device Class"}
			tell me to write (deviceClass & tab & serialNumber & return) to fileReference
		end repeat
	end tell
	close access fileReference
on error
	try
		close access file logFile
	end try
end try

Thank you very much! I have been working on this every day for the past week during my breaks at work and this is the relief I needed. Much Appeciated StefanK!

one last question…

why does this not work?

set MyName to do shell script “whoami”
set the plist_ to POSIX path of (“Users/” & MyName & “/Library/Preferences/com.apple.iPod.plist”)

tell application “System Events”
set p_list to property list file (plist_)
value of p_list
end tell

tell application “Finder”
if exists file plist_ then
display dialog “Scan Next Grouping of Device?”
delete file plist_
empty trash
end if
end tell

the Finder doesn’t like POSIX paths


set the plist_ to (path to preferences folder as text) & "com.apple.iPod.plist" -- points always to the current user and is HFS path

tell application "Finder"
	if exists file plist_ then
		display dialog "Scan Next Grouping of Device?"
		delete file plist_
		empty trash
	end if
end tell

Thank you!!!