Simplest/quickest way to obtain the directory or full filename path of the active window

Hello,

What is the simplest way to obtain the directory or full filename path of the active window?

What I am after does not necessarily need to include the actual filename:

        C:\Users\Username\Documents\FolderName\Filename.***

(where *** represents any extension), but only the directory:

        C:\Users\Username\Documents\FolderName

I have always acquired it by this route:

        File > Save as > Alt+d > Ctrl+c

and even shorter than that — at least in MS Office windows — is to use the shortcut {F12}:

        {F12} > Alt+d > Ctrl+c

However, are there any other ways to capture the currently active window's full directory — particularly that do not necessitate opening a second, albeit transient, window (eg Windows Explorer) as is the case above?

Thanks
WeThotUWasAToadAsked:
Who is Participating?
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Hi WeThotUWasAToad,

I know you're a fan of AutoHotkey, so here's a simple script that does it for Excel, PowerPoint, and Word:

!^e::
ActiveFileFullName:=ComObjActive("Excel.Application").ActiveWorkbook.FullName
MsgBox,4096,Full File Name,%ActiveFileFullName%
Return

!^p::
ActiveFileFullName:=ComObjActive("PowerPoint.Application").ActivePresentation.FullName
MsgBox,4096,Full File Name,%ActiveFileFullName%
Return

!^w::
ActiveFileFullName:=ComObjActive("Word.Application").ActiveDocument.FullName
MsgBox,4096,Full File Name,%ActiveFileFullName%
Return

Open in new window


I assigned them above to Alt-Ctrl-e, Alt-Ctrl-p, and Alt-Ctrl-w but, of course, you may assign each one to whatever hotkey you want. Add them to your current AHK hotkeys script and then a single key click will do it for you. I tested it here in Excel 2013, PowerPoint 2013, and Word 2013 — works perfectly. But I did not test in prior Office versions.

The code above simply displays the file name in a MsgBox dialog, but since you have the value in a variable (ActiveFileFullName) you may do anything you want with it, such as write it to a text file with AHK's FileAppend command or put it on the clipboard (via Clipboard:=ActiveFileFullName).

Update: I just realized that you said, "does not necessarily need to include the actual filename". So if you do want the file name, the above code does it. If you don't want the file name, then change FullName to Path in the code, as follows:

!^e::
ActiveFilePath:=ComObjActive("Excel.Application").ActiveWorkbook.Path
MsgBox,4096,Path Name,%ActiveFilePath%
Return

!^p::
ActiveFilePath:=ComObjActive("PowerPoint.Application").ActivePresentation.Path
MsgBox,4096,Path Name,%ActiveFilePath%
Return

!^w::
ActiveFilePath:=ComObjActive("Word.Application").ActiveDocument.Path
MsgBox,4096,Path Name,%ActiveFilePath%
Return

Open in new window


Once again, I tested the code here in Excel 2013, PowerPoint 2013, and Word 2013, but not earlier Office versions. Regards, Joe
0
 
jcimarronCommented:
WeThotUWasAToad --
Right click in Address line of the active window in explorer|"Copy address as text".
0
 
NVITCommented:
For Office, e.g. Word, you can make a macro and a related toolbar icon:

Sub CopyDocPath()
 Dim MyData As DataObject
 Dim strClip As String
 strClip = ActiveDocument.Path

 Set MyData = New DataObject

 MyData.SetText strClip
 MyData.PutInClipboard
 End Sub

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
WeThotUWasAToadAuthor Commented:
Thank you for the responses.

jcimarron
Right click in Address line of the active window in explorer|"Copy address as text".
I believe that is essentially the same as the process I mentioned initially:
I have always acquired it by this route:

        File > Save as > Alt+d > Ctrl+c

and even shorter than that — at least in MS Office windows — is to use the shortcut {F12}:

        {F12} > Alt+d > Ctrl+c
If not, please explain.

NewVillageIT
For Office, e.g. Word, you can make a macro and a related toolbar icon:
Unfortunately I'm pretty much VBA-illiterate so I'm most comfortable with the third solution:

