Last active
          January 2, 2024 15:04 
        
      - 
      
- 
        Save nicolas-oliveira/13b8d73b9fbcd550d33144a23ec68648 to your computer and use it in GitHub Desktop. 
    Script that fix the icons in ubuntu / gnome like destops to follow the theme installed in distro.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import os | |
| import time | |
| # Script that iterates a specific folder | |
| # (usually the folder containing the '.desktop' files) | |
| # Changes the Jetbrains file to hidden in Ubuntu or Gnome like desktop | |
| # and Creates a copy file with the default linux theme icon | |
| HOME = os.environ["HOME"] | |
| DIR_PATH = f"{HOME}/.local/share/applications/" | |
| EXTENSION_SUFIX = ".desktop" | |
| FILE_PATTERNS = ["jetbrains-", "PyCharm Community-"] | |
| PROCESSED_PREFIX_PATTERN = "fix-" | |
| EXCEPTION_ICONS_NAME = {"android-studio": "studio", "jetbrains-toolbox": "toolbox"} | |
| class Colors: | |
| RED = "\033[31m" | |
| GREEN = "\033[32m" | |
| YELLOW = "\033[33m" | |
| END = "\033[39m" | |
| def main(): | |
| warning( | |
| "This is a script that fixes the jetbrains desktop icons on linux, run it with attention\n" | |
| ) | |
| time.sleep(0.5) | |
| path_list_all = get_path_list(DIR_PATH) | |
| path_list_by_extension = filter_path_list_by_extension( | |
| path_list_all, EXTENSION_SUFIX | |
| ) | |
| path_list_to_process = [] | |
| for path_element in path_list_by_extension: | |
| if not os.path.basename(path_element).startswith(PROCESSED_PREFIX_PATTERN): | |
| if any( | |
| os.path.basename(path_element).startswith(prefix) | |
| for prefix in FILE_PATTERNS | |
| ): | |
| path_list_to_process.append(path_element) | |
| warning("Processing the following files:") | |
| for path_element in path_list_to_process: | |
| print(path_element) | |
| time.sleep(0.5) | |
| print("\n") | |
| for path_element in path_list_to_process: | |
| process(path_element) | |
| def search_dict_by_value(__value: any, __dict: dict[any:any]): | |
| return [key for key, value in __dict.items() if value == __value][0] | |
| def error(__input: str): | |
| print(Colors.RED + __input + Colors.END) | |
| def success(__input: str): | |
| print(Colors.GREEN + __input + Colors.END) | |
| def warning(__input: str): | |
| print(Colors.YELLOW + __input + Colors.END) | |
| def exist_line_in_file(file_path, target_line): | |
| try: | |
| with open(file_path, 'r') as file: | |
| # Check if the target line exists in any line of the file | |
| return any(line.strip() == target_line for line in file) | |
| except FileNotFoundError: | |
| # Handle file not found error | |
| return False | |
| def get_path_list(__dir: str) -> list[any]: | |
| """Gets a list of file paths in the specified directory.""" | |
| path_list = [] | |
| for filename in os.listdir(__dir): | |
| path_list.append(os.path.join(__dir, filename)) | |
| return path_list | |
| def filter_path_list_by_extension(__pathlist: list[str], __endswith: str) -> list[any]: | |
| """Filters a list of file paths based on the specified file extension.""" | |
| new_path_list = [] | |
| for path in __pathlist: | |
| if path.endswith(__endswith): | |
| new_path_list.append(path) | |
| return new_path_list | |
| def process(__path: str): | |
| current_file_content = [] | |
| new_content = [] | |
| file_basename = os.path.basename(__path) | |
| try: | |
| # Read the path passed as arg in function, getting information from the file | |
| with open(__path, "r") as current_file: | |
| for line in current_file.readlines(): | |
| if line.startswith("Icon="): | |
| icon_name = os.path.basename( | |
| line.strip().replace("Icon=", "").replace(".svg", "") | |
| ) | |
| # Change the app name if the name is different compared with the line | |
| for value in EXCEPTION_ICONS_NAME.values(): | |
| if value == icon_name: | |
| icon_name = search_dict_by_value(value, EXCEPTION_ICONS_NAME) | |
| # Generate new path for the new file | |
| new_path = ( | |
| os.path.dirname(__path) + "/" | |
| + PROCESSED_PREFIX_PATTERN | |
| + file_basename | |
| + EXTENSION_SUFIX | |
| ) | |
| print(new_path) | |
| new_line = f"Icon={icon_name}\n" | |
| else: | |
| if line.startswith("NoDisplay=true"): | |
| new_line = f"NoDisplay=false\n" | |
| else: | |
| new_line = line | |
| new_content.append(new_line) | |
| # Edit the current file to not display at launcher | |
| with open(__path, "r") as current_file: | |
| for line in current_file.readlines(): | |
| if line.startswith("NoDisplay="): | |
| new_line = f"NoDisplay=true\n" | |
| else: | |
| new_line = line | |
| current_file_content.append(new_line) | |
| # Fixes files that doesn't contain NoDisplay at the file | |
| if not exist_line_in_file(__path, "NoDisplay="): | |
| new_content.append("NoDisplay=false\n") | |
| current_file_content.append("NoDisplay=true\n") | |
| # Edit the current file to hide it from launcher | |
| with open(__path, "w") as current_file: | |
| current_file.writelines(current_file_content) | |
| # Create a new file with the props created above | |
| with open(new_path, "w") as new_file: | |
| new_file.writelines(new_content) | |
| success(f"Successfully edited {file_basename}") | |
| except IOError: | |
| print(f"Could not process the file {file_basename}") | |
| main() | |
| print("Processing completed!") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment