Opening folders

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Opening folders

12 Jul 2017, 06:38

I have a script to have hotkeys for specific folders, like:

Code: Select all

^!d::
{
	Run C:\Documents\
	WinWait, Documents
	Gosub, Center
	Return
}
where "Center" is:

Code: Select all

Center:
{
	WinGetPos, , , WinW, WinH, A
	WinMove, A, , A_ScreenWidth/2-WinW/2, A_ScreenHeight/2-WinH/2-60
	Return
}
to center it in the screen. It works quite fine, but since I switched to Win10 (from XP) sometime it just seems to freeze: after I opened a folder, that same folder doesn't open anymore (but the rest do), and for it to work properly I have to restart Windows Explorer services and the script itself. I can't replicate it, it just happens sometime.

When it happens, the current windows loses focus after regaining it, so it seems it's trying to open the folder; I think it could depend on WinWait instruction, but I'm not sure: for some reason maybe Run doesn't work, and the script get stuck waiting for the folder; if I change from

Code: Select all

WinWait, Documents
to

Code: Select all

WinWait, Test
I have the same issue.

I also tried to open the folder manually, hoping if the issue was the WinWait it'd be solved, but it didn't, I still had to restart Explorer and script.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Opening folders

12 Jul 2017, 07:29

Probably a timing issue. What about to use WinWaitActive instead of WinWait?
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

12 Jul 2017, 10:27

I'll try that and keep you updated, thanks!
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

13 Jul 2017, 13:16

Unfortunately it's not that, also with WinWaitActive it freezes like before.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Opening folders

13 Jul 2017, 15:12

Try adding a timeout to WinWaitActive, and have the script display an error and return if the timeout triggers.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

16 Jul 2017, 16:54

It did trigger, so it was WinWaitActive, so maybe it does is Run not opening the folder and WinWaitActive looping for that. Ideas? Maybe adding a Return in case of error, but if there were a way to prevent the error altogether would be great.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

19 Aug 2017, 13:45

It keeps happening, unfortunately the timeout doesn't seem to stop the loop.
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Opening folders

19 Aug 2017, 15:51

post your code that is failing. :)
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

19 Aug 2017, 17:05

It's this code:

Code: Select all

^!d::
{
	Run C:\Documents\
	WinWaitActive, Documents,,1
	Gosub, Center
	Return
}
where "center" is:

Code: Select all

Center:
{
	WinGetPos, , , WinW, WinH, A
	WinMove, A, , A_ScreenWidth/2-WinW/2, A_ScreenHeight/2-WinH/2-60
	Return
}
It doesn't happen often, but sometimes it just freeze as described above. The same issue happens with WinWait instead that WinWaitActive.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Opening folders

19 Aug 2017, 17:17

One possible issue is if there is already a window called 'Documents' open.

You could do something like:

Code: Select all

IfWinExist, Documents
	WinActivate, Documents
else
{

}
I would try this for your code so far:

Code: Select all

^!d::
{
	Run, C:\Documents
	WinWait, Documents
	;WinWaitActive, Documents ;you could use this line instead
	Gosub, Center
	return
}

