| 1 | import 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. |
| 5 | let sourceURL = URL(fileURLWithPath: CommandLine.arguments[1]) |
| 6 | let outputURL = URL(fileURLWithPath: CommandLine.arguments[2]) |
| 7 | guard 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) |
| 12 | else { fatalError("Cannot load or render the app’s icon") } |
| 13 | NSGraphicsContext.saveGraphicsState() |
| 14 | NSGraphicsContext.current = context |
| 15 | context.imageInterpolation = .high |
| 16 | let tile = NSRect(x: 100, y: 100, width: 824, height: 824) |
| 17 | NSBezierPath(roundedRect: tile, xRadius: 184, yRadius: 184).addClip() |
| 18 | source.draw(in: tile, from: .zero, operation: .copy, fraction: 1) |
| 19 | NSGraphicsContext.restoreGraphicsState() |
| 20 | guard let png = bitmap.representation(using: .png, properties: [:]) else { |
| 21 | fatalError("Cannot encode the app’s icon") |
| 22 | } |
| 23 | try png.write(to: outputURL) |
| 24 | |