rastrillo / native Public

Clone
git clone https://amadan.net/rastrillo/native

Plain git — no account needed to clone.

Download

Download this file

1import AppKit
2
3// macOS displays the supplied silhouette; unlike iOS it does not mask an opaque master.
4// Keep the approved square master intact and derive a padded Mac icon from it.
5let sourceURL = URL(fileURLWithPath: CommandLine.arguments[1])
6let outputURL = URL(fileURLWithPath: CommandLine.arguments[2])
7guard let source = NSImage(contentsOf: sourceURL),
8 let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 1024, pixelsHigh: 1024,
9 bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false,
10 colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0),
11 let context = NSGraphicsContext(bitmapImageRep: bitmap)
12else { fatalError("Cannot load or render the app’s icon") }
13NSGraphicsContext.saveGraphicsState()
14NSGraphicsContext.current = context
15context.imageInterpolation = .high
16let tile = NSRect(x: 100, y: 100, width: 824, height: 824)
17NSBezierPath(roundedRect: tile, xRadius: 184, yRadius: 184).addClip()
18source.draw(in: tile, from: .zero, operation: .copy, fraction: 1)
19NSGraphicsContext.restoreGraphicsState()
20guard let png = bitmap.representation(using: .png, properties: [:]) else {
21 fatalError("Cannot encode the app’s icon")
22}
23try png.write(to: outputURL)
24