alexzielenski / Mousecape

Cursor Manager for OSX
http://www.alexzielenski.com
Other
1.42k stars 168 forks source link

Exporting cursor images from cape file #246

Open llsc12 opened 1 month ago

llsc12 commented 1 month ago

Dragging images out of the editor into a folder doesn't copy it but rather removes it and there seems to be no way to export images as a result. Thanks in advance.

llsc12 commented 1 month ago
import PlaygroundSupport
import Foundation

// put file.cape in the Resources folder of the macos playground

let fileURL = Bundle.main.url(forResource: "file", withExtension: "cape")!

let dec = PropertyListDecoder()

struct CapeFile: Codable {
  let Author: String
  let Cursors: [String: Cursor]

  struct Cursor: Codable {
    let Representations: [Data]
  }
}

let cape = try dec.decode(CapeFile.self, from: Data(contentsOf: fileURL))

// this iterates through capes and writes the tiff files
// saved to ~/Desktop/dumped/
try cape.Cursors.forEach { (key: String, value: CapeFile.Cursor) in
  let loc = URL.desktopDirectory.appendingPathComponent("dumped").appendingPathComponent(key).appendingPathExtension("tiff")
  try FileManager.default.createDirectory(at: URL.desktopDirectory.appendingPathComponent("dumped"), withIntermediateDirectories: true)
  try value.Representations.first?.write(to: loc)
}

In case anyone needs this, hope it helps.