ChatbotGPT created addon to open a window and display/edit contents like a notepad
#1
This code was pretty much all generated by chatbotgpt, I signed up to check it out and thought it would be funny if it could create something that would actually work.
Turns out, it can lol
But I can barely code, so I'm struggling a little bit.

Anyways, I'm having a hard time with the logic for this because I don't think I'm doing it right:

The idea is to have the addon read the contents of a file and then display the contents inside of a editable control, so the user can change code.
But I don't really understand how do that per se.

Right now I can display the contents of a file in a textbox, but users cant edit those.
But you can edit an edit control, but I don't know how to "feed" the contents of the file into that or use the contents of the file as the default label.

Is it possible to open/create a window that operates like a traditional Notepad?
 
Quote:import os
import xbmc
import xbmcvfs
import xbmcgui
import xbmcaddon
from resources.lib import utils as utils

ADDON = xbmcaddon.Addon()
ADDON_ID = ADDON.getAddonInfo('name')
DEFAULT_FILE_LOCATION = ADDON.getSetting("default_file_location")
ADDON_DATA_FOLDER = xbmcvfs.translatePath("special://profile/addon_data/%s" % ADDON_ID)


class NotepadEditor(xbmcgui.WindowXMLDialog):
    def __init__(self, *args, **kwargs):
        self.file_path = kwargs.get("file_path")
        self.text = ""

    def onInit(self):
        if self.file_path:
            try:
                with open(self.file_path, "r") as file:
                    self.text = file.read()
                    self.getControl(100).setText(self.text)
            except Exception as e:
                utils.log(f"Unable to open file - {str(e)}", xbmc.LOGERROR)
                # xbmc.log(f"{ADDON_ID}: Unable to open file - {str(e)}", xbmc.LOGERROR)
                utils.show_notification(f"Unable to open file - {str(e)}")

    def onClick(self, control_id):
        if control_id == 200:
            try:
                with open(self.file_path, "w") as file:
                    self.text = self.getControl(100).getText()
                    file.write(self.text)
                self.close()
            except Exception as e:
                xbmc.log(f"{ADDON_ID}: Unable to save file - {str(e)}", xbmc.LOGERROR)
        elif control_id == 201: # Create close button
            self.close()
        elif control_id == 202: # Create edit button
            self.edit_file()   
        elif control_id == 203: # Create file button
            self.create_file()
        elif control_id == 204: # Delete file button
            self.delete_file()


    def edit_file(self):
        try:
            with open(self.file_path, "r") as file:
                self.text = file.read()
                self.getControl(202).setText(self.text)
                xbmc.log(f"{ADDON_ID}: Edit complete - {str(e)}", xbmc.LOGERROR)
        except Exception as e:
            xbmc.log(f"{ADDON_ID}: Unable to open file - {str(e)}", xbmc.LOGERROR)


    def create_file(self):
        file_name = xbmcgui.Dialog().input("Enter the file name", type=xbmcgui.INPUT_ALPHANUM)
        if file_name:
            file_path = os.path.join(ADDON_DATA_FOLDER, file_name)
            with open(file_path, "w") as f:
                pass
            xbmc.executebuiltin("Container.Refresh")

    def delete_file(self):
        if xbmcgui.Dialog().yesno("Confirm", "Are you sure you want to delete the file?"):
            os.remove(self.file_path)
            xbmc.executebuiltin("Container.Refresh")

def open_notepad_editor(file_path=None):
    notepad_editor = NotepadEditor("notepad-editor.xml", ADDON.getAddonInfo("path"), file_path=file_path)
    notepad_editor.doModal()
    del notepad_editor


def main():
    default_file_path = ADDON.getSetting("default_file_location")
    if default_file_path:
        default_file_path = os.path.join(ADDON_DATA_FOLDER, default_file_path)
        open_notepad_editor(default_file_path)
    else:
        xbmcgui.Dialog().ok("Error", "No default file selected in settings.")
        utils.open_settings()

if __name__ == "__main__":
    main()
 
Quote:<?xml version="1.0" encoding="UTF-8"?>
<window>
  <controls>
    <control type="image">
      <description>background image</description>
      <left>-10</left>
      <top>-10</top>
      <width>1920</width>
      <height>1080</height>
      <texture border="20">script-module-youtube-dl-shadow.png</texture>
    </control>
    <control type="image">
      <description>background image</description>
      <left>0</left>
      <top>0</top>
      <width>1920</width>
      <height>1080</height>
      <texture border="20">script-module-youtube-dl-dialog_back.png</texture>
    </control>
    <control type="scrollbar">
        <width>8</width>
        <texturesliderbackground colordiffuse="dialog_bg_12">common/white.png</texturesliderbackground>
        <texturesliderbar colordiffuse="dialog_bg_30">common/white.png</texturesliderbar>
        <orientation>vertical</orientation>
    </control>
    <control type="textbox" id="100">
      <posx>0</posx>
      <posy>0</posy>
      <width>1920</width>
      <height>1080</height>
      <textcolor>white</textcolor>
    </control>
    <control type="edit" id="205">
      <posx>0</posx>
      <posy>0</posy>
      <width>1920</width>
      <height>1080</height>
      <textcolor>white</textcolor>
    </control>
    <control type="button" id="200">
      <description>Save</description>
      <posx>0</posx>
      <posy>1000</posy>
      <width>640</width>
      <height>80</height>
      <textcolor>FF999999</textcolor>
      <focusedcolor>FFFFFFFF</focusedcolor>
      <disabledcolor>40999999</disabledcolor>
      <texturefocus border="8">script-module-youtube-dl-button_focus.png</texturefocus>
      <texturenofocus border="8">script-module-youtube-dl-button.png</texturenofocus>
      <label>Save</label>
    </control>
    <control type="button" id="201">
      <description>Cancel</description>
      <posx>640</posx>
      <posy>1000</posy>
      <width>640</width>
      <height>80</height>
      <textcolor>FF999999</textcolor>
      <focusedcolor>FFFFFFFF</focusedcolor>
      <disabledcolor>40999999</disabledcolor>
      <texturefocus border="8">script-module-youtube-dl-button_focus.png</texturefocus>
      <texturenofocus border="8">script-module-youtube-dl-button.png</texturenofocus>
      <label>Cancel</label>
    </control>
    <control type="button" id="204">
      <description>Delete</description>
      <posx>1280</posx>
      <posy>1000</posy>
      <width>640</width>
      <height>80</height>
      <textcolor>FF999999</textcolor>
      <focusedcolor>FFFFFFFF</focusedcolor>
      <disabledcolor>40999999</disabledcolor>
      <texturefocus border="8">script-module-youtube-dl-button_focus.png</texturefocus>
      <texturenofocus border="8">script-module-youtube-dl-button.png</texturenofocus>
      <label>Delete</label>
    </control>
  </controls>
</window>
(^^ I stole some assets from youtube-dl module because it was the easiest one at the time. If I can get this working correctly, I will replace them with unique assets)

This is what the addon looks like when it opens a test file:


Image
Reply

Logout Mark Read Team Forum Stats Members Help
ChatbotGPT created addon to open a window and display/edit contents like a notepad0