Center:
{
	WinGetPos,,, WinW, WinH, Documents
	WinMove, Documents,, % A_ScreenWidth/2-WinW/2, % A_ScreenHeight/2-WinH/2-60
	return
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

19 Aug 2017, 17:41

jeeswg wrote:One possible issue is if there is already a window called 'Documents' open
That's not the case, the issue happened without windows with the same name being already opened.

Code: Select all

I would try this for your code so far:
[code]
^!d::
{
	Run, C:\Documents
	WinWait, Documents
	;WinWaitActive, Documents ;you could use this line instead
	Gosub, Center
	return
}

Center:
{
	WinGetPos,,, WinW, WinH, Documents
	WinMove, Documents,, % A_ScreenWidth/2-WinW/2, % A_ScreenHeight/2-WinH/2-60
	return
}
return
Using WinWait instead of WinWaitActive doesn't resolve the issue (it was the first version of the code, see opening post); as for Center, I use A instead of Documents because several shortcut calls it (Documents, Work, etc.), so I can't have a fixed name.

I think I'll have to go for an If version; thanks for the suggestions!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Opening folders

19 Aug 2017, 18:10

If Center must be applied to the active window, I would do this:

Code: Select all

^!d::
{
	Run, C:\Documents
	WinWait, Documents ;possibly add the timeout parameter, and some code to handle a timeout
	WinActivate, Documents
	;WinWaitActive, Documents
	Gosub, Center
	return
}

Center:
{
	WinGetPos,,, WinW, WinH, A
	WinMove, A,, % A_ScreenWidth/2-WinW/2, % A_ScreenHeight/2-WinH/2-60
	return
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

20 Aug 2017, 02:38

Will try that, thanks; but the issue being the folder (Documents in this example) don't even open and WinWait timeout, would WinActivate do anything? I was thinking about using something like if there is an error, return, if nothing else works.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

22 Aug 2017, 04:27

I was thinking about doing another subroutine before center, that checks ErrorLevel and stops WinWaitActive if there is an error; any idea how to do this? Using Return in the subroutine stops the subroutine itself, not sure how to say to stop WinWaitActive.
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: Opening folders

22 Aug 2017, 07:43

You can use the timeout feature of WinWaitActive:
WinWaitActive, Documents, , 2
This waits only 1 second to activate the window, when the window is still not active ErrorLevel is set to 1. Following the command with a ‘If ErrorLevel’ line allows to take action when something is wrong.

Code: Select all

WinWaitActive, Documents, , 2	; Wait 2 seconds for the window to activate.
If ErrorLevel
{
	; Something to do on error.
}
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

22 Aug 2017, 07:58

Yup, my idea was to put the If ErrorLevel part in a subroutine, since I would have to use it with the same effect more than once and it'd be simple to define one subroutine and call it, rather then having If ErrorLevel each time; it's possible to do so?
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: Opening folders

22 Aug 2017, 08:17

Like

Code: Select all

; ...
WinWaitActive, Documents, , 2	; Wait 2 seconds for the window to activate.
GoSub, CheckErrorLevel

Return

CheckErrorLevel:
If ErrorLevel
{
	; Something to do on error.
} Else {
	; Something to do on succes.
}
Return
?
Yes, that is possible.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: Opening folders

22 Aug 2017, 08:25

If I want it to just stop WinWaitActive what should I do? I think something like this:

Code: Select all

CheckErrorLevel:
If ErrorLevel
{
	Return
}
Return
would just return from the subroutine to the main script, but would it stop the loop?

(I'm trying to see if the subroutine works, but since I can't reproduce the glitch I have to wait for it to happens.)
User avatar
DyaTactic
Posts: 221
Joined: 04 Apr 2017, 05:52

Re: Opening folders

22 Aug 2017, 08:29

To reproduce the glitch you can change toe WinTitle form Documents to ‘Nonexisitingswindowsorsomethinglikethis’.
You last post will only return to the lines under the ‘GoSub, CheckErrorLevel’ command.
What you want is accomplished with just

Code: Select all

WinWaitActive, , , 2
If ErrorLevel
	Return
Guest

Re: Opening folders

22 Aug 2017, 09:48

It doesn't seem to work, no idea why; this is the code:

Code: Select all

^!d::
{
	Run C:\Documents\
	WinWaitActive, F,,1
	Gosub, CheckErrorLevel
	Gosub, Center
	Return
}

Center:
{
	WinGetPos, , , WinW, WinH, A
	WinMove, A, , A_ScreenWidth/2-WinW/2, A_ScreenHeight/2-WinH/2-60
	Return
}

CheckErrorLevel:
{
	If ErrorLevel
		MsgBox, Text
	Return
}
It opens the folder without showing the textbox.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, DataLife, Google [Bot], Rohwedder and 175 guests