2
$\begingroup$

Does anyone know if there is a builtin way to get all opened locations in the file explorer?

Like for browsers, the browser itself remembers the last opened locations. So even if it crashes one can restore them. But for file explorer, once the processes crash for some reason there is no way to get that information back.

Maybe there is some Developer context function or maybe someone knows how to get this information using the NetLink functionality? So I can periodically store them in my notebook so I don't lose them.

$\endgroup$
1
  • 1
    $\begingroup$ What is the file explorer? If you mean the interface for picking a file that Mathematica will open up, then I’m pretty sure the answer is no. That’s implemented by the OS so both the OS and Mathematica would need to expose some API and I’m not sure if either of them do. $\endgroup$
    – b3m2a1
    Feb 7, 2020 at 19:55

1 Answer 1

4
$\begingroup$

If you are referring to the Windows File Explorer, then we can use NETLink and the Shell API to get a list of open windows:

Needs["NETLink`"]
InstallNET[];

shellWindows[] :=
  NETBlock@Module[{app, windows, w, item}
  , Internal`WithLocalSettings[
      app = CreateCOMObject["Shell.Application"]
    , windows = app@Windows[]
    ; Table[
        Internal`WithLocalSettings[
          item = windows@Item[w]
        , item@Name -> item@LocationURL
        , ReleaseCOMObject[item]
        ]
      , {w, 0, windows@Count-1}
      ] // GroupBy[First->Last]
    , Scan[ReleaseCOMObject, {windows, app}]
    ]
  ]

This function retrieves an association of the names and file URLs of all open shell windows, grouped by their names. File Explorer windows will all have the name File Explorer on Windows 10 (or Windows Explorer on earlier versions of Windows).

For example:

shellWindows screenshot

This function will also return entries for other shell windows that happen to be open, such as Internet Explorer. But such entries will be grouped under other keys.

$\endgroup$
8
  • $\begingroup$ I have a few questions, does the manual release of COM objects necessary? Doesn't the NETBlock automatically release them when it ends? Secondly, what is the purpose of Internal`WithLocalSettings? Thirdly, is UninstallNET[] needed to completely close all connections? Fourthly, once you release the COM object calling any of its methods obviously fails but is there a function to check whether COM object bound to a WL symbol is active or has been released? Finally, is there a function to get all the method names for the COM object? $\endgroup$
    – user13892
    Feb 8, 2020 at 14:15
  • $\begingroup$ Also, was the localization of w necessary? Isn't it automatically localized since it is a Table variable? $\endgroup$
    – user13892
    Feb 8, 2020 at 14:20
  • $\begingroup$ NETBlock only releases .NET objects. COM objects need manual release. WithLocalSettings is used to ensure the release even if an error occurs within the body. UninstallNET can be used if you know you are done with all .NET interaction. I usually leave it open for the whole session. I do not know of a way to test a released COM object. The only ways I know to get a list of COM methods is to read the docs (which I did here) or inspect the type library (if you have one available). Yes, w is necessary. $\endgroup$
    – WReach
    Feb 8, 2020 at 14:48
  • $\begingroup$ Thank you @WReach for the answers! Do you also know how to get the list of all currently running programs being displayed on the windows taskbar? If you want I can open a new question. $\endgroup$
    – user13892
    Feb 8, 2020 at 15:20
  • $\begingroup$ I don't know how to do that, but you might start from something like this. $\endgroup$
    – WReach
    Feb 8, 2020 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.