import json import os import glob def update_jr_button_paths(file_path): # Read the JSON file with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) # Counter for changes changes_made = 0 total_jr_found = 0 # Function to recursively search and update paths def update_paths(obj): nonlocal changes_made, total_jr_found if isinstance(obj, dict): # Check if this is a JR component by looking at meta.name if 'meta' in obj and 'name' in obj['meta']: name = obj['meta']['name'] # Check if name contains JR if 'JR' in name: total_jr_found += 1 print(f"Found JR component #{total_jr_found}: {name}") # Check for path in props if 'props' in obj and 'path' in obj['props']: current_path = obj['props']['path'] print(f" Current path: '{current_path}'") if current_path == 'Symbol-Views/Equipment-Views/Estop': obj['props']['path'] = 'Symbol-Views/Equipment-Views/JR_Button' changes_made += 1 print(f" ✓ Changed path for: {name}") else: print(f" ✗ Not changed - different path") else: print(f" ✗ Not changed - no path found") for value in obj.values(): update_paths(value) elif isinstance(obj, list): for item in obj: update_paths(item) # Update paths in the data update_paths(data) # Write the updated data back to the file with open(file_path, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2) print(f"\nSummary:") print(f"Total JR components found: {total_jr_found}") print(f"Components updated: {changes_made}") return changes_made def find_view_files(): # Search for view.json files only in the Detailed-Views directory base_path = r"C:\Program Files\Inductive Automation\Ignition\data\projects\MTN6_SCADA\com.inductiveautomation.perspective\views\Detailed-Views" view_files = [] for root, dirs, files in os.walk(base_path): for file in files: if file == 'view.json': view_files.append(os.path.join(root, file)) return view_files def main(): # Find all view.json files view_files = find_view_files() if not view_files: print("No view.json files found!") return # Display available files print("\nAvailable view.json files:") for i, file_path in enumerate(view_files, 1): # Show only the folder name and view.json for cleaner display folder_name = os.path.basename(os.path.dirname(file_path)) print(f"{i}. {folder_name}/view.json") # Get user selection while True: try: choice = input("\nEnter the number of the file you want to update (or 'q' to quit): ") if choice.lower() == 'q': break choice = int(choice) if 1 <= choice <= len(view_files): selected_file = view_files[choice - 1] try: print(f"\nProcessing file: {selected_file}") changes = update_jr_button_paths(selected_file) print(f"\nUpdated {changes} JR button paths in {os.path.basename(os.path.dirname(selected_file))}/view.json") except Exception as e: print(f"\nError processing {selected_file}: {str(e)}") else: print("Invalid selection! Please try again.") except ValueError: print("Please enter a valid number or 'q' to quit.") # Ask if user wants to process another file if input("\nDo you want to process another file? (y/n): ").lower() != 'y': break if __name__ == "__main__": main()