Re: How to get working directory?


Rui Fontes
 

And, for what I need it have two advantages:

1 - It works in the special folders like Downloads, Documents and so on;

2 - It retrieves the full path with extension even if the extension is hidden.


Many, many thanks!


Rui Fontes


Às 22:40 de 14/09/2021, Robert Hänggi escreveu:

I'm glad that it is working.
Cheers
Robert

On 14/09/2021, Rui Fontes <rui.fontes@tiflotecnia.com> wrote:
Hello Robert!


Many thanks! With two small corrections, it works beautifully in last
Alpha versions of NVDA and Windows 10 in portuguese...

The corrections are in the:

api.controlTypes.Role.PANE #because of changes in controlTypes

and

focusedItem.path # in Python 3 we do not need the encoding...


        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
        # self.shell = self.shell or
ct.client.CreateObject("shell.application")
        self.shell = COMCreate("shell.application")
        # We go through the list of open Windows Explorers to find the
one that has the focus.
        for window in self.shell.Windows():
            if window.hwnd == fg.windowHandle:
                focusedItem=window.Document.FocusedItem
                break
        else: # loop exhausted
            return
        # Now that we have the current folder, we can explore the
SelectedItems collection.
        targetFile= focusedItem.path


Best regards,

Rui Fontes
NVDA portuguese team


Às 21:56 de 13/09/2021, Robert Hänggi escreveu:
Rui, you will probably have to adapt the code.
I wrote it for 2.7.
Some import statements might also be missing, after all it is only an
excerpt.

from comtypes.client import CreateObject as COMCreate
import api
import os

(global class definition would go here)

def script_sayValue(self, gesture):
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
#self.shell = self.shell or ct.client.CreateObject("shell.application")
self.shell = COMCreate("shell.application")
# We go through the list of open Windows Explorers to find the one
that has the focus.
for window in self.shell.Windows():
if window.hwnd == fg.windowHandle:
focusedItem=window.Document.FocusedItem
break
else: # loop exhausted
return
# Now that we have the current folder, we can explore the
SelectedItems collection.
targetFile= focusedItem.path.encode('mbcs')
(snip)

Cheers
Robert

On 13/09/2021, Rui Fontes <rui.fontes@tiflotecnia.com> wrote:
Hello!


@Abdel

Even it was not what the person wants, I myself need that...


@Robert Hänggi, you wrote:Why not using the com interface?
It gives you the selected or focused file in the explorer, if that is
what you want.
There are also some functions for the mentioned special folders, if I
don't err.


I need the full path of the file even in the special folders...

Can you gave me some topics to start?


Rui Fontes


how to use

Às 16:34 de 13/09/2021, Abdel escreveu:
Hi Rui,

Thanks for these explanations, you are right.

We'll wait if it corresponds to what the person is asking for.

Kind regards,
Abdel.

Le 13/09/2021 à 16:01, Rui Fontes a écrit :
Hi Abdel!


If the folder is Downloads, Documents, Images and so on, it will not
work...

The address copied is only Downloads, Documents and so on... Never
c:\users\user\downloads...


To the other folders, it is possible to use:

        path = ' '
        focusObj = api.getFocusObject()
            # If the focus is on windows explorer, gets the address
of the folder.
        if 'explorer' in focusObj.appModule.appModuleName:
            hForeground = api.getForegroundObject().windowHandle
            h = self.findDescendantWindow(hForeground, 1001)
            if not h:
                h = self.findDescendantWindow(hForeground, 41477)
                h = self.findDescendantWindow(h, "Edit")
            if h:
                obj = NVDAObjects.IAccessible.getNVDAObjectFromEvent
(h, -4, 0)
                if getwindowsversion().major == 5: # windows xp.
                    path = obj.value
                else:
                    name = obj.name


Rui Fontes


Às 22:49 de 12/09/2021, Abdel escreveu:
Hi,

I think the person is talking about the current directory in Windows
Explorer.

You can use "Ald + D" to switch to the address bar.

If that doesn't work, press "F4" or "Control + L".

Then, to copy the address, tab twice and you will find the path copy
choice in the context menu.

Is this what you are looking for?

Kind regards,
Abdel.

Le 12/09/2021 à 13:03, Lukasz Golonka via groups.io a écrit :
On Sun, 12 Sep 2021 18:52:41 +0800 (CST)
"高生旺" <coscell@gmail.com> wrote:

In file explorer I can choose any folder.
Are you asking how to get name of the folder opened in Windows
Explorer?
If so in what opened folder are you interested in? Perhaps if you
would
tell us what you want to achieve in the end we would be able to
assist you
better.

< That folder is the working directory.

Not really.  Working directory is per process i.e. os.getcwd
retrieves a
working directory of a calling program. There is no such thing as a
working directory global to  Windows.










Join nvda-addons@nvda-addons.groups.io to automatically receive all group messages.