Skip to content

Instantly share code, notes, and snippets.

Last active November 28, 2020 01:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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