Joe Winograd
…here's a simple script that does it for Excel, PowerPoint, and Word:
Thanks a bunch Joe. This is just about exactly what I wanted.

As you correctly pointed out, I am a fan of AHK but there are parts of your scripts which I don't recognize — particularly in the second line.

I can see that you are defining the arbitrarily-named variable, "ActiveFilePath" as being equal to the expression:

        ComObjActive("Excel.Application").ActiveWorkbook.Path

I looked up ComObjActive but did not get much from the definition even though I also looked up some of its initialisms/acronyms (eg COM, OLE, CLSID).

Also, the remainder of that second line does not appear to be AHK at all. (Is it VBA?) but if you could elaborate, I'd appreciate it.

Also, can you explain the term "Path Name" in the MsgBox line?

Finally, is there any possibility of combining the scripts under a single hotkey? For example, since Windows can have only one active file (window) at any given time, is it not possible to retrieve or capture that file's type (by its extension or in some other manner) and then use that information in some type of nested or repeated If/Else commands — the idea of which I can represent most comfortably in a sort-of Excel-like form:

=IF(ActiveFileType="Excel", --> proceed to ScriptA,
IF(ActiveFileType="Word", --> proceed to ScriptB,
IF(ActiveFileType="Text", --> proceed to ScriptC,
…and so on)))

Open in new window


Thanks
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Hi We...Toad,

Microsoft developed a technology called Component Object Model (COM). Here's a starting link to some info about it:
https://www.microsoft.com/com/default.mspx

Here are links about Word and Excel:
https://msdn.microsoft.com/en-us/library/kw65a0we.aspx
https://msdn.microsoft.com/en-us/library/wss56bz7.aspx

It can be used by VB, but it is not restricted to use by VB. Any language can build support for it, as AutoHotkey has. The COM doc for AHK begins here:
http://ahkscript.org/docs/commands/ComObjActive.htm

There are 10 other doc sections under that. Here are some tutorials about COM in AHK:
http://www.autohotkey.com/board/topic/69033-basic-ahk-l-com-tutorial-for-excel/
http://www.autohotkey.com/board/topic/64563-basic-ahk-v11-com-tutorial-for-webpages/
http://www.autohotkey.com/board/topic/47052-basic-webpage-controls-with-javascript-com-tutorial/

The remainder of the second line is AHK in the sense that it is a call to AHK's defined implementation of COM. It is not VB or VBA.

Path Name is simply the title of the MsgBox window (it will appear in the Title bar). The syntax of the MsgBox statement in AHK is this:

MsgBox,Options,Title,Text,Timeout

I used 4096 for the Options, which means (i) just an OK button and (ii) always on top. I used Path Name for the Title (could be any text). I used the contents of the variable ActiveFilePath for the Text (could be any text). I didn't specify the Timeout. Full doc on the MsgBox statement is here:
http://ahkscript.org/docs/commands/MsgBox.htm

> is there any possibility of combining the scripts under a single hotkey?

Yes, but it's a little tricky. The thing is, the code is not actually going after the active/focus window from the OS perspective. It is going after what Excel/PowerPoint/Word thinks is its active file. Try it — you'll see. Make some other window the active/focus window and the hotkey will still show what the active Excel/PowerPoint/Word file is — all three, in fact, by hitting the three hotkeys one after the other.

But if you want to change it so that a single hotkey will show the results for the active/focus window from the OS perspective, you could use AHK code (for a single hotkey) to determine if the active/focus window is Excel or Word or PowerPoint (perhaps via WinGetActiveTitle), and then execute the corresponding COM call (for Excel or PowerPoint or Word). If you'd like, I'll be happy to write that code for you. Regards, Joe

Update #1: Although I didn't hear back from you, I went ahead with writing the code for displaying the Path of the Windows active/focus window, and assigning it to a single hotkey (I made it F12 — change it to whatever you prefer):

F12::
AppNameMaxLen:=13
WinGetActiveTitle,ActiveWindowTitle
TitleLen:=StrLen(ActiveWindowTitle)
StartPos:=TitleLen-AppNameMaxLen+1
If (StartPos<1)
  StartPos:=1
