How to read the full path to a focused file?


Han Solo
 

Hi,
I am trying to understand it, but I don't know how. Can you send an example that will display the full path to the file in the explorer in ui.browseableMessage after pressing the keyboard shortcut?
thanks and regards


Rui Fontes
 

Hello!


    def getDocName(self):
        docPath = ""
        # getting file path and name
        fg = api.getForegroundObject()
        # We check if we are in the Windows Explorer.
        if fg.role != api.controlTypes.Role.PANE and fg.appModule.appName != "explorer":
            return
        shell = COMCreate("shell.application")
        # We go through the list of open Windows Explorers to find the one that has the focus.
        for window in shell.Windows():
            try:
                if window.hwnd and window.hwnd == fg.windowHandle:
                    focusedItem=window.Document.FocusedItem
                    break
            except:
                pass
        else: # loop exhausted
            desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
            docPath = '\"' + desktop_path + '\\' + api.getDesktopObject().objectWithFocus().name + '\"'
            return
        # Now that we have the current folder, we can explore the SelectedItems collection.
        targetFile= focusedItem.path
        docPath = '\"'+str(targetFile)+'\"'
        return docPath


This code was originally develloped byAlberto Buffolino  for the Column review add-on...

I use it in TesseractOCR and NAPS2OCR add-ons...


Best regards,

Rui Fontes
NVDA portuguese team



Às 18:46 de 25/09/2022, Han Solo escreveu:

Hi,
I am trying to understand it, but I don't know how. Can you send an example that will display the full path to the file in the explorer in ui.browseableMessage after pressing the keyboard shortcut?
thanks and regards