Skip to content

Instantly share code, notes, and snippets.

@mattcox
Last active November 28, 2020 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcox/8a5d68a0e6b768be3e393aeadbed0af3 to your computer and use it in GitHub Desktop.
Save mattcox/8a5d68a0e6b768be3e393aeadbed0af3 to your computer and use it in GitHub Desktop.
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