Simple File Explorer Fix

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Era
Posts: 25
Joined: 28 Apr 2018, 22:36

Simple File Explorer Fix

13 May 2018, 01:17

Here's my solution to a problem that's been plaguing me for years, but is there something better? (Purer, more programmatic code than that found in my newbie script below?)

When I open the File Explorer window, the scrollbar is always halfway down so you can't even see the (so called) Quick Access zone. You have to scroll up to reveal what should by default be the most visible zone. Anyway, this script solves it. (IDK if this issue is peculiar only to Windows 10, or indeed only to my computer.)

Code: Select all

; This ensures that when you open File Explorer, the Quick Access area shows so you don't have to scroll up to it.
; This has no hotkey because I put the code into my AHK startup script so it's always in the background. Maybe a bad idea?

#If WinActive("ahk_class CabinetWClass") 

; click in left side of screen, then scroll up to show the Quick Access items
	Click, 48, 55
	Loop 5
		Click, WheelUp
return
#If 
swagfag
Posts: 2491
Joined: 11 Jan 2017, 17:59

Re: Simple File Explorer Fix

13 May 2018, 03:36

u can have a script track whenever a new windows explorer is opened and send Home to the tree view area, which should bring the scrollbar all the way up to the top

Code: Select all

#NoEnv
#Persistent
#SingleInstance Force
SendMode Input
SetBatchLines -1
SetTitleMatchMode 2

fn := Func("focusQuickAccess")
SetTimer, % fn, 50

focusQuickAccess() {
	static Explorers := []

	if (activeHwnd := WinActive("ahk_class CabinetWClass"))
	{
		for each, hwnd in Explorers
		{
			if (activeHwnd == hwnd)
				return
		}
	}

	Explorers.push(activeHwnd)
	ControlSend, SysTreeView321, {Home}, % "ahk_id" . activeHwnd 
}
User avatar
Era
Posts: 25
Joined: 28 Apr 2018, 22:36

Re: Simple File Explorer Fix

13 May 2018, 07:27

Thank you. This seems more generalized and certainly more sophisticated than my version. I can't test it though because my version is running (it's in my ahk startup file) and I don't yet know how to selectively turn off running scripts. Also, I don't fully understand how your code works, but I'll study it.

