I am attempting to use the output from the system_profiler tool to gather a record of installed applications on the current machine. The following command outputs XML in a plist format:
system_profiler -nospawn SPApplicationsDataType -xml -detailLevel mini
and outputs something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>_SPCommandLineArguments</key>
<array>
<string>/usr/sbin/system_profiler</string>
<string>-nospawn</string>
<string>SPApplicationsDataType</string>
<string>-xml</string>
<string>-detailLevel</string>
<string>mini</string>
</array>
<key>_SPCompletionInterval</key>
<real>8.4461650252342224</real>
<key>_dataType</key>
<string>SPApplicationsDataType</string>
<key>_detailLevel</key>
<integer>1</integer>
<key>_items</key>
<array>
<dict>
<key>_name</key>
<string>Terminal</string>
<key>has64BitIntelCode</key>
<string>yes</string>
<key>lastModified</key>
<date>2014-09-30T21:28:15Z</date>
<key>obtained_from</key>
<string>apple</string>
<key>path</key>
<string>/Applications/Utilities/Terminal.app</string>
<key>runtime_environment</key>
<string>arch_x86</string>
<key>signed_by</key>
<array>
<string>Software Signing</string>
<string>Apple Code Signing Certification Authority</string>
<string>Apple Root CA</string>
</array>
<key>version</key>
<string>2.5</string>
</dict>
which just goes on to list all of the installed applications on the system.
However, while attempting to assign that data to an NSDictionary
in order to access the elements, one way or another I end up receiving this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I cannot figure out why this is and every attempt at gathering this information always leads to the error. Here is the current code that I'm attempting to use:
var path = NSBundle.mainBundle().pathForResource("/Users/Tyler/Documents/data", ofType: "plist")
var dict = NSDictionary(contentsOfFile: path!)
I've also tried:
if let path = NSBundle.mainBundle().pathForResource("/Users/Tyler/Documents/data", ofType: "plist") {
myDict = NSDictionary(contentsOfFile: path)
}
This of course is after exporting the returned data to its own file, just so I don't have to wait every time for the command to be run. Ideally I would wish to take the output of the command directly with an NSTask
and receiving the standardOutput.
Any information in this matter would be greatly appreciated! I've really come to an impasse with this situation.
Thanks!