Open Folder and Close if already open

Get help with using AutoHotkey and its commands and hotkeys
ShYbeR
Posts: 8
Joined: 08 Jul 2019, 09:35

Open Folder and Close if already open

16 Jul 2019, 16:23

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
scriptor2016
Posts: 515
Joined: 21 Dec 2015, 02:34

Re: Open Folder and Close if already open

17 Jul 2019, 00:14

Code: Select all

Window=X3 Attachement

^f10::
IfWinExist, %Window%
Winclose, %Window%
else
run, C:\Users\XXX\Desktop\%Window%
Return
ShYbeR
Posts: 8
Joined: 08 Jul 2019, 09:35

Re: Open Folder and Close if already open

17 Jul 2019, 09:11

thank you very much! it worked like a charm!
ShYbeR
Posts: 8
Joined: 08 Jul 2019, 09:35

Re: Open Folder and Close if already open

17 Jul 2019, 12:02

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
hd0202
Posts: 140
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Open Folder and Close if already open

17 Jul 2019, 22:40

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
tmplinshi
Posts: 1519
Joined: 01 Oct 2013, 14:57

Re: Open Folder and Close if already open

18 Jul 2019, 02:11

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.
Last edited by tmplinshi on 20 Jul 2019, 08:46, edited 2 times in total.
User avatar
jeeswg
Posts: 6816
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Open Folder and Close if already open

18 Jul 2019, 04:15

@tmplinshi: One thing to consider is this. Cheers.
RTrim(window.Document.Folder.Self.Path, "\")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
tmplinshi
Posts: 1519
Joined: 01 Oct 2013, 14:57

Re: Open Folder and Close if already open

18 Jul 2019, 04:26

Oh, nice. I didn't know that. Thanks jeeswg!
teadrinker
Posts: 1018
Joined: 29 Mar 2015, 09:41
Contact:

Re: Open Folder and Close if already open

18 Jul 2019, 04:30

@jeeswg
Is RTrim really needed?

Code: Select all

for window in ComObjCreate("Shell.Application").Windows
   MsgBox, % window.Document.Folder.Self.Path
User avatar
jeeswg
Posts: 6816
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Open Folder and Close if already open

18 Jul 2019, 04:36

For most paths, there is no trailing backslash.
An exception is the root of the drive e.g. C:\.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ShYbeR
Posts: 8
Joined: 08 Jul 2019, 09:35

Re: Open Folder and Close if already open

18 Jul 2019, 08:14

thank you very much everyone! :dance:

Return to “Ask For Help”

Who is online

Users browsing this forum: aifritz, Google [Bot], TAC109 and 179 guests