Skip to content

Instantly share code, notes, and snippets.

@chrisschreiner
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisschreiner/bbbd7c87540f60093f3e to your computer and use it in GitHub Desktop.
Save chrisschreiner/bbbd7c87540f60093f3e to your computer and use it in GitHub Desktop.
import Foundation
import XCPlayground
enum E:ErrorType {
case SomeThingHappened(payload:String)
}
let filename = XCPSharedDataDirectoryPath.stringByAppendingPathComponent("output.plist")
func run(filename:String) throws -> String {
let pipe = NSPipe()
let task = NSTask()
let fileBuffer = pipe.fileHandleForReading
task.launchPath = "/usr/sbin/system_profiler"
task.arguments = ["-xml","SPApplicationsDataType","-detailLevel","mini"]
task.standardOutput = pipe
task.launch()
let data = fileBuffer.readDataToEndOfFile()
if data.writeToFile(filename, atomically: true) {
return filename
} else {
throw E.SomeThingHappened(payload:filename)
}
}
func listOfApps(filename:String) -> [String] {
let d = NSArray(contentsOfFile: filename) as! [AnyObject]
let d2 = d.first! as! [String:AnyObject]
var result:[String] = []
for each in d2["_items"]! as! [[String:AnyObject]] {
let name = each["_name"]! as! String
result.append(name)
}
return result
}
do {
let result = try listOfApps(run(filename))
for i in 0...5 {print(result[i])}
} catch E.SomeThingHappened(let fname) {
print("something happened \(fname)")
} catch {
print("I have no idea what happened \(error)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment