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
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")
3. Run the script to change the icon.
python3 set-icon.py <folder path> <image path>
Top comments (0)