StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
soooulp  
#1 Posted : Thursday, February 10, 2022 7:22:27 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
No matter whether I use the TidyTabs that a program to group the same program such as the explorer or notepad in one window like Chrome tabs, it will auto change to the next one when I active the window that is minimized.

I don't know whether it is a case for me. I use the following script to keep the window as before minimized when active by an action.

And there is no special name for the explorer to recognize except ClassName, but the window that gets from ClassName seems not in an array.

Code:
var hCMD = sp.WindowFromClassOrTitle('CabinetWClass', '');

var wnds = sp.AllApplications();
var apps = new Array();
for(var i = 0, len = wnds.Length; i < len; i++) {
    if(wnds[i].ClassName == "CabinetWClass") {
        apps.push(wnds[i]);
    }
}

if (hCMD !== null){
    var num = apps.length - 1;
    sp.WindowsFromTitlePartial(apps[num].Title).First().Activate();;
}else{
    sp.SendModifiedVKeys([vk.LWIN], [vk.VK_E]);   
}


Code:
var wnd = sp.WindowsFromTitlePartial("- notepad");
if(wnd.Count() === 0) {
    sp.RunOrActivate('notepad.exe');
} else {
    wnd[wnd.Length-1].Activate();
    wnd[wnd.Length-1].BringToFront();
}


Rob  
#2 Posted : Thursday, February 10, 2022 8:40:08 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Yeah, WindowFromClassOrTitle is simply a wrapper for FindWindowA - which only returns the first.

You're right that you need to enumerate all windows.

WindowsFromTitlePartial just enumerates all windows and does regex on each title - so there's really no difference in S+ doing it or you calling AllApplications and looping through.
thanks 1 user thanked Rob for this useful post.
soooulp on 2/10/2022(UTC)
soooulp  
#3 Posted : Thursday, February 10, 2022 8:49:25 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
Thank you Rob for your explanation.

The strange is that the active Window is not the one before minimized and I don’t find it these years.
Rob  
#4 Posted : Thursday, February 10, 2022 9:07:54 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Do you have Always Activate Window Where Gesture Began checked under Options > General?

That will activate the window below the mouse before your script executes.
soooulp  
#5 Posted : Thursday, February 10, 2022 10:06:46 PM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
It shows chenked.

It is more and more strange, and I set Three Hotkeys to active Chrome, Explorer, and Notepad.

If I minimize the Explorer or Notepad, use the normal wnd.First().Activate() will show the next Window of that, but it is normal if I don't minimize the Window, just active Chrome and then active that Window directly.

So if I use the script I post to change the .length-1 index Window, the first is normal, but the second will show the previous Window. HAHAHAHA.

Never mind it.
Rob  
#6 Posted : Saturday, February 12, 2022 3:11:54 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
So AllApplications and WindowsFromTitlePartial both use the EnumWindows API call.

Generally speaking, the order in which the window handles are returned is in Z-order, starting from the top-most (usually the active) window down the the bottom-most (oldest app that has been activated).
See this post for some in-depth discussions on the topic.

Meaning, the order of the array will change as different windows are activated, constantly moving the active window to the top [0] and the last application (alt+tab) becoming [1], then downward after that.

Hopefully this helps to understand why you could be having unexpected or confusing results!

This script below is a refactored version of yours, simplifying the logic and reducing the calls to EnumWindows.
Code:
var wnds = sp.AllApplications()
             .Where(x => x.ClassName == "CabinetWClass")
             .ToList();

// Just to debug, so you can see current window order of Explorer windows
for(var i = 0, len = wnds.Count; i < len; i++) {
    StrokesPlus.Console.Log(`${wnds[i].ClassName} - ${wnds[i].Title}`);
}

if(wnds.Count > 0) {
    wnds.First().Activate();  // Can also use Last: wnds.Last().Activate();
} else {
    sp.SendModifiedVKeys([vk.LWIN], [vk.VK_E]); 
}

Edited by user Saturday, February 12, 2022 3:17:31 AM(UTC)  | Reason: Not specified

thanks 1 user thanked Rob for this useful post.
soooulp on 2/12/2022(UTC)
soooulp  
#7 Posted : Saturday, February 12, 2022 3:53:49 AM(UTC)
soooulp

Rank: Advanced Member

Reputation:

Groups: Moderators, Approved
Joined: 4/23/2020(UTC)
Posts: 161
China

Thanks: 46 times
Was thanked: 23 time(s) in 17 post(s)
After many attempts, I find an effective way is to get the Title of the Explorer Window before minimizing it, then activate it by the stored Title.

Also, thank you for your advanced code.

I get the Title in the minimize script.
Code:
sp.StoreString('title', sp.WindowFromPoint(sp.GetCurrentMousePoint(), true).Title);



Code:
var hCMD = sp.WindowFromClassOrTitle('CabinetWClass', '');
if (hCMD !== null){
    if(hCMD.Minimized) {
        sp.WindowsFromTitlePartial(sp.GetStoredString('title')).First().Activate();
    } else {
        hCMD.Activate();
        hCMD.BringToFront();
    }
}else{
    sp.SendModifiedVKeys([vk.LWIN], [vk.VK_E]);
}






Rob  
#8 Posted : Saturday, February 12, 2022 4:31:55 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
You can also just store the actual window object itself, so you don't have to try to find it again by title - it will be a direct reference to that window.
Code:
// In minimize
sp.StoreObject("minWindow", sp.WindowFromPoint(sp.GetCurrentMousePoint(), true));

// Later code
sp.GetStoredObject("minWindow").Activate();

Not really much different, but just so you're aware :)
thanks 1 user thanked Rob for this useful post.
soooulp on 2/12/2022(UTC)
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.