• Login
Community
  • Login

Finding available shortcut keys (originally "Where's the 'New Window' command?")

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
53 Posts 7 Posters 5.8k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E
    Ekopalypse @TBugReporter
    last edited by Jan 6, 2023, 11:08 AM

    @TBugReporter

    Found the time to try it out during my lunch break.

    TCM_GETITEMW = (TCM_FIRST + 60)  # get the current tab message
    TCIF_TEXT = 1  # flag to specify that we are only interested in pszText
    
    # define the storage that will contain the requested information.
    class TCITEM(ctypes.Structure):
        _fields_ = [('mask',        wintypes.UINT),
                    ('dwState',     wintypes.DWORD),
                    ('dwStateMask', wintypes.DWORD),
                    ('pszText',     wintypes.LPWSTR),
                    ('cchTextMax',  wintypes.INT),
                    ('iImage',      wintypes.INT),
                    ('lParam',      wintypes.LPARAM)]
    
    
    pszText = ctypes.create_unicode_buffer(260)  # create a buffer to hold the tab text
    tcitem = TCITEM()  # create an instance
    tcitem.mask = TCIF_TEXT  # specify that we want to read the pszText field
    tcitem.pszText = addressof(pszText)  # points to the tab text buffer 
    tcitem.cchTextMax = len(pszText)  # length of buffer
    pITEM = byref(tcitem)  # pointer to the concrete struct TCITEM 
    
    # within the for loop call
    found = user32.SendMessageW(sys_tab_hwnd, TCM_GETITEMW, tab, pITEM)
    if found:
    	print(tcitem.pszText)
    

    Seems to work, let me know if anything is unclear.

    T 1 Reply Last reply Jan 7, 2023, 7:18 AM Reply Quote 2
    • T
      TBugReporter @guy038
      last edited by Jan 7, 2023, 2:57 AM

      @guy038:

      which title would you like to see ?

      How about

      Finding available shortcut keys (originally "Where's the 'New Window' command?")
      

      As a moderator, I can modify it easily !

      TYVM!

      @Ekopalypse:

      Seems to work, let me know if anything is unclear.

      Yes, that looks like the info I needed; now I just have to add it to the script.

      TYVM to you too!

      1 Reply Last reply Reply Quote 1
      • T
        TBugReporter @Ekopalypse
        last edited by Jan 7, 2023, 7:18 AM

        @Ekopalypse

        My latest sticking point:

        NameError: global name 'byref' is not defined
        
        R 1 Reply Last reply Jan 7, 2023, 9:01 AM Reply Quote 0
        • R
          rdipardo @TBugReporter
          last edited by Jan 7, 2023, 9:01 AM

          NameError: global name 'byref' is not defined
          

          Try ctypes.byref instead.

          1 Reply Last reply Reply Quote 1
          • T
            TBugReporter
            last edited by Jan 9, 2023, 6:27 AM

            Here’s a question from the “newless cluebie to Python” department:

            I’m currently struggling with errors like

            NameError: global name 'my_new_variable' is not defined
            

            I read that there’s a “global” statement that can help, but it doesn’t seem to work for me. Exactly where should this go? Or, is there another way I can rearrange the code that will avoid this?

            E 1 Reply Last reply Jan 9, 2023, 9:32 AM Reply Quote 0
            • E
              Ekopalypse @TBugReporter
              last edited by Jan 9, 2023, 9:32 AM

              @TBugReporter

              In the function in which it is used, suppose there is a code like this

              x = 42
              y = {'x': 42}
              
              def main():
                  global x
                  x += 1
                  y['x'] += 1
                  print(x)
                  print(y)
                  
              main()
              

              x needs to be defined as global, while it is not necessary for y, because it is a dict.

              1 Reply Last reply Reply Quote 2
              • T
                TBugReporter
                last edited by Jan 10, 2023, 11:48 PM

                The pest is back! 😁 (I thank you in advance for your patience with me.)

                • Can I use PythonScript to display a Windows MessageBox? Can I create my own custom dialogs in Windows style?

                • How can I prematurely end my script (if something weird happens)? I found sys.exit(), but this apparently terminates all of Notepad++ along with the script.

                P T 2 Replies Last reply Jan 11, 2023, 12:31 AM Reply Quote 1
                • P
                  PeterJones @TBugReporter
                  last edited by PeterJones Jan 11, 2023, 2:39 PM Jan 11, 2023, 12:31 AM

                  @TBugReporter said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                  • Can I use PythonScript to display a Windows MessageBox?

                  Yes. 😉1

                  Can I create my own custom dialogs in Windows style?

                  Not easily. 😉2

                  • How can I prematurely end my script (if something weird happens)? I found sys.exit(), but this apparently terminates all of Notepad++ along with the script.

                  The best way I’ve found is to have a bunch of “if” processing so that it keeps on dumping out without running any of your code

                  ----

                  WinkNotes: 😉3

                  😉1: notepad.messageBox()

                  😉2: Alan tends to get around this by doing a bunch of free-form editor.input() notepad.prompt() calls, and then processing the text that the user types in to make the decisions. In theory, since PythonScript comes with the libraries necessary to access any of the win32 API functions: see plenty of examples in the forum with accessing SendMessage; searching the forum on the SendMessage from win32 API, and narrowing it down to @Ekopalypse’s posts, will get you some good examples pretty quickly – you would then have to extrapolate from that on how to access the various win32 API functions you want. But that said, I don’t know how practical it is to build a whole window using raw API calls – normally when programming win32 API, you would use resource files or equivalents, but I have no clue if you can define a resource file and easily give access to that resource file to the Python interpreter in PythonScript. (If you figure out a way, let me know, because I want to use that to reduce my library requirements in PerlScript some day.)

                  😉3: wink notes are similar to footnotes, but made more annoying because I hid my actual answer inside the footnotes rather than in the main flow of the conversation

                  T P 2 Replies Last reply Jan 11, 2023, 7:08 AM Reply Quote 1
                  • T
                    TBugReporter @PeterJones
                    last edited by TBugReporter Jan 11, 2023, 7:21 AM Jan 11, 2023, 7:08 AM

                    @PeterJones said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                    The best way I’ve found is to have a bunch of “if” processing

                    If I have to resort to something like that, I’d use

                    while True:
                        pass
                    

                    instead. (Still an evil hack, but it seems slightly more elegant.)

                    E 1 Reply Last reply Jan 11, 2023, 9:16 AM Reply Quote 0
                    • E
                      Ekopalypse @TBugReporter
                      last edited by Ekopalypse Jan 11, 2023, 9:18 AM Jan 11, 2023, 9:16 AM

                      @TBugReporter

                      With ctypes you have access to the Windows Api and can therefore do everything it offers. E.g. create a TaskDialog .

                      39a6b14b-f03f-41a6-aca0-71952d2acaeb-image.png

                      You can also build your own dialogs with the included Tkinter module, see for example the Formatter.py script from the demo directory.

                      As for sys.exit, I found out that calling a “main” function and returning from that function serve the same purpose.

                      def main():
                          if x != 0:
                              return
                      
                      main()
                      
                      A P 2 Replies Last reply Jan 11, 2023, 2:41 PM Reply Quote 5
                      • P
                        PeterJones @PeterJones
                        last edited by PeterJones Jan 11, 2023, 2:40 PM Jan 11, 2023, 2:38 PM

                        @PeterJones said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                        free-form editor.input() calls

                        It was pointed out to me that it should have said notepad.prompt() . sorry for the confusion. I have updated the original post to not confuse future readers.

                        1 Reply Last reply Reply Quote 0
                        • A
                          Alan Kilborn @Ekopalypse
                          last edited by Alan Kilborn Jan 11, 2023, 2:56 PM Jan 11, 2023, 2:41 PM

                          @Ekopalypse said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                          You can also build your own dialogs with the included Tkinter module

                          For me, this approach is just “too much” as in “complication” but to each his own. Maybe I’ve just never liked Tkinter in general.

                          I don’t know, I’ve felt that the “mini UIs” I’ve been able to create with the notepad.prompt() window are lightweight and workable… A classic example would be in this THREAD; see the script in the Dec 16, 2017, 11:03 PM posting.

                          E 1 Reply Last reply Jan 11, 2023, 4:39 PM Reply Quote 1
                          • P
                            PeterJones @Ekopalypse
                            last edited by Jan 11, 2023, 2:45 PM

                            @Ekopalypse ,

                            Was your screenshot created with the Tkinter library, or with raw calls to the TaskDialog? If with raw calls, could you share the code (as a gist if it’s too long to fit nicely in the forum)?

                            E 1 Reply Last reply Jan 11, 2023, 6:42 PM Reply Quote 0
                            • E
                              Ekopalypse @Alan Kilborn
                              last edited by Jan 11, 2023, 4:39 PM

                              @Alan-Kilborn

                              I don’t use Tkinter either, but as you said it is an alternative.
                              90% of my scripts I use have a configuration section, so no dialog at all. The rest use dialogs like the task dialog.
                              Just my preference - could be done differently.

                              @PeterJones

                              The screenshot with the TaskDialog was created with ctypes and the corresponding Win32 API calls.
                              Give me a few minutes to create a standalone, callable version. I’ll post the link to the gist once I’m done.

                              1 Reply Last reply Reply Quote 1
                              • E
                                Ekopalypse @PeterJones
                                last edited by Jan 11, 2023, 6:42 PM

                                @PeterJones

                                tested with PS 2 and 3.
                                Note the comment on FindResourceExA
                                If something is unclear, let me know.

                                A 1 Reply Last reply Jan 11, 2023, 7:07 PM Reply Quote 2
                                • A
                                  Alan Kilborn @Ekopalypse
                                  last edited by Jan 11, 2023, 7:07 PM

                                  @Ekopalypse said:

                                  (provided code link)
                                  tested with PS 2 and 3.
                                  Note the comment on FindResourceExA
                                  If something is unclear, let me know.

                                  I’m sure it works great.
                                  IMO though, it seems “heavy”.
                                  But…it depends on how much effort one wants to put in, I guess.

                                  I mean, that is a lot of lines of code, just for a UI for a script.
                                  When considering that the non-UI part of the code is only going to add to that already large line count, well…

                                  Also, there’s no “edit box” type of input demo’d, which seems kind of important when one considers what we’ve been able to do in the past to gather user input from the workhorse notepad.prompt() function.

                                  This is by no means an insult to the code provided, just some comments…

                                  E 1 Reply Last reply Jan 11, 2023, 7:17 PM Reply Quote 1
                                  • E
                                    Ekopalypse @Alan Kilborn
                                    last edited by Jan 11, 2023, 7:17 PM

                                    @Alan-Kilborn

                                    Yes, it is larger than a “prompt” solution, and the TaskDialog is strictly defined by MS. You can only have what it provides.

                                    If you want a custom dialog, you have to wrap DialogBoxes
                                    but that is just as heavy, if not heavier, than the TaskDialog.

                                    The prompt solution may be fine for simple scripts, but as soon as you need to parameterize a script with different settings depending on different models, it is much easier to use a guided dialog, at least in my opinion.

                                    A 1 Reply Last reply Jan 11, 2023, 7:21 PM Reply Quote 2
                                    • A
                                      Alan Kilborn @Ekopalypse
                                      last edited by Alan Kilborn Jan 11, 2023, 9:12 PM Jan 11, 2023, 7:21 PM

                                      @Ekopalypse said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                                      The prompt solution may be fine for simple scripts

                                      “Simple” scripts are really all we should be doing, with a tool such as the PythonScript plugin.

                                      Of course, and probably more of late, some of mine haven’t exactly been “simple”, so I’m a violator of my above edict. :-)

                                      And maybe sadly, I’ve got a doozy in the in-progress pipeline. Maybe my excuse for it is, let’s push the limits of a PS.

                                      But maybe my overall point is, if I can keep the UI relatively simple-minded, I can spend more time on the “meat” of whatever the script is trying to accomplish.

                                      Others can go off and make the UI part as polished (and as complicated and lengthy) as they want to, in their own scripts.

                                      E 1 Reply Last reply Jan 11, 2023, 7:22 PM Reply Quote 2
                                      • E
                                        Ekopalypse @Alan Kilborn
                                        last edited by Jan 11, 2023, 7:22 PM

                                        @Alan-Kilborn said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                                        Of course, and probably more of late, some of mine haven’t exactly been “simple”

                                        :-D for sure !! :-D

                                        1 Reply Last reply Reply Quote 2
                                        • T
                                          TBugReporter @TBugReporter
                                          last edited by Jan 12, 2023, 3:59 AM

                                          @Ekopalypse said in Finding available shortcut keys (originally "Where's the 'New Window' command?"):

                                          As for sys.exit, I found out that calling a “main” function and returning from that function serve the same purpose.

                                          Due to the scope errors I’ve been getting, I decided to forego that, and just put everything as immediate commands.

                                          @Alan-Kilborn said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                                          I’m sure it works great.
                                          IMO though, it seems “heavy”.

                                          At 445 lines for do-nothing sample code, “heavy” seems an understatement. 😁

                                          @TBugReporter said in Finding available shortcut keys (originally “Where’s the ‘New Window’ command?”):

                                          How can I prematurely end my script (if something weird happens)?

                                          Here’s what I came up with for this:

                                          def bailout(msg):                               # routine to display error message and exit
                                              print(msg)                                  # duplicate msg to console
                                              notepad.messageBox(msg, "Error", 0)
                                              user32.keybd_event(VK_MENU,   0, 0,     0)  # press alt key
                                              user32.keybd_event(VK_P,      0, 0,     0)  # press P key (to activate "Plugins" menu)
                                              user32.keybd_event(VK_P,      0, KEYUP, 0)  # release P key
                                              user32.keybd_event(VK_MENU,   0, KEYUP, 0)  # release alt key
                                              user32.keybd_event(VK_P,      0, 0,     0)  # press P key (to activate "Python Script" submenu)
                                              # above assumes no other Plugins starting with "P" are installed
                                              user32.keybd_event(VK_P,      0, KEYUP, 0)  # release P key
                                              user32.keybd_event(VK_RIGHT,  0, 0,     0)  # press cursor right key (to enter submenu)
                                              user32.keybd_event(VK_RIGHT,  0, KEYUP, 0)  # release cursor right key
                                              user32.keybd_event(VK_DOWN,   0, 0,     0)  # press cursor down key
                                              user32.keybd_event(VK_DOWN,   0, KEYUP, 0)  # release cursor down key
                                              user32.keybd_event(VK_DOWN,   0, 0,     0)  # press cursor down key again (to move to "Stop Script")
                                              user32.keybd_event(VK_DOWN,   0, KEYUP, 0)  # release cursor down key
                                              user32.keybd_event(VK_RETURN, 0, 0,     0)  # press enter key
                                              user32.keybd_event(VK_RETURN, 0, KEYUP, 0)  # release enter key
                                          
                                          E 1 Reply Last reply Jan 12, 2023, 5:43 AM Reply Quote 0
                                          29 out of 53
                                          • First post
                                            29/53
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors