We help IT Professionals succeed at work.

AutoHotkey: open containing folder

High Priority
96 Views
Last Modified: 2020-02-20
Hello experts,

The following AutoHotkey allows me to copy files path to clipboard and do the same for notepad++ by using send input.

;====================================
;Windows Explorer: copy full path of active Excel, PowerPoint, or Word file to the clipboard. 
;====================================

+F8::
WinGet ActiveWindowPID,PID,A ; save active window PID
WinActivate % "ahk_id " DllCall("GetDesktopWindow","ptr") ; register Office app in ROT - thanks to lexikos for this line of code
WinActivate,ahk_pid %ActiveWindowPID% ; after registering Office app in ROT, reactivate Office app
If (WinActive("ahk_exe excel.exe"))
  Clipboard:=ComObjActive("Excel.Application").ActiveWorkbook.FullName
Else
If (WinActive("ahk_exe powerpnt.exe"))
  Clipboard:=ComObjActive("PowerPoint.Application").ActivePresentation.FullName
Else
If (WinActive("ahk_exe winword.exe"))
  Clipboard:=ComObjActive("Word.Application").ActiveDocument.FullName
Else
If (WinActive("ahk_exe notepad++.exe"))
  Sendinput ^+p
Else
  Clipboard:="Active window is not Excel, PowerPoint, Word or Notepad++"
Return

Open in new window


I would like to take as a reference to open containing folder:
20191223_103502-open-file-location.png
Defined shortcut on notepad to open containing folder is: ^!0

If you have questions, please contact me.
Thank you for your help.
Comment
Watch Question

Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
Hi Luis,
Here's AutoHotkey code that does what you want, i.e., leveraging the code that you posted, it opens the containing folder of the active Excel, PowerPoint, Word, or Notepad++ file via the Ctrl+Alt+0 hotkey:

;====================================
;Open containing folder of active Excel, PowerPoint, Word or Notepad++ file
;====================================

^!0::
WinGet ActiveWindowPID,PID,A ; save active window PID
WinActivate % "ahk_id " DllCall("GetDesktopWindow","ptr") ; register Office app in ROT - thanks to lexikos for this line of code
WinActivate,ahk_pid %ActiveWindowPID% ; after registering Office app in ROT, reactivate Office app
If (WinActive("ahk_exe excel.exe"))
  Clipboard:=ComObjActive("Excel.Application").ActiveWorkbook.FullName
Else
If (WinActive("ahk_exe powerpnt.exe"))
  Clipboard:=ComObjActive("PowerPoint.Application").ActivePresentation.FullName
Else
If (WinActive("ahk_exe winword.exe"))
  Clipboard:=ComObjActive("Word.Application").ActiveDocument.FullName
Else
If (WinActive("ahk_exe notepad++.exe"))
  SendInput ^+p
Else
{
  MsgBox,4144,Error,Active window is not Excel, PowerPoint, Word or Notepad++
  Return
}
ClipWait,1 ; wait for file name to appear on clipboard
If (ErrorLevel=1)
{
  MsgBox,4144,Error,File name did not appear on clipboard after one second
  Return
}
FullName:=Clipboard ; get full file name from clipboard
SplitPath,FullName,,Folder ; get containing folder from full file name
Run,explorer.exe %Folder% ; open containing folder with Windows/File Explorer
Return

Open in new window

Tested here on W7 with Excel 365, PowerPoint 365, Word 365, and NPP v7.8.2 — worked perfectly! Regards, Joe
Luis DiazIT consultant

Author

Commented:
Thank you Joe. I will test it and keep you informed.
Luis DiazIT consultant

Author

Commented:
Joe, I tested and it works!
Just one question, if I want to take as a reference your improved version with error levels to copy file path to clipboard. Can I proceed like this?
;====================================
;Copy full path of Excel, PowerPoint, Word or Notepad++ active file
;====================================

+F8::
WinGet ActiveWindowPID,PID,A ; save active window PID
WinActivate % "ahk_id " DllCall("GetDesktopWindow","ptr") ; register Office app in ROT - thanks to lexikos for this line of code
WinActivate,ahk_pid %ActiveWindowPID% ; after registering Office app in ROT, reactivate Office app
If (WinActive("ahk_exe excel.exe"))
  Clipboard:=ComObjActive("Excel.Application").ActiveWorkbook.FullName
Else
If (WinActive("ahk_exe powerpnt.exe"))
  Clipboard:=ComObjActive("PowerPoint.Application").ActivePresentation.FullName
Else
If (WinActive("ahk_exe winword.exe"))
  Clipboard:=ComObjActive("Word.Application").ActiveDocument.FullName
Else
If (WinActive("ahk_exe notepad++.exe"))
 Sendinput ^+p
Else
{
  MsgBox,4144,Error,Active window is not Excel, PowerPoint, Word or Notepad++
  Return
}
ClipWait,1 ; wait for file name to appear on clipboard
If (ErrorLevel=1)
{
  MsgBox,4144,Error,File name did not appear on clipboard after one second
  Return
}
FullName:=Clipboard ; get full file name from clipboard
Return

Open in new window

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
> Can I proceed like this?

Yes, although line 31 (FullName:=Clipboard) is not needed...simply delete it from the AutoHotkey code that you posted. I hope you understand why that can be deleted...it is because the purpose of the Shift+F8 hotkey is to put a value on the clipboard, which the code before line 31 does. The purpose of line 31 in the Ctrl+Alt+0 hotkey is to assign a value to the FullName variable so that it can be parsed by SplitPath to extract the folder for use with Windows/File Explorer. That is not needed in the Shift+F8 hotkey. It doesn't hurt to be there, but it doesn't help...the very next line is Return. Regards, Joe
Luis DiazIT consultant

Author

Commented:
Thank you Joe.
Yes I understood the line 31 proposal FullName:=Clipboard. You putted as variable FullName to get the folder location of active file through SplitPath and then use explorer.exe feature to open file location. I forgot to remove it from the proposal.
Thank you again for your help and have a great day!
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Commented:
> Yes I understood the line 31 proposal

Excellent!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.