Last active
January 15, 2018 18:44
-
-
Save paulgalow/7b74740c2b166f1ec53a6190610464fb to your computer and use it in GitHub Desktop.
A Swift version of https://scriptingosx.com/2018/01/get-an-icon-for-your-mac/ and https://gist.github.com/pudquick/1edd0d5e6c1ca5a1486bf727d071664e to create an icon for your Mac.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import AppKit | |
func generateMacIcon(dimension: CGFloat, name iconName: String) { | |
// Make sure our maximum rendering size does not exceed 512 px | |
guard dimension <= 512 else { | |
print("Error: Maximum dimension allowed is 512") | |
return | |
} | |
// Make sure our minimum rendering size is greater than 0 | |
guard dimension > 0 else { | |
print("Error: Please provide a non-negative dimension value") | |
return | |
} | |
// Try to create an NSImage of the computer model if not bail out | |
guard let image = NSImage(named: NSImage.Name.computer) else { return } | |
// Set image size. | |
image.size = NSMakeSize(dimension / 2, dimension / 2) | |
// Create CGImage from NSImage | |
if let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) { | |
let imageRep = NSBitmapImageRep.init(cgImage: cgImage) | |
let pngData = imageRep.representation(using: .png, properties: [:]) | |
// Set current directory as output directory | |
let destinationDir = URL(fileURLWithPath: "\(FileManager().currentDirectoryPath)") | |
// Write PNG image to disk | |
do { | |
try pngData?.write(to: destinationDir.appendingPathComponent("\(iconName).png"), options: .withoutOverwriting) | |
} catch { | |
print(error.localizedDescription) | |
} | |
} | |
} | |
generateMacIcon(dimension: 512, name: "computer") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment