convert_conf.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/swift
  2. // main.swift
  3. // nscolor
  4. //
  5. // Created by Fabrizio FD. Destro on 28/12/18.
  6. // Copyright © 2018 Fabrizio FD. Destro. All rights reserved.
  7. //
  8. import Foundation
  9. import AppKit
  10. func hex(color: NSColor) -> String {
  11. return String(format: "#%02x%02x%02x", Int(color.redComponent * 0xFF), Int(color.greenComponent * 0xFF), Int(color.blueComponent * 0xFF))
  12. }
  13. func process_color(field: String, data: Data) {
  14. let color = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSColor
  15. print("\(field) \(hex(color: color))");
  16. }
  17. func generate_conf_line(field: String, key: String, dictionary: NSDictionary){
  18. if let data = dictionary[key] {
  19. process_color(field: field, data: data as! Data)
  20. }
  21. }
  22. func process(filename: String) {
  23. let plist = NSDictionary(contentsOfFile: filename)!
  24. generate_conf_line(field: "background", key: "BackgroundColor", dictionary: plist)
  25. generate_conf_line(field: "foreground", key: "TextColor", dictionary: plist)
  26. generate_conf_line(field: "cursor", key: "CursorColor", dictionary: plist)
  27. generate_conf_line(field: "selection_background", key: "SelectionColor", dictionary: plist)
  28. generate_conf_line(field: "color0", key: "ANSIBlackColor", dictionary: plist)
  29. generate_conf_line(field: "color8", key: "ANSIBrightBlackColor", dictionary: plist)
  30. generate_conf_line(field: "color1", key: "ANSIRedColor", dictionary: plist)
  31. generate_conf_line(field: "color9", key: "ANSIBrightRedColor", dictionary: plist)
  32. generate_conf_line(field: "color2", key: "ANSIGreenColor", dictionary: plist)
  33. generate_conf_line(field: "color10", key: "ANSIBrightGreenColor", dictionary: plist)
  34. generate_conf_line(field: "color3", key: "ANSIYellowColor", dictionary: plist)
  35. generate_conf_line(field: "color11", key: "ANSIBrightYellowColor", dictionary: plist)
  36. generate_conf_line(field: "color4", key: "ANSIBlueColor", dictionary: plist)
  37. generate_conf_line(field: "color12", key: "ANSIBrightBlueColor", dictionary: plist)
  38. generate_conf_line(field: "color5", key: "ANSIMagentaColor", dictionary: plist)
  39. generate_conf_line(field: "color13", key: "ANSIBrightMagentaColor", dictionary: plist)
  40. generate_conf_line(field: "color6", key: "ANSICyanColor", dictionary: plist)
  41. generate_conf_line(field: "color14", key: "ANSIBrightCyanColor", dictionary: plist)
  42. generate_conf_line(field: "color7", key: "ANSIWhiteColor", dictionary: plist)
  43. generate_conf_line(field: "color15", key: "ANSIBrightWhiteColor", dictionary: plist)
  44. }
  45. if (CommandLine.argc == 2) {
  46. let filename = CommandLine.arguments[1]
  47. process(filename: filename)
  48. } else {
  49. print("Missing plist's path.")
  50. }