I do have a somewhat unrelated question, though, and this is the ask-for-help section. I've noticed that lots of AHK scripts begin with certain directives like #SingleInstance Force and SetTitleMatchMode 2. So wouldn't it be helpful if a few of these nearly universal commands were just baked into AHK itself? They would just be assumed behavior of all scripts (the way ! stands for ALT), thus not required to be specified in each new script? And if for some reason you do need to turn the behavior off, you could then type it in (such as -#SingleInstance Force, or SetTitleMatchMode 0).

I bring this up because the same (to me difficult to understand) repetition of assumed startup conditions appears as well in other languages. For example, you are expected to specify version 1 in any XML document. But there is no version 2. Hasn't been for decades. Likely never will be. (The language expands via an accretion of plugins.)

I think the question marks were put in there because others beside myself wonder why the version number must be mentioned at all if there is only one version:
<?xml version="1.0"?>

It's like the post office requiring that you end every address with Earth, on the theory that some day there might be interplanetary delivery.
gregster
Posts: 2397
Joined: 30 Sep 2013, 06:48

Re: Simple File Explorer Fix

13 May 2018, 10:59

First of all, Era, I don't see how your snippet from the first post could have the effect that you expect - I also never encountered the problem, neither on Win10 nor elsewhere. But anyway, let's say the problem exists (on your system) - your code cannot fix it, I am sure.

#If directives only affect hotkeys and hotstrings and make them context-sensitive. If you put normal code between them, it will be run once (if it is part of an auto-execute section or a subroutine) or multiple times (if part of a SetTimer, subroutine or loop etc.). But the #if directives will have no effect at all on this.

It looks, by the code provided by you, that theClick... code is only executed (at max) once when you start your script - ignoring the #If directives altogether which are not relevant here, since there is no hotkey or hotstring between them. How can this achieve the effect you want (every time sending the click, when a file explorer is active, I assume)?!?
swagfag on the other hand, uses a timer to constantly check, every 50 milliseconds, if a File Explorer window is active.

If you don't believe me, run this script (at first, with no explorer open):

Code: Select all

#persistent
#If WinActive("ahk_class CabinetWClass") 
msgbox Explorer really active ???
return
#If 
It will show the msgbox, even if there is no file explorer window. Because I made it #persistent, it is still running. Now open an explorer window and... :wave: nothing happens, no msgbox shows.


Secondly, I think this is a misperception:
I've noticed that lots of AHK scripts begin with certain directives like #SingleInstance Force and SetTitleMatchMode 2. So wouldn't it be helpful if a few of these nearly universal commands were just baked into AHK itself? They would just be assumed behavior of all scripts (the way ! stands for ALT), thus not required to be specified in each new script? And if for some reason you do need to turn the behavior off, you could then type it in (such as -#SingleInstance Force, or SetTitleMatchMode 0).
You don't need any of these directives or commands (which are setting options) to code a perfectly working AHK script. These are still only options. All these directives like #SingleInstance or commands like SetTitleMatchMode have a default value or behaviour that is described in the docs - if you are fine with the default, no need to change or even mention it in the script.
For example, for SetTitleMatchMode you can find the following in the docs: "If unspecified, TitleMatchMode defaults to 1 and fast." 1 is listed as: "A window's title must start with the specified WinTitle to be a match."
And for #SingleInstance:
A script containing hotkeys, hotstrings, #Persistent, OnMessage(), or Gui is single-instance (dialog & prompt) by default. Other scripts default to allowing multiple instances. [...]
This directive is ignored when any of the following command line switches are used: /force /f /restart /r
Of course, there are a lot of options (that's how AHK can easily do lots of things) - but there are always default options (most make sense, changing them would break older scripts) - I have several scripts running where I don't specify any directive. In short, this behaviour that you wish, already exists. The default options might not be your preferred ones, though, but there... we can't help ;)
User avatar
Era
Posts: 25
Joined: 28 Apr 2018, 22:36

Re: Simple File Explorer Fix

13 May 2018, 14:51

Thank you, gregster. I need to figure out #persistent.

swagfag's use of:

SetTimer, % fn, 50

makes me wonder how much of an impact running timers like this has on the CPU usage.

Thanks for the info on the directives/commands. I think I need to calm down and do more reading about how AHK works. :?
User avatar
jeeswg
Posts: 6376
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simple File Explorer Fix

24 May 2018, 18:59

Here's a script which plays a sound every time an Explorer window is activated.

Code: Select all

#Persistent
;plays a sound every time an Explorer window is activated
OnExplorerActive(hWinEventHook, vEvent, hWnd)
{
	;EVENT_SYSTEM_FOREGROUND := 0x3
	static _ := DllCall("user32\SetWinEventHook", UInt,0x3, UInt,0x3, Ptr,0, Ptr,RegisterCallback("OnExplorerActive"), UInt,0, UInt,0, UInt,0, Ptr)
	DetectHiddenWindows, On
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass")
	|| (vWinClass = "ExploreWClass")
		SoundPlay, *64
	;else
	;	SoundPlay, *48
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Era
Posts: 25
Joined: 28 Apr 2018, 22:36

Re: Simple File Explorer Fix

25 May 2018, 08:15

Thanks for this. I'm going to save it to use in other scripts, after I figure it out!
Something strange on my machine: had to add a Wait command to the SoundPlay to hear anything...
User avatar
jeeswg
Posts: 6376
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simple File Explorer Fix

25 May 2018, 08:32

- This slightly altered script should be easier to understand:

Code: Select all

#Persistent
;EVENT_SYSTEM_FOREGROUND := 0x3
DllCall("user32\SetWinEventHook", UInt,0x3, UInt,0x3, Ptr,0, Ptr,RegisterCallback("OnExplorerActive"), UInt,0, UInt,0, UInt,0, Ptr)

;plays a sound every time an Explorer window is activated
OnExplorerActive(hWinEventHook, vEvent, hWnd)
{
	;EVENT_SYSTEM_FOREGROUND := 0x3
	;static _ := DllCall("user32\SetWinEventHook", UInt,0x3, UInt,0x3, Ptr,0, Ptr,RegisterCallback("OnExplorerActive"), UInt,0, UInt,0, UInt,0, Ptr)
	DetectHiddenWindows, On
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass")
	|| (vWinClass = "ExploreWClass")
		SoundPlay, *64
	;else
	;	SoundPlay, *48
}
- SetWinEventHook checks for event numbers in the range specified (I specified from 3 to 3), and if such an event is detected, the callback function is called.
- You either need to run SetWinEventHook somewhere in the script, as in my 2nd version, or, as in the 1st version, all *static* lines are executed once and only once, when the script starts.
- So the 1st version has the advantage that SetWinEventHook is called without having to place a line outside of the function.
- Does SoundBeep work? One potential issue is that the function can be called repeatedly and quickly. SoundPlay worked fine for me on Windows 7.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Era
Posts: 25
Joined: 28 Apr 2018, 22:36

Re: Simple File Explorer Fix

25 May 2018, 09:57

Thanks again for this explanation. Yes, soundbeep works. I have a souped-up Alienware computer so maybe it's just flying past the audio. When I add Wait, there's a brief, quiet, high-frequency bip, nothing else.

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 192 guests