How to get working directory?


=?ISO-2022-JP?Q?=1B$B9b=4082=22=1B=28J?=
 

In my addon, if I call os.getcwd(), it always return
c:\Program Files (x86)\nvda.
How can I get real working path?


Fawaz Abdul rahman
 

Hi,
os.path.dirname(__file) should give you the path of the current script.
if you want the absolute path then
os.path.abspath(os.path.dirname(__file))
good luck.


On Sun, Sep 12, 2021 at 6:58 AM 高生旺 <coscell@...> wrote:
In my addon, if I call os.getcwd(), it always return
c:\Program Files (x86)\nvda.
How can I get real working path?






=?ISO-2022-JP?Q?=1B$B9b=4082=22=1B=28J?=
 

I don't need the path of the current script.
I just need working directory.

On Sun, 12 Sep 2021, Fawaz Abdul rahman wrote:

Date: Sun, 12 Sep 2021 10:09:55 +0300
From: Fawaz Abdul rahman <fawaz.ar94@gmail.com>
Reply-To: nvda-addons@nvda-addons.groups.io
To: "nvda-addons@nvda-addons.groups.io Integration"
<nvda-addons@nvda-addons.groups.io>
Subject: Re: [nvda-addons] How to get working directory?
Hi,
os.path.dirname(__file) should give you the path of the current script.
if you want the absolute path then
os.path.abspath(os.path.dirname(__file))
good luck.
On Sun, Sep 12, 2021 at 6:58 AM 高生旺 <coscell@gmail.com> wrote:
In my addon, if I call os.getcwd(), it always return
c:\Program Files (x86)\nvda.
How can I get real working path?


Lukasz Golonka
 

On Sun, 12 Sep 2021 17:23:42 +0800 (CST)
"高生旺" <coscell@gmail.com> wrote:

I don't need the path of the current script.
I just need working directory.
What do you mean by 'working directory'?

--
Regards
Lukasz


=?ISO-2022-JP?Q?=1B$B9b=4082=22=1B=28J?=
 

In file explorer I can choose any folder. That folder is the working directory.

On Sun, 12 Sep 2021, Lukasz Golonka via groups.io wrote:

Date: Sun, 12 Sep 2021 11:28:31 +0200
From: Lukasz Golonka via groups.io <lukasz.golonka=mailbox.org@groups.io>
Reply-To: nvda-addons@nvda-addons.groups.io
To: nvda-addons@nvda-addons.groups.io
Subject: Re: [nvda-addons] How to get working directory?
On Sun, 12 Sep 2021 17:23:42 +0800 (CST)
"高生旺" <coscell@gmail.com> wrote:

I don't need the path of the current script.
I just need working directory.
What do you mean by 'working directory'?

--
Regards
Lukasz





Lukasz Golonka
 

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.

--
Regards
Lukasz


Abdel
 

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.


Rui Fontes
 

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.



Robert Hänggi
 

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.
Robert

On 13/09/2021, Rui Fontes <rui.fontes@tiflotecnia.com> wrote:
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.








Abdel
 

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.






Rui Fontes
 

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.









Robert Hänggi
 

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.














Rui Fontes
 

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.











Robert Hänggi
 

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.
















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.











Robert Hänggi
 

On 15/09/2021, Rui Fontes <rui.fontes@tiflotecnia.com> wrote:
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.
Absolutely.
I think the code is also used in the column review add-on for e.g.
searching for files.
There are in any case further interfaces with cool methods for files
and folders available within the shell environment.

I use it simply to do actions on specific files, e.g. previewing audio files.
Cheers
Robert



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.
















Alberto Buffolino
 

Robert Hänggi, il 15/09/2021 09.49, ha scritto:
I think the code is also used in the column review add-on for e.g.
searching for files.
Alberto:
yeah, and it still works very well today, thanks to your suggestion and other valuable contributions from here :)
For Rui, be careful, this approach does not work in all contexts, surely not in open dialog window; not very important for you, I think, but you could want to warn users about it (I did it in ColumnsReview).
Alberto


