DEV Community

Franz Wong
Franz Wong

Posted on

3 2

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)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay