Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Powershell & Windows batch: get full path of active document displayed

Hello experts,

I was wondering if there is a way in Powershell & Windows batch to get the full path of active document displayed other than Windows office document such as png and pdf files?

Thank you in advance for your help.
Avatar of Bill Prew
Bill Prew

@Luis,

Can you explain further, it's not clear to me.

It sounds like you want to perhaps get a path to a file being worked on in any number of non-Office applications?

You mention image files, so perhaps you are thinking of the Windows "Photos" application?  Or are you wanting any application that has an image open, like Paint.net, etc?

You also mention PDF files, so would that need to be opened in Acrobat, or are you thinking anything.  Like web browsers often can display PDF files as well, and there are other viewers / applications.

If you are indeed asking the general question about getting the one active "document" path from any file open application, that feels like it's not doable.  Yes, I'll say you could find the active window when the script runs, and maybe link that to a process that tells you what application EXE owns that window.  But then finding out what the path of the file is I don't see as doable in general.  Some applications like office allow automation, and there might be one or two of these non-Office apps involved that support that, but it would be hit or miss, and the object model and interface for each would be different.

Bottom line, I'll say "you can't get there from here", at least not in a way that takes a reasonable amount of effort and is easily scaled to additional applications.


»bp
Avatar of Luis Diaz

ASKER

Hi Bill,

I am talking about two main applications.

Screenpresso:User generated image
No feature available
PdfEditor:User generated imageFeature available but I need to select the tab.

I have an AutoHotkey which cover this need for Office applications and Notepad++ applications.
The final objective is to include Screenpresso and PdfEditor to the process, in order to ensure simplicity and a single approach for the applications used regularly.

Another approach that we can take and I think it can be manageable:
Scan/Loop the recent item folder and get the full path of the most recent file located at:
I know that recent items folder get always the last open file as a result we can get the information.
User generated image
I was wondering if there is a way in Powershell & Windows batch to get the full path of active document displayed other than Windows office document such as png and pdf files?
Re-reading this now I'm curious, what are you going to do once you know that say PdfEditor currently has document "c:\temp\doc1.pdf" open?


»bp
Attach the file on Outlook, EE, Gmail or Google drive.
With regard to the recent files list, not sure if this is of any use, but maybe you leverage a powershell script to get the most recent file paths?

Something like:

$WScript = New-Object -ComObject WScript.Shell
Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\*.lnk" | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Select-String -Pattern '\.(pdf|png)$'

Open in new window


»bp
Hi Bill,
the script proposed works perfectly for me:

User generated image
Now the question is if I want to get the first of the list, the most recent and send the information to the clipboard as recommended here: https://devblogs.microsoft.com/scripting/powertip-send-output-to-clipboard-with-powershell/ how do you think that I should proceed?

With those new requirements, I can test by opening a pdf file>launching the script and see if I get the full path of the file opened at PdfEditor or Screenpresso.
It's a Sunday here so I'm not able to do much on EE today, but I'll propose my thoughts tomorrow.


»bp
Ok Bill, No rush I will wait.
Now the question is if I want to get the first of the list, the most recent and send the information to the clipboard
Give this a try:

$WScript = New-Object -ComObject WScript.Shell; Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\*.lnk" | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Select-String -Pattern '\.(pdf|png)$' | Select-Object -First 1 | Clip

Open in new window


»bp
Hi Bill,

Tested and it works! perfectly. I am just having a little issue: when I paste the clipboard I got blank cells.
Is there a way to end the script by removing the blank lines of the clipboard?
User generated image
In order to have:User generated image