AppName:=SubStr(ActiveWindowTitle,StartPos,AppNameMaxLen)
WinActivate % "ahk_id " DllCall("GetDesktopWindow","ptr")
If (InStr(AppName," - Excel",True))
{
  ActiveFilePath:=ComObjActive("Excel.Application").ActiveWorkbook.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
If (InStr(AppName," - PowerPoint",True))
{
  ActiveFilePath:=ComObjActive("PowerPoint.Application").ActivePresentation.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
If (InStr(AppName," - Word",True))
{
  ActiveFilePath:=ComObjActive("Word.Application").ActiveDocument.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
MsgBox,4096,Path Name,Active window is not Excel/PowerPoint/Word
Return

Open in new window


This code looks for " - Excel" or " - PowerPoint" or " - Word" (without the quotes, but with the spaces) on the right side (not left side) of the active/focus window Title bar and executes the appropriate COM call. Note that this code works only with Office 2013 — it does not work with 2003, 2007, or 2010. But the only reason that it doesn't work is because of the Title bar string searches for " - Excel" and " - PowerPoint" and " - Word". The earlier versions put different strings in the Title bar. But that would be easy to fix if needed. The much more important point — critical, in fact — is the COM calls, and I tested them in Office 2003, 2007, and 2010. The COM calls in AHK for Excel, PowerPoint, and Word work in all three of those Office releases!

If you want the full file name instead of just the path, change .Path in the COM calls to .FullName (and change other code if you'd like, such as the Title in the MsgBox dialogs and/or the variable names).

As I mentioned earlier, you may replace the MsgBox statement (or add to it) with whatever code you want, such as doing a FileAppend or copying the value to the clipboard. Regards, Joe

Update #2: I did some research for a more robust solution than the approach above that looks for " - Excel", " - PowerPoint", " - Word" in the Active Title. Turns out that the code in the Examples section of AHK's ComObjGet documentation is exactly what's needed. My new script incorporates that sample code to retrieve the command line that was used to launch the active window's process. For Excel, it will contain excel.exe; for PowerPoint, powerpnt.exe; for Word, winword.exe. Looking for those strings (case insensitive) in the command line launch works for Office 2003, 2007, 2010, and 2013. Here's the new code:

F12::
WinGet pid,PID,A
wmi:=ComObjGet("winmgmts:")
queryEnum:=wmi.ExecQuery("" . "Select * from Win32_Process where ProcessId=" . pid) ._NewEnum()
If queryEnum[process]
  CommandLine:=process.CommandLine
Else
{
  MsgBox,4096,Path Name,Process not found
  Return
}
wmi:=queryEnum:=process:=""
WinActivate % "ahk_id " DllCall("GetDesktopWindow","ptr")
If (InStr(CommandLine,"excel.exe",False))
{
  ActiveFilePath:=ComObjActive("Excel.Application").ActiveWorkbook.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
If (InStr(CommandLine,"powerpnt.exe",False))
{
  ActiveFilePath:=ComObjActive("PowerPoint.Application").ActivePresentation.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
If (InStr(CommandLine,"winword.exe",False))
{
  ActiveFilePath:=ComObjActive("Word.Application").ActiveDocument.Path
  MsgBox,4096,Path Name,%ActiveFilePath%
  Return
}
MsgBox,4096,Launch Command Line,Active window is not Excel/PowerPoint/Word - launch command line is:`n%CommandLine%
Return

Open in new window


Regards, Joe

Update #3: I modified the last line of the script to display the command line that launched the program when it was not Excel, PowerPoint, or Word. In my personal use of this script I am finding that to be very helpful. Please let me know what you think. Regards, Joe

Update #4: Added a line of code (WinActivate) to register the Office app in the ROT.

Update #5: I don't know if it's useful for you, but if you want to see the command line launch string for Excel/PowerPoint/Word, append this to the end of the three MsgBox statements:

`n%CommandLine%

This is the same as what the script already has when the Active window is not Excel/PowerPoint/Word.
0
 
WeThotUWasAToadAuthor Commented:
Great responses! Thanks
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
You're welcome, Steve. Always happy to help. And thanks to you for the kind words. Regards, Joe
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.