|
| 1 | +import os |
| 2 | +import folder_paths # ComfyUI utility to get paths |
| 3 | + |
| 4 | +class EmAySee_SaveTextToFile: |
| 5 | + """ |
| 6 | + Saves a string input to a text file with a specified filename and subfolder. |
| 7 | + """ |
| 8 | + @classmethod |
| 9 | + def INPUT_TYPES(s): |
| 10 | + return { |
| 11 | + "required": { |
| 12 | + "text_to_save": ("STRING", {"multiline": True}), # The text content to save |
| 13 | + "filename": ("STRING", {"default": "output_text"}), # The desired filename (without extension) |
| 14 | + "subfolder": ("STRING", {"default": ""}), # Optional subfolder within the output directory |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + # This node performs a side effect (saving a file) and doesn't need to output anything |
| 19 | + # If you wanted to output the saved file path, you could add RETURN_TYPES = ("STRING",) |
| 20 | + # RETURN_TYPES = () # No direct output |
| 21 | + |
| 22 | + CATEGORY = "EmAySee_Utils" # Category in the ComfyUI menu |
| 23 | + TITLE = "EmAySee Save Text to File" # Title displayed on the node |
| 24 | + |
| 25 | + FUNCTION = "EmAySee_save_text" # The method that will be executed |
| 26 | + |
| 27 | + def EmAySee_save_text(self, text_to_save, filename, subfolder): |
| 28 | + """ |
| 29 | + Saves the text content to a file in the specified subfolder. |
| 30 | + """ |
| 31 | + # Get the base output directory for ComfyUI |
| 32 | + output_dir = folder_paths.get_output_paths(None)[0] # Get the first output path |
| 33 | + |
| 34 | + # Construct the full directory path |
| 35 | + if subfolder: |
| 36 | + save_dir = os.path.join(output_dir, subfolder) |
| 37 | + else: |
| 38 | + save_dir = output_dir |
| 39 | + |
| 40 | + # Ensure the directory exists, create if necessary |
| 41 | + os.makedirs(save_dir, exist_ok=True) |
| 42 | + |
| 43 | + # Construct the full file path with .txt extension |
| 44 | + # Sanitize filename to remove potentially problematic characters |
| 45 | + safe_filename = "".join(c for c in filename if c.isalnum() or c in (' ', '_', '-')).rstrip() |
| 46 | + if not safe_filename: |
| 47 | + safe_filename = "output_text" # Fallback if filename becomes empty after sanitization |
| 48 | + |
| 49 | + file_path = os.path.join(save_dir, f"{safe_filename}.txt") |
| 50 | + |
| 51 | + try: |
| 52 | + # Save the text content to the file |
| 53 | + with open(file_path, "w", encoding="utf-8") as f: |
| 54 | + f.write(text_to_save) |
| 55 | + |
| 56 | + print(f"Successfully saved text to: {file_path}") |
| 57 | + |
| 58 | + except IOError as e: |
| 59 | + print(f"Error saving text to file {file_path}: {e}") |
| 60 | + except Exception as e: |
| 61 | + print(f"An unexpected error occurred while saving text: {e}") |
| 62 | + |
| 63 | + # This node doesn't return any specific output values |
| 64 | + return () |
| 65 | + |
| 66 | +# Mapping of node class name to the class |
| 67 | +NODE_CLASS_MAPPINGS = { |
| 68 | + "EmAySee_SaveTextToFile": EmAySee_SaveTextToFile |
| 69 | +} |
| 70 | + |
| 71 | +# Mapping of node class name to the display name in the UI |
| 72 | +NODE_DISPLAY_NAME_MAPPINGS = { |
| 73 | + "EmAySee_SaveTextToFile": "EmAySee Save Text to File" |
| 74 | +} |
0 commit comments