Page 1 of 1

Open Folder and Close if already open

Posted: 16 Jul 2019, 16:23
by ShYbeR
Hello I am trying to make a script to open a specific folder and if it is already opened to close it.

Right now I have a script only to open the folder:

^F10::
Run C:\Users\XXX\Desktop\X3 Attachement
Return

Re: Open Folder and Close if already open

Posted: 17 Jul 2019, 00:14
by scriptor2016

Code: Select all

Window=X3 Attachement

^f10::
IfWinExist, %Window%
Winclose, %Window%
else
run, C:\Users\XXX\Desktop\%Window%
Return

Re: Open Folder and Close if already open

Posted: 17 Jul 2019, 09:11
by ShYbeR
thank you very much! it worked like a charm!

Re: Open Folder and Close if already open

Posted: 17 Jul 2019, 12:02
by ShYbeR
So I tried adding onto the script by having it do the same for a different folder and it seem to work the first time but after that it only opens and closes the Estimate folder:

Code: Select all

Window=X3 Attachement

^f10::
IfWinExist, %Window%
Winclose, %Window%
else
run, C:\Users\XXX\Desktop\%Window%
Return

^f9::
Window=Estimates

IfWinExist, %Window%
Winclose, %Window%
else
run, C:\Users\XXX\Desktop\%Window%
Return

Re: Open Folder and Close if already open

Posted: 17 Jul 2019, 22:40
by hd0202
the variable "Window" is set to "X3 Attachment" only at the start of the script (auto-execution section) and not again when you press ^f10. see my change below.

Code: Select all

^f10::
Window=X3 Attachement

IfWinExist, %Window%
Winclose, %Window%
else
run, C:\Users\XXX\Desktop\%Window%
Return
Hubert

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 02:11
by tmplinshi
Potential issues:
  • Different folders path may have the same folder name.
  • The window title could be the folder name or path based on user settings.
A more reliable method is to use ComObjCreate("Shell.Application"):

Code: Select all

^f10::FolderToggleOpen("C:\Users\XXX\Desktop\X3 Attachement")
^f9::FolderToggleOpen("C:\Users\XXX\Desktop\Estimates")

FolderToggleOpen(FolderPath) {
	For Window In ComObjCreate("Shell.Application").Windows {
		If (RTrim(Window.Document.Folder.Self.Path, "\") = RTrim(FolderPath, "\"))
			Return Window.Quit
	}
	Run, % FolderPath
}
Edit: Changed window.LocationURL to window.Document.Folder.Self.Path as jeeswg suggested.
Edit2: Changed WinClose, % "ahk_id " window.hWnd to Window.Quit.

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 04:15
by jeeswg
@tmplinshi: One thing to consider is this. Cheers.
RTrim(window.Document.Folder.Self.Path, "\")

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 04:26
by tmplinshi
Oh, nice. I didn't know that. Thanks jeeswg!

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 04:30
by teadrinker
@jeeswg
Is RTrim really needed?

Code: Select all

for window in ComObjCreate("Shell.Application").Windows
   MsgBox, % window.Document.Folder.Self.Path

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 04:36
by jeeswg
For most paths, there is no trailing backslash.
An exception is the root of the drive e.g. C:\.

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 04:53
by teadrinker
Right, thanks.

Re: Open Folder and Close if already open

Posted: 18 Jul 2019, 08:14
by ShYbeR
thank you very much everyone! :dance: