Guest User

Set Folder Icon rightclick

a guest
May 20th, 2017
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import os
  4.  
  5. # --- set the list of valid extensions below (lowercase)
  6. # --- use quotes, *don't* include the dot!
  7. ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"]
  8. # ---
  9.  
  10. # retrieve the path of the targeted folder
  11. current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI").replace("file://", "").replace("%20", " ")
  12. dr = os.path.realpath(current)
  13.  
  14. for root, dirs, files in os.walk(dr):
  15.     for directory in dirs:
  16.         folder = os.path.join(root, directory)
  17.         try:
  18.             first = min(p for p in os.listdir(folder)
  19.                         if p.split(".")[-1].lower() in ext)
  20.         except ValueError:
  21.             pass
  22.         else:
  23.               subprocess.Popen([
  24.                   "gvfs-set-attribute", "-t", "string",
  25.                   os.path.abspath(folder), "metadata::custom-icon",
  26.                   "file://"+os.path.abspath(os.path.join(folder, first))
  27.                   ])
RAW Paste Data