DEV Community

Franz Wong
Franz Wong

Posted on

Set MacOS finder folder icon with Python

Problem

I want to use Python script to replace MacOS finder folder icon with an image.

Steps

1. Install packages

pip3 install pyobjc-core pyobjc-framework-Cocoa
Enter fullscreen mode Exit fullscreen mode

2. Create a python script file set-icon.py with the following code.

import sys, Cocoa

folder_path = sys.argv[1]
print(f"Folder path: {folder_path}")

image_path = sys.argv[2]
print(f"Image path: {image_path}")

result = Cocoa.NSWorkspace.sharedWorkspace().setIcon_forFile_options_(Cocoa.NSImage.alloc().initWithContentsOfFile_(image_path), folder_path, 0)
if result:
    print("Succeed")
else:
    print("Failed")
Enter fullscreen mode Exit fullscreen mode

3. Run the script to change the icon.

python3 set-icon.py <folder path> <image path>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)