Thank you for your help.
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Bill, tested and it works! possible to have this in one-line command and a msgbox after the clipboard, by doing so I can move this to AutoHotkey and it is done.
Thank you for your help.
It might make more sense to save the PS code off in a PS1 file, and then launch it from AHK as that file, rather than trying to do it all on the command line (a lot of special characters to work around there potentially, quotes and pipes, etc.  Thoughts?


»bp
The advantage of having one line command is that I can centralize n a single ahk file. This is a real benefit for me as I use multiple computers and I constantly update my ahk file in which I already have more than 100 hotkey/hotsring. However, if this is not possible I will adapt myself to the situation.
I suspect with a little work you could do the whole thing right in AHK, without spawning out to Powershell...


»bp
Ok, I will try to deliver something.
I was wondering if you can help me to cover the following by talking as a reference the previous powershell script.
Send to clipboard just folders of recent items folder order descending by modified date.

Thank you for your help.
Do you mean modified date of the folder, or of the recently used LNK item?


»bp
And, would you only want folders included that still exist - it seems like it if you want to sort by there last update stamp...


»bp
Yes, only folders included that still exists and available in recent item folder.
Concerning the sort, with the previous proposal,
 -Property LastWriteTime -Descending

Open in new window

I think we can manage this.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much Bill,
I tested and it works!
Last question, it is possible to get the most recent folder with the same approach.
Thank you very much for your help.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Bill,

I tested but I got the following error message:
User generated imageI think that those folders related to this message still in recent items but they have been removed by myself . I don't know if there is a way to manage this case.

Additionally I was wondering how do you Msgbox in powershell the clipboard, this will help me to properly test that the expected folder has been selected.

Thank you for your help.
I tested but I got the following error message:
In testing here I can't reproduce that.  The code already filters out files/folders that no longer exist, and it should never get to the Get-Item that seems to be causing the error if it doesn't exist.  In testing here it worked as expected.  

What version of Powershell are your running there?

And did you alter my suggestion at all or just past it in to Powershell script and run it?


»bp
Hi Bill,

I am using Windows PowerShell ISE (x86) I also test it with Windows PowerShell ISE and I have the same error.
User generated imageI also noticed that the folders related to error are empty probably this cause the error message.

I didn't alter your suggestion. I took the proposal of #a43046122

Regards,
Luis.
I still can't reproduce here, perhaps some sort of a permissions problem there?  You will need to look at the folders that are causing the error and try and identify what is unique about them.  Once we know that we could code for that condition.

The other approach is to "catch" the exception in PS, but that feels like overkill since we might want to see an error for some other reason, and not just ignore any errors trying to access a folder that exists.


»bp
Hi Bill,

I noticed that all folders related to error message were hidden, I change folder properties to show all files and folder hidden and I don't have the same error.

I have the following error message related to a single file:

The specified wildcard character pattern is not valid: Demonstration in the 
tool [tool t. . . - Monday, March 2, 2020 8.02.10 AM.mp4

Open in new window


Questions:
1.How to manage the error message if I am in hidden mode, possible to adjust this at script level?
2.Which is the character that cause problem of file named  Demonstration in the tool [tool t. . . - Monday, March 2, 2020 8.02.10 AM.mp4?

Thank you in advance for your help.
I created a file with the name you posted in your comment, and did a test using this Powershell command line:

$WScript = New-Object -ComObject WScript.Shell; Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\*.lnk" | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Select-String -Pattern '\.(pdf|png|mp4)$' | Select-Object -ExpandProperty Line -First 1 | Set-Clipboard

Open in new window

I got this result in the console, no errors:

PS C:\Users\xxxxx> $WScript = New-Object -ComObject WScript.Shell; Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\*.lnk" | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Select-String -Pattern '\.(pdf|png|mp4)$' | Select-Object -ExpandProperty Line -First 1 | Set-Clipboard

Open in new window

And when I pasted from the clipboard, as expected I got this:

PS C:\Users\xxxxx> C:\Temp\Demonstration in the tool [tool t. . . - Monday, March 2, 2020 8.02.10 AM.mp4

Open in new window

It may be that there is some special undisplayable character in that filename?


»bp
Indeed, I am going to try to find it.
Any progress on this one?


»bp
Planned this weekend.
Hi Bill, I retested it with the following sequence:
Download a file from an external cloud with the following name:
Experiment in the sandbox   Q A [Scif. . . - Tuesday, March 3, 2020 8.06.11 AM
User generated imageand I got the following error:
User generated imageI removed [ and any error is displayed.
I think that I am going to leave like this and put in the comment of the script if error is displayed some files names contains special characters such as ([) or files & folders are hidden. Let me know what do you think.