Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

1 hotkey to activate and minimize windows



  • Please log in to reply
3 replies to this topic
SplitProphet
  • Members
  • 2 posts
  • Last active: Nov 04 2015 02:24 PM
  • Joined: 29 Oct 2015
Hey, I'm pretty new to Autohotkey, so my scripts are by far nothing professional. But let's get to my problem: I didn't want my "Capslock" to work as a key that just lets me write everything in Caps. I'd rather want it to work like a key that if i press it, it first checks if any window is active. If so, it minimizes it/them. If there is no window active (all minimized), it maximizes all windows. If you want to, you can add something, that if it there are for example 5 windows existing (3 of them active) andit minimized these 3, then it only activates the 3 it has minimized (the other 2 stay minimized). I'm fine with just a script that works, but i'd be thankful if you could explain what you worte (if you got the time), so i can understand and learn easier :)

Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Let's start off small by using your first idea. A simple window-minimiziation key.

 

CapsLock::WinMinimize, A

 

CapsLock is our Hotkey. WinMinimize will minimize the specified window - A is specifying the active window (WinTitle); also check this out for ahk_id in the code below.

 

But you also wanted to be able to maximize all windows if they were all minimized. This becomes tricky because 1) We have to detect when all windows are minimized and 2) Maximize them all. There are two interpretations to "Maximize" as well - one is Restore (WinRestore) and one is full-screen maximized (WinMaximize). I will go with WinRestore in this script.

 

CapsLock::
WinGetClass, var, A
If (var="WorkerW" || var="Shell_TrayWnd") ; I get WorkerW if I use the #d shortcut, but ShellTrayWnd if I use this hotkey
{
WinGet, myList, list
Loop %myList%
WinRestore, % "ahk_id " myList%A_Index% ; this is the trickiest step to understand
}
else
WinMinimize, A
return

 

WinGetClass identifies the class of a window. If checks for a condition to be true, in this case, if the class we retrieved is equal to "WorkerW" or to "Shell_TrayWnd". The || is the OR operator. I am using parentheses to treat both sides of the equation as variables to make the || operator work. As a catch to that, I need to pass my WorkerW and Shell_TrayWnd into the expression as literal strings by putting quotation marks around them. (A bit more on that coming up.) A Block is used to encompass the next three lines and make them only execute if that IF statement is true. We will get a list of all windows on the system with WinGet. According to WinGet's documentation, the number of windows that are matched (all of them are matched because we don't give any extra parameters) is stored inside of the variable we specified.

 

This is where it gets tricky. Loop will execute the line (or block; but that's not in this case) underneath of it. By specifying a number of times beside the word Loop, it will do this sequence of commands that many times - no number will make it do so infinitely. We express the value of a variable (or dereference it) by putting % signs around it. This becomes important in the next step. A thing about Loop's is they keep track of how many times they have run in the variable A_Index.

 

Because of the WinGet documentation telling us that the matching windows are put into variables myList1, myList2, myList3 and so on, and we've figured out how many windows there are, it would be nice to activate each of them by calling their names. You cannot do this: %myList%A_Index%% to say you want to evaluate %myList1% on the first loop iteration, then %myList2% on the second. Instead, you change the parameter's syntax from expecting literal strings to expecting variables - this is done by putting a single % at the beginning of the parameter. But like I just said, we're now expecting variables. But we still want to have the literal name ahk_id in there. You can pass literal strings into a parameter that expects variables by putting quotation marks around them. Notice I included a space before my closing quotation mark - without it, AHK would have read the command as WinRestore, ahk_idvalue_of_myListNumber and nothing would've happened as no window has that name. Literal spaces have to be passed within quotation marks too. Anyway, now that we are passing variables, what will happen is myList%A_Index% is dereferenced to myList1 or myList2 or whatever number of the loop we're on. And then the lone % takes effect and evaluates this variable and finds the ID number to use.

 

And if we're not on the desktop, we'll use a normal WinMinimize like before.

 

I hope that made sense.



SplitProphet
  • Members
  • 2 posts
  • Last active: Nov 04 2015 02:24 PM
  • Joined: 29 Oct 2015

Hey, thank you for explaining that much :) And yea I meant "WinRestore" (didn't know that command before).

 

I must say even though there are some things in there that I understand or at least understand partially, the most of the rest (especially behind "Loop") is still a bit (very) to complicated for me - i just began "scripting" like a week ago.

I'm trying to understand though and i guess it's like this how I learn.

 

I have some specific question about the things I just understand partially:

 

What does the A in ... stand for?:

WinGetClass, var, A

 

What does "WorkerW" and "ShellTrayWnd" mean/do?

(I pressed #d on my pc and it minimized all open windows as well as "restored" them)

 

What does the operator || do?

 

Where does the "myList" and "list" in ... come from? Is it built-in?

WinGet, myList, list



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012
✓  Best Answer

What does the A in ... stand for?

 

Check out WinTitle. It retrieves the active window regardless of the window's actual title.

 

What does the operator || do?

 

The || operator is the OR operator.

 

What does "WorkerW" and "ShellTrayWnd" mean/do?

 

WorkerW is the class I get when I use #d to go to my desktop on my windows 8.1 computer and check what Class the active "window" has (even the desktop has one)

Shell_TrayWnd is the class I get when I manually minimize the windows or if I run the script I provided and reach the desktop. I believe Shell_TrayWnd is for the task bar at the bottom, not 100% sure. (And maybe I've mixed up which things are actually class WorkerW and class Shell_TrayWnd).

 


Where does the "myList" and "list" in ... come from? Is it built-in?

 

Check out the WinGet documentation. myList is a custom variable I made up to receive the output from the WinGet command. WinGet has many different things that it can output, and you specify what information you want in the second parameter - I wanted a list so I picked "list" from the possible parameters to put there. You can find what parameters are available in the documentation.