Robert Hänggi
 

Alberto, I would wish that there was a similar procedure for the
open/save dialogs.
WXWidgets has a (user) window message that can give you the currently
selected file, however, I wasn't successful in sending it from
"outside" the process.
There was also a procedure described on the AutoIt forum which works
essentially with the DropTarget interface.
It needed a dll from that forum which made it less attractive and the
code is quite complicated.
Unfortunately, I can't find it anymore.
Robert

On 15/09/2021, Alberto Buffolino <a.buffolino@gmail.com> wrote:
Robert Hänggi, il 15/09/2021 09.49, ha scritto:
I think the code is also used in the column review add-on for e.g.
searching for files.
Alberto:
yeah, and it still works very well today, thanks to your suggestion and
other valuable contributions from here :)
For Rui, be careful, this approach does not work in all contexts, surely
not in open dialog window; not very important for you, I think, but you
could want to warn users about it (I did it in ColumnsReview).
Alberto






Rui Fontes
 

Hi!

@Albert, for my use it is not a problem since those dialogs report as as app owner the app that have invoked them... In NotePad, press Ctrl+o to display the open file window and you can see the app in execution is still NotePad...

So, my script will say: You must be in a Windows File Explorer window...


@Robert, do you need to get the file selected in one of those open/save dialogs invoked by one specific app or from a dialog invoked by your script?

I have created an open file dialog to one of my add-ons in development and I can get the full path of selected file...


Rui Fontes


Às 14:23 de 15/09/2021, Robert Hänggi escreveu:

Alberto, I would wish that there was a similar procedure for the
open/save dialogs.
WXWidgets has a (user) window message that can give you the currently
selected file, however, I wasn't successful in sending it from
"outside" the process.
There was also a procedure described on the AutoIt forum which works
essentially with the DropTarget interface.
It needed a dll from that forum which made it less attractive and the
code is quite complicated.
Unfortunately, I can't find it anymore.
Robert

On 15/09/2021, Alberto Buffolino <a.buffolino@gmail.com> wrote:
Robert Hänggi, il 15/09/2021 09.49, ha scritto:
I think the code is also used in the column review add-on for e.g.
searching for files.
Alberto:
yeah, and it still works very well today, thanks to your suggestion and
other valuable contributions from here :)
For Rui, be careful, this approach does not work in all contexts, surely
not in open dialog window; not very important for you, I think, but you
could want to warn users about it (I did it in ColumnsReview).
Alberto







Robert Hänggi
 

I would need the access for a foreign dialog.
However, the script for the own/custom dialog might still be useful.
Robert

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

@Albert, for my use it is not a problem since those dialogs report as as
app owner the app that have invoked them... In NotePad, press Ctrl+o to
display the open file window and you can see the app in execution is
still NotePad...

So, my script will say: You must be in a Windows File Explorer window...


@Robert, do you need to get the file selected in one of those open/save
dialogs invoked by one specific app or from a dialog invoked by your
script?

I have created an open file dialog to one of my add-ons in development
and I can get the full path of selected file...


Rui Fontes


Às 14:23 de 15/09/2021, Robert Hänggi escreveu:
Alberto, I would wish that there was a similar procedure for the
open/save dialogs.
WXWidgets has a (user) window message that can give you the currently
selected file, however, I wasn't successful in sending it from
"outside" the process.
There was also a procedure described on the AutoIt forum which works
essentially with the DropTarget interface.
It needed a dll from that forum which made it less attractive and the
code is quite complicated.
Unfortunately, I can't find it anymore.
Robert

On 15/09/2021, Alberto Buffolino <a.buffolino@gmail.com> wrote:
Robert Hänggi, il 15/09/2021 09.49, ha scritto:
I think the code is also used in the column review add-on for e.g.
searching for files.
Alberto:
yeah, and it still works very well today, thanks to your suggestion and
other valuable contributions from here :)
For Rui, be careful, this approach does not work in all contexts, surely
not in open dialog window; not very important for you, I think, but you
could want to warn users about it (I did it in ColumnsReview).
Alberto