Skip to content

Instantly share code, notes, and snippets.

@danielbayley
Created February 25, 2023 15:27
Show Gist options
  • Save danielbayley/d7fd4e495eab6db20f718d2de27dd3cd to your computer and use it in GitHub Desktop.
Save danielbayley/d7fd4e495eab6db20f718d2de27dd3cd to your computer and use it in GitHub Desktop.
Set icon for file or folder from image, existing app or folder.
#! /usr/bin/xcrun swift
import Cocoa
let args = CommandLine.arguments[1...]
let (target, icon) = (URL(fileURLWithPath: args.first!).path, args[2])
var folder: ObjCBool = false
FileManager.default.fileExists(atPath: icon, isDirectory: &folder)
let image: NSImage = folder.boolValue
? NSWorkspace.shared.icon(forFile: icon)
: NSImage(byReferencingFile: icon)!
NSWorkspace.shared.setIcon(image, forFile: target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment