Skip to content

[Possible Bug / Error] PythonScript 3.0.24.0 - "PluginsManager:runPluginCommand Exception" dialog with "Access Violation" with one of my scripts. #405

@futuremotiondev

Description

@futuremotiondev

Version Info and Config

OS: Windows 11 Pro x64 Version 25H2 (0S Build 26200.7705)
Npp: Notepad++ v8.9.1 (64-bit)
PythonScript: v3.0.24 (https://github.com/bruderstein/PythonScript/releases/tag/v3.0.24)
Image Image

Overview of Issue

I created two new scripts today (via Claude Opus 4.5) to transform a specific JSON format to another. In this case it's to replace RGB values to equivalent hex colors, and back. Claude has never failed me until today... but unfortunately my script is causing Npp to spit out an PluginsManager::runPluginCommand Exception - Access Violation popup:

Image

How can I debug this?

This is really frustrating to diagnose because even though I have the "Show Console" enabled for PythonScript globally, it doesn't seem to show the error in the console at all, and I have no way of knowing what line or specific section of code is throwing this error.

All of my old scripts work completely fine, so I don't think this is an installation issue. Can someone possibly spot what's wrong with my script or at least provide me with a better way to debug this? I have no clue what to do in order to diagnose this bug.

Image

My Scripts

Here are both scripts:

This is my input (I've tried running on selection as well as the whole file):

{
  "Name": "Some Name",
  "BackgroundColor": "22, 22, 24",
  "LightBackgroundColor": "19, 19, 21",
  "DarkBackgroundColor": "14, 14, 15",
  "TextColor": "227, 227, 239",
  "BorderColor": "39, 39, 42",
  "CheckerColor": "213, 188, 170",
  "CheckerColor2": "212, 146, 146",
  "CheckerSize": 17,
  "LinkColor": "95, 136, 255",
  "MenuHighlightColor": "44, 44, 52",
  "MenuHighlightBorderColor": "28, 28, 34",
  "MenuBorderColor": "28, 28, 34",
  "MenuCheckBackgroundColor": "186, 219, 130",
  "MenuFont": "Roboto, 14px",
  "ContextMenuFont": "Roboto, 14px",
  "ContextMenuOpacity": 100,
  "SeparatorLightColor": "255, 75, 238",
  "SeparatorDarkColor": "25, 25, 28"
}

FM TransformColorJsonRgbToHex.py:

# -*- coding: utf-8 -*-
"""
RGB to Hex Color Converter for Notepad++ PythonScript
Converts RGB color values like "22, 22, 24" to hex format "#161618"
Handles selection or entire document with full undo support.
"""

import re

def rgb_to_hex(match):
    """
    Convert an RGB string match to hex format.
    
    Args:
        match: Regex match object containing RGB values
        
    Returns:
        Hex color string in format "#RRGGBB"
    """
    try:
        r = int(match.group(1).strip())
        g = int(match.group(2).strip())
        b = int(match.group(3).strip())
        
        # Clamp values to valid RGB range
        r = max(0, min(255, r))
        g = max(0, min(255, g))
        b = max(0, min(255, b))
        
        return '"#{:02X}{:02X}{:02X}"'.format(r, g, b)
    except (ValueError, AttributeError):
        # Return original if conversion fails
        return match.group(0)


def convert_rgb_to_hex(text):
    """
    Convert all RGB color values in text to hex format.
    
    Matches patterns like:
        "22, 22, 24"
        "255, 128, 0"
        " 22 , 22 , 24 " (with extra whitespace)
    
    Args:
        text: Input text containing RGB color values
        
    Returns:
        Text with RGB values converted to hex
    """
    # Pattern matches quoted RGB values: "R, G, B"
    # Captures three groups of digits separated by commas
    # Allows flexible whitespace
    pattern = r'"(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})"'
    
    return re.sub(pattern, rgb_to_hex, text)


def main():
    """Main entry point for the script."""
    
    # Check if there's a selection
    selection_start = editor.getSelectionStart()
    selection_end = editor.getSelectionEnd()
    has_selection = selection_start != selection_end
    
    # Begin undo action for single-step undo
    editor.beginUndoAction()
    
    try:
        if has_selection:
            # Process only selected text
            selected_text = editor.getSelText()
            converted_text = convert_rgb_to_hex(selected_text)
            
            if selected_text != converted_text:
                editor.replaceSel(converted_text)
                notepad.messageBox(
                    "RGB to Hex conversion complete (selection).",
                    "Conversion Complete",
                    0
                )
            else:
                notepad.messageBox(
                    "No RGB color values found in selection.",
                    "No Changes",
                    0
                )
        else:
            # Process entire document
            full_text = editor.getText()
            converted_text = convert_rgb_to_hex(full_text)
            
            if full_text != converted_text:
                # Preserve cursor position
                current_pos = editor.getCurrentPos()
                editor.setText(converted_text)
                
                # Restore cursor (adjust if position is now out of bounds)
                new_length = editor.getTextLength()
                editor.gotoPos(min(current_pos, new_length))
                
                notepad.messageBox(
                    "RGB to Hex conversion complete (entire document).",
                    "Conversion Complete",
                    0
                )
            else:
                notepad.messageBox(
                    "No RGB color values found in document.",
                    "No Changes",
                    0
                )
    finally:
        # Always end undo action, even if an error occurs
        editor.endUndoAction()


# Execute the script
main()

Conclusion

Can someone possibly check my script to see what's causing this violation?

I resorted to posting an issue because I have no idea how to debug this.

Thanks much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions