Skip to content

Instantly share code, notes, and snippets.

@WFT
Last active September 27, 2024 20:37
Show Gist options
  • Save WFT/8c293ee16362dd2262d0e5a00998e3a7 to your computer and use it in GitHub Desktop.
Save WFT/8c293ee16362dd2262d0e5a00998e3a7 to your computer and use it in GitHub Desktop.
Reads a plist with top-level dictionary and converts to JSON
import Foundation
// Reads a plist with top-level dictionary and converts to JSON. JSON printed to stdout.
// Run like this: `swift plist-to-json.swift myplist.plist`
guard CommandLine.arguments.count == 2 else {
let msg = "Usage: \(CommandLine.arguments[0]) <path-to-input.plist>\n"
try FileHandle.standardError.write(contentsOf: Data(msg.utf8))
exit(1)
}
let dict = try NSDictionary(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]), error: ())
let data = try JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys])
try FileHandle.standardOutput.write(contentsOf: data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment