Community
    • Login

    Linked text to open other files into Notepad++

    Scheduled Pinned Locked Moved General Discussion
    28 Posts 4 Posters 11.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.
    • Alan KilbornA
      Alan Kilborn
      last edited by

      As a side note, if you work a lot with pathnames that contain spaces and you want to use the above technique, it could be helpful to record a macro for the following Replace All operation:

      228e7973-67fb-46dc-abb5-2bd490e11435-image.png

      The little blue rectangle in the Find what: box is a single space.

      After recording and saving the macro, when you are dealing with a pathname with a space and it is problematic as a link because of the spaces, e.g.:

      31c74096-b009-41cd-8447-d572ef64c211-image.png

      Just select it:

      39260356-70e1-4bc5-afc0-e00e0ab42c45-image.png

      And run the macro, to obtain:

      fdd19d4a-5cb4-4bfb-9475-55ab85facf6e-image.png

      Kinda basic stuff, but maybe worth explicitly pointing out…

      1 Reply Last reply Reply Quote 3
      • Alan KilbornA
        Alan Kilborn
        last edited by

        I should also mention that I didn’t show an example of network paths, but they work fine with this technique as well.

        For example, the text for this cheesy network path:

        edit:\\127.0.0.1\w$\junk\test.txt

        becomes a nice alt+clickable link that works and appears as you’d expect in a N++ tab:

        1b1a8003-c2c9-4cbf-9a8c-656fcc24b88d-image.png

        1 Reply Last reply Reply Quote 3
        • Alan KilbornA Alan Kilborn referenced this topic on
        • Alan KilbornA Alan Kilborn referenced this topic on
        • J. BrouwerJ
          J. Brouwer
          last edited by

          @Alan-Kilborn

          A nice script for a general workaround on this issue/these issues (I have imported it into the NPP Python Script plugin), though not quite helpful for my case at the moment, that is a pity.

          But, of course, I understand that Notepad is textually based in the first place. So there is a logic in it that this script is for opening .txt files, and within a Notepad++ tab.

          Nevertheless, this script could be helpful for me in other cases in the future, so when not working with graphic files, like .jpg, .png, and .gif, mostly.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @J. Brouwer
            last edited by PeterJones

            @J-Brouwer said in Linked text to open other files into Notepad++:

            though not quite helpful for my case at the moment, that is a pity.

            Well, perhaps the idea (in the other thread that pointed you here) was that you could take this and build upon it to in some way meet your need.

            J. BrouwerJ 1 Reply Last reply Reply Quote 0
            • J. BrouwerJ
              J. Brouwer @Alan Kilborn
              last edited by

              @Alan-Kilborn

              I cannot code, unfortunately…

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @J. Brouwer
                last edited by Alan Kilborn

                @J-Brouwer

                Well, it certainly wouldn’t be that hard (for me) to augment the script to open “associated” files from the operating system, e.g. if the underlined text ends in .jpg, when alt+clicked it would open whatever program on your system defaults to opening them.

                Does everything else about the script meet your need? Meaning that if it was expressed as the following underlined text:

                edit:w:\junk\test%20but%20I%20have%20spaces.jpg

                and when you alt+clicked it, it opens in your jpg viewer/editor, that would be good?

                J. BrouwerJ 1 Reply Last reply Reply Quote 0
                • J. BrouwerJ
                  J. Brouwer @Alan Kilborn
                  last edited by

                  @Alan-Kilborn

                  That would be marvelous! Need to say here that my standard app for, amongst others, .jpg, .png and .gif files is picture viewer IrfanView 64-bit, or not?

                  Thanks in advance.

                  Alan KilbornA 1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @J. Brouwer
                    last edited by

                    @J-Brouwer said in Linked text to open other files into Notepad++:

                    Need to say here that my standard app for, amongst others, .jpg, .png and .gif files is picture viewer IrfanView 64-bit, or not?

                    Is IrfanView what opens when you double-click any files of these types from Explorer? If yes, then No, you don’t need to say it.

                    I’ll add the capability; check back in a day or two…

                    1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn
                      last edited by Alan Kilborn

                      So what follows is the UriIndicatorAltClick2.py script. This version handles opening the default viewer/editor for linked text that is not a known type to the script.

                      The script as written makes .txt and .log files the known types, so those will be opened in Notepad++. To change this, simply change edit_with_npp_lowercase_extension_list in the code to add/subtract…even a non-coder can do this.

                      Other types are unspecified, but an example would be .jpg files – these will be opened by whatever program is associated with them in the operating system.

                      Examples:

                      • edit:c:\test.txt <-- will be underlined and if Alt+clicked will open test.txt in N++
                      • edit:d:\image.jpg <-- will be underline and if Alt+clicked will open image.jpg in the default program for the jpg file type

                      As to spaces in the pathnames, as per the original version of the script, these will need to be replaced with %20 in order for the entire pathname to be recognized as a clickable link, example: edit:d:\my%20image.jpg.

                      The script listing:

                      # -*- coding: utf-8 -*-
                      
                      # see https://community.notepad-plus-plus.org/topic/22274/linked-text-to-open-other-files-into-notepad
                      
                      from Npp import *
                      import os
                      import re
                      import ctypes
                      
                      class UIAC2(object):
                      
                          def __init__(self):
                              self.edit_with_npp_lowercase_extension_list = [ 'txt', 'log' ]
                              self.URL_INDIC = 8  # URL_INDIC is used in N++ source code
                              self.ALT_MODIFIER = 4
                              self.backslash = '\\' ; self.two_backslashes = self.backslash * 2
                              self.alt_held_at_click = False
                              self.installed = False
                              self.install()
                      
                          def install(self):
                              if not self.installed:
                                  # https://www.scintilla.org/ScintillaDoc.html#SCN_INDICATORCLICK
                                  editor.callback(self.indicator_click_callback, [SCINTILLANOTIFICATION.INDICATORCLICK])
                                  # https://www.scintilla.org/ScintillaDoc.html#SCN_INDICATORRELEASE
                                  editor.callback(self.indicator_release_callback, [SCINTILLANOTIFICATION.INDICATORRELEASE])
                                  self.installed = True
                      
                          def uninstall(self):
                              if self.installed:
                                  editor.clearCallbacks(self.indicator_click_callback)
                                  editor.clearCallbacks(self.indicator_release_callback)
                                  self.installed = False
                      
                          def is_installed(self):
                              return self.installed
                      
                          def mb(self, msg, flags=0, title=''):
                              return notepad.messageBox(msg, title, flags)
                      
                          def extension_from_path(self, path):
                              l = path.rsplit('.', 1)
                              ext = l[1] if len(l) == 2 else ''
                              return ext
                      
                          def shell_open(self, uri_text, args=None):
                              SW_SHOW = 5
                              return ctypes.windll.Shell32.ShellExecuteA(None, 'open', uri_text, args, None, SW_SHOW) > 32
                      
                          def get_indicator_range(self, indic_number):
                              # similar to ScintillaEditView::getIndicatorRange() in N++ source
                              # https://github.com/notepad-plus-plus/notepad-plus-plus/blob/8f38707d33d869a5b8f5014dbb18619b166486a0/PowerEditor/src/ScitillaComponent/ScintillaEditView.h#L562
                              curr_pos = editor.getCurrentPos()
                              indic_mask = editor.indicatorAllOnFor(curr_pos)
                              if (indic_mask & (1 << indic_number)) != 0:
                                  start_pos = editor.indicatorStart(indic_number, curr_pos)
                                  end_pos = editor.indicatorEnd(indic_number, curr_pos)
                                  if curr_pos >= start_pos and curr_pos <= end_pos:
                                      return (start_pos, end_pos)
                              return (0, 0)
                      
                          def indicator_click_callback(self, args):
                              # example: INDICATORCLICK: {'position': 12294, 'idFrom': 0, 'modifiers': 4, 'code': 2023, 'hwndFrom': 1577146}
                              #print('UriIndicatorAltClick indicator click callback')
                              self.alt_held_at_click = (args['modifiers'] & self.ALT_MODIFIER) != 0
                      
                          def indicator_release_callback(self, args):
                      
                              # example: INDICATORRELEASE: {'position': 12294, 'idFrom': 0, 'modifiers': 0, 'code': 2024, 'hwndFrom': 1577146}
                      
                              #print('UriIndicatorAltClick indicator release callback')
                      
                              if not self.alt_held_at_click: return
                              self.alt_held_at_click = False
                      
                              (start_pos, end_pos) = self.get_indicator_range(self.URL_INDIC)
                              if start_pos == end_pos:  return  # if click on indicator that is not URL_INDIC
                      
                              uri_text = editor.getTextRange(start_pos, end_pos)
                      
                              (uri_scheme, _, uri_path) = uri_text.partition(':')
                      
                              uri_path = uri_path.replace('%20', ' ').replace('%24', '$').replace('/', self.backslash)
                      
                              # check for optional syntax at end:   edit:....txt(L127,C12)
                              goto_line = goto_col = 0
                              m = re.search(r'\(L(-?\d+)(?:,C(\d+))?\)$', uri_path)
                              if m:
                                  uri_path = uri_path[:-len(m.group())]
                                  goto_line = int(m.group(1))
                                  if m.group(2): goto_col = int(m.group(2))
                      
                              if not os.path.isfile(uri_path):
                                  # look for a relative path, relative to currently active document
                                  try:
                                      (valid_dir_of_active_doc, _) = notepad.getCurrentFilename().rsplit(os.sep, 1)
                                  except ValueError:
                                      # we started out in a "new 1" file, no path on that whatsoever
                                      self.mb('Cannot find file:\r\n\r\n{}'.format(uri_path))
                                      return
                                  test_path_in_active_doc_dir = os.path.join(valid_dir_of_active_doc, uri_path)
                                  if os.path.isfile(test_path_in_active_doc_dir):
                                      uri_path = test_path_in_active_doc_dir
                                  else:
                                      (test_dir, test_filename) = test_path_in_active_doc_dir.rsplit(os.sep, 1)
                                      if os.path.isdir(test_dir):
                                          expanded_test_dir = os.path.abspath(test_dir)
                                          if expanded_test_dir != test_dir:
                                              self.mb('Cannot find file:\r\n\r\n{}\r\n\r\nLooked in this dir:\r\n\r\n{}'.format(test_filename, expanded_test_dir))
                                              return
                                      self.mb('Cannot find file:\r\n\r\n{}'.format(uri_path))
                                      return
                      
                              opened_in_npp = False
                              if self.extension_from_path(uri_path).lower() in self.edit_with_npp_lowercase_extension_list:
                                  notepad.open(uri_path)
                                  opened_in_npp = True
                              else:
                                  self.shell_open(uri_path)
                      
                              if opened_in_npp and goto_line != 0:
                                  if goto_line == -1: goto_line = editor.getLineCount()
                                  goto_line -= 1
                                  if goto_col != 0:
                                      goto_col_pos = editor.findColumn(goto_line, goto_col)
                                      editor.gotoPos(goto_col_pos)
                                  else:
                                      editor.gotoLine(goto_line)
                      
                      if __name__ == '__main__':
                      
                          if 'uiac2' not in globals():
                              uiac2 = UIAC2()  # will automatically "install" it
                          else:
                              # each running the script toggles install/uninstall:
                              uiac2.uninstall() if uiac2.is_installed() else uiac2.install()
                              print('uiac2 installed?:', uiac2.is_installed())
                      
                      J. BrouwerJ 1 Reply Last reply Reply Quote 1
                      • Alan KilbornA Alan Kilborn referenced this topic on
                      • J. BrouwerJ
                        J. Brouwer @Alan Kilborn
                        last edited by

                        @Alan-Kilborn

                        Wow, nice and working! Now we got a real workaround for this space character deficiency problem in NPP.

                        I, being a genuine nag, have found another issue, yet. There is a problem with opening files containing special - or even not that “special” - characters, like the following: edit:C:\Users\Jack\Desktop\probleem%20voor%20alldup\kopiëren.txt, where that “ë” is the culprit here. Is it because of “coding: utf-8”, in line 1 of your script, maybe?
                        UTF-8 is selected as the character set in my version of NPP, so that should be OK, I guess.

                        Alan KilbornA 1 Reply Last reply Reply Quote 1
                        • Alan KilbornA
                          Alan Kilborn @J. Brouwer
                          last edited by

                          @J-Brouwer said in Linked text to open other files into Notepad++:

                          kopiëren.txt, where that “ë” is the culprit here…

                          Ok, so there are 2 problems:

                          • I don’t use such characters when I name files (which means I don’t typically code for that situation – but I understand that others want to name files with ‘special’ characters)

                          • The non-beta PythonScript plugin uses Python2, which makes it a bit of a painful experience to write code that deals with the situation

                          That being said, I managed to mod the script a bit, and it seems to work when I test it with your filename in my file system – but I really have no idea how “fragile” it might be.

                          I’ll dub it the “2a” version of the script and here are the changes:

                          • change editor.getTextRange(start_pos, end_pos) to be unicode(editor.getTextRange(start_pos, end_pos), 'utf-8')

                          • change ShellExecuteA(None, 'open' to be ShellExecuteW(None, u'open'

                          J. BrouwerJ 1 Reply Last reply Reply Quote 2
                          • J. BrouwerJ
                            J. Brouwer @Alan Kilborn
                            last edited by

                            @Alan-Kilborn

                            I have changed the two line fragments in v. 2, and have saved it as v. 2a.

                            But now, unfortunately, it does not work anymore, besides of underlining the text of the urn; after an Alt-LClick in that line, just nothing happens.

                            I have opened the Python console, and here is what it reports:

                            Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
                            Initialisation took 32ms
                            Ready.
                            Traceback (most recent call last):
                              File "C:\Program Files\Notepad++\plugins\PythonScript\scripts\Samples\UriIndicatorAltClick2a.py", line 116, in indicator_release_callback
                                notepad.open(uri_path)
                            Boost.Python.ArgumentError: Python argument types in
                                Notepad.open(Notepad, unicode)
                            did not match C++ signature:
                                open(class NppPythonScript::NotepadPlusWrapper {lvalue}, char const * __ptr64 filename)
                            Traceback (most recent call last):
                              File "C:\Program Files\Notepad++\plugins\PythonScript\scripts\Samples\UriIndicatorAltClick2a.py", line 116, in indicator_release_callback
                                notepad.open(uri_path)
                            Boost.Python.ArgumentError: Python argument types in
                                Notepad.open(Notepad, unicode)
                            did not match C++ signature:
                                open(class NppPythonScript::NotepadPlusWrapper {lvalue}, char const * __ptr64 filename)
                            
                            

                            I believe this is twice the same traceback.

                            Alan KilbornA 1 Reply Last reply Reply Quote 0
                            • Alan KilbornA
                              Alan Kilborn @J. Brouwer
                              last edited by

                              @J-Brouwer said in Linked text to open other files into Notepad++:

                              But now, unfortunately, it does not work anymore

                              Hmm, yea, apparently I didn’t retest that part (opening a link into Notepad++)…sorry.

                              I think this falls into the “fragile” realm I mentioned before. Hack something to mix unicode into Python2, something else breaks…

                              It probably gets a lot cleaner with Python3 and the beta version of the PythonScript plugin. As I haven’t really dipped into that realm yet, I don’t have an instant solution for the problem at hand.

                              But this may be a good opportunity for me to test the waters with the newer PythonScript. I’ve been writing Python3 code for years, just not with Notepad++.

                              I’ll post something more when I have something…check back in a bit…

                              1 Reply Last reply Reply Quote 0
                              • Alan KilbornA
                                Alan Kilborn
                                last edited by Alan Kilborn

                                Ok, I had some free time earlier than expected…

                                I started with the beta 3.10.2 version of PythonScript.
                                Then I took the UriIndicatorAltClick2.py version of the script (above from Sept 12 9:58 posting) and made a single-character change to it:

                                • change ShellExecuteA to ShellExecuteW

                                (If we need to refer to it further, maybe this becomes the 2b version)

                                And with that everything seemed to work, for files with or without “special characters” in their names.

                                So, @J-Brouwer , my suggestion is that you give that a try.

                                J. BrouwerJ 1 Reply Last reply Reply Quote 1
                                • J. BrouwerJ
                                  J. Brouwer @Alan Kilborn
                                  last edited by

                                  @Alan-Kilborn said in Linked text to open other files into Notepad++:

                                  I started with the beta 3.10.2 version of PythonScript.

                                  Where could that version be found? I searched the internet for it, and all that I could find were versions below v. 2. The PythonScript plugin installed within my version of NPP is 2.

                                  Anyway, I have tried to use the 2b version, but to no avail. According to the Python console traceback, the same error in line 116 occured.

                                  Now I have loaded v. 2 again, so a sort of a downgrade that is. But that version is working in my situation, to a great extent.

                                  All of this is becoming too complicated for me. Like I said before, I am not a coder. Moreover, English is not my native tongue.

                                  Finally, I want to remark that in many languages diacritical characters (like ë, though we got Emily Brontë :-)) are (quite) more common than in English.
                                  In my own language spelling “kopieren”, instead of “kopiëren” (Dutch, meaning “to copy”), would just be wrong.

                                  Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                                  • Alan KilbornA
                                    Alan Kilborn @J. Brouwer
                                    last edited by

                                    @J-Brouwer said in Linked text to open other files into Notepad++:

                                    All of this is becoming too complicated for me.

                                    All of what? What does this mean? You’re giving up on it?
                                    You, who’s been asked to put exactly zero effort in along the way?
                                    Ok, then, I won’t put any more effort in either…

                                    J. BrouwerJ 1 Reply Last reply Reply Quote 0
                                    • PeterJonesP
                                      PeterJones @J. Brouwer
                                      last edited by PeterJones

                                      @J-Brouwer said in Linked text to open other files into Notepad++:

                                      Where could that version be found?

                                      The same github repository where the non-beta version comes from. You can find out this repository by going to Notepad++'s Plugins > Plugins Admin, clicking on the Installed tab ‡, and clicking on PythonScript – it will show you the Homepage: .... You paste that into your browser, click on the Releases link on the right, and voila, you can see “v3.0.14 [Pre-Release]” right there at the top of the releases page.

                                      (‡: it’s on the Installed tab for you, because you already have PythonScript v2 installed. If someone else found these instructions but they don’t yet have PythonScript installed at all, they will have to look on the Available tab instead.)

                                      I searched the internet for it

                                      You might want to spend some time refining your search, then. When I searched for “Notepad++ PythonScript v3”, the first link was the old sourceforge home for PythonScript (understandable as a first hit, but it’s not the current home); the second link was a 2019 post in this forum (which predated v3); but the third link was a post from this June which talks about how to install the v3 PythonScript – success in 3 links! That’s pretty good for the first combination of search terms I tried.

                                      J. BrouwerJ Alan KilbornA 2 Replies Last reply Reply Quote 2
                                      • J. BrouwerJ
                                        J. Brouwer @Alan Kilborn
                                        last edited by

                                        @Alan-Kilborn

                                        ???

                                        1 Reply Last reply Reply Quote 0
                                        • J. BrouwerJ
                                          J. Brouwer @PeterJones
                                          last edited by

                                          @PeterJones said in Linked text to open other files into Notepad++:

                                          success in 3 links! That’s pretty good for the first combination of search terms I tried.

                                          Well, not in my case then. With about the same search words, that third hit was not given at all. As we know, everyone individually - person, machine, IP-address -, is put in “bubbles” by search engines. So, after having seen about 15 results, I gave up that effort.

                                          PeterJonesP 1 Reply Last reply Reply Quote 0
                                          • Alan KilbornA
                                            Alan Kilborn @PeterJones
                                            last edited by Alan Kilborn

                                            @PeterJones said in Linked text to open other files into Notepad++:

                                            success in 3 links!

                                            Same for me (3rd hit) – which in my case was the current github home for the plugin.
                                            My choice of search phrase was not influenced by Peter’s as I purposefully avoided looking at his before trying my own.
                                            Mine was: notepad++ python script plugin

                                            It IS unfortunate that the first hit we both obtained is for a really old version of PythonScript on sourceforge (ugh), but that page makes it fairly clear that the version there is 1.0.8.0, and since this thread was discussion version 3.0.12 (in my earlier post, I just used something 3-ish that I had on hand, I didn’t seek out the latest which appears to be 3.14), it should be obvious that the sourceforge one is not the one wanted here. BTW, any version of “3” should work for purposes of this thread.

                                            I suppose in the future we should probably do more spoonfeeding and post exact links, for those that have trouble searching the internet. More work for the people already doing the work, I guess. :-(

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors