Last active
August 29, 2015 14:24
-
-
Save chrisschreiner/bbbd7c87540f60093f3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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