Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Cocoa
import Foundation
let pathToImage = "/some/path/to/an/image.png"
let pathToFolders = URL(fileURLWithPath: "/Some/Folder/Path/")
guard let image = NSImage(contentsOfFile: pathToImage) else {
fatalError("Unable to load image.")
}
do {
let fileManager = FileManager.default
let directoryContents = try fileManager.contentsOfDirectory(at: pathToFolders, includingPropertiesForKeys: [.isDirectoryKey], options: FileManager.DirectoryEnumerationOptions(rawValue: 0))
for subPathURL in directoryContents {
if (try subPathURL.resourceValues(forKeys: [.isDirectoryKey])).isDirectory ?? false {
Cocoa.NSWorkspace.shared.setIcon(image, forFile: subPathURL.path, options: NSWorkspace.IconCreationOptions(rawValue: 0))
}
}
} catch {
print("Something went wrong...")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment