AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Run a script in any Explorer window, but not the Desktop

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 10:20 am    Post subject: Run a script in any Explorer window, but not the Desktop Reply with quote

I have created a couple of basic scripts for some keyboard shortcuts that I'd like to use in the Explorer file manager. One script creates a new folder and the other sorts the contents of the current folder into Name order.

The scripts work fine, but they obviously only make sense if they are used within Explorer, but I am a complete novice and so I haven't managed to figure out what the best way is to restrict my scripts to only being applied to Explorer windows.

For one of my scripts I would also like to exclude the Desktop. This is so that I don't inadvertently press my 'sort by name' hotkey while focussed on the desktop (as this would totally mess up my icon layout).

As my scripts are not very sophisticated (they just mimic the keyboard shortcuts for corresponding feature) I'd need to exclude certain shell Namespace folders from being affected (e.g. My Computer) because the menu structure is different there (It's sort by "Drive" not by "Name") which means my script doesn't work as intended and just leaves a menu hanging on the screen.

Can anyone advise me on the best way to go about this?

Thanks
Back to top
View user's profile Send private message
GodlyMario



Joined: 02 Jul 2008
Posts: 39

PostPosted: Sun Jul 06, 2008 3:06 pm    Post subject: Reply with quote

search manual

#IfWinActive
IfWinNotActive
SetTitleMatchMode

# is global ( if i remember correctly), which will affect all scripts afterward until the same commandline is declared.

and # doesnt exist for ifwinnotactive, so you can use brackets like this
Code:
IfWinNotActive [, WinTitle, WinText, ExcludeTitle, ExcludeText]
{
script of yours
}


one thing to mention is that using IfWinActive the same way as IfWinNotActive in the shown code above , in other word using brackets instead of global, will work too.
Back to top
View user's profile Send private message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 7:24 pm    Post subject: Reply with quote

Thanks for the reply.

I did search the manual and I had already seen IfWinActive / IfWinNotActive, but the method used to determine whether the window is active or not is 'title matching'.

Title matching (as far as I'm aware) will be is impossible to use in Explorer, because the window title could be absolutely anything (an Explorer window title is just whatever the name of the folder happens to be) so there is nothing that is common to all explorer window titles, and there is also no way to know what the window title text will be at any given moment.

That's why I was looking for another way to identify whether something is an Explorer window or not (by process, or executable, or something).

Also, the Desktop does not have a title (the Desktop folder in the Explorer tree does, but I don't know whether applying a rule to that Desktop folder would mean that I'd get the desired results on the actual Desktop itself).

For special shell folder exclusion (e.g. My Computer), I'd rather use something like namespace GUID (if that's possible) to be sure that the script function only applies to the correct window.
Back to top
View user's profile Send private message
Z Gecko
Guest





PostPosted: Sun Jul 06, 2008 7:33 pm    Post subject: Reply with quote

Quote:
Title matching (as far as I'm aware) will be is impossible to use in Explorer,
No!
Please check the help again.
help wrote:
The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode). If this and the other 3 parameters are omitted, the Last Found Window will be used. To use a window class, specify ahk_class ExactClassName (shown by Window Spy). To use a process identifier (PID), specify ahk_pid %VarContainingPID%. To use a window group, specify ahk_group GroupName. To use a window's unique ID number, specify ahk_id %VarContainingID%. The search can be narrowed by specifying multiple criteria. For example: My File.txt ahk_class Notepad
Back to top
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 7:39 pm    Post subject: Reply with quote

Thank you!

I don't know why I didn't see that before.

That has certainly pointed me in the right direction. I'll read up on it.


Last edited by Renfro on Sun Jul 06, 2008 7:43 pm; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6264

PostPosted: Sun Jul 06, 2008 7:42 pm    Post subject: Re: Run a script in any Explorer window, but not the Desktop Reply with quote

Renfro wrote:
I haven't managed to figure out what the best way is to restrict my scripts to only being applied to Explorer windows.


Try this:

Code:
SetTitleMatchMode RegEx       

#IfWinActive ahk_class ExploreWClass|CabinetWClass
  MButton::MsgBox, You middle clicked on Windows Explorer
#IfWinActive

; Credit: posted by PhiLho somewhere in this forum


Smile
_________________
Back to top
View user's profile Send private message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 8:04 pm    Post subject: Reply with quote

Thanks Skan.

To my amazement (I'm usually rubbish at this kind of thing) I actually now have it working exactly as I want! Who knew that the "Desktop" was actually Program Manager in disguise - lol (Windows 3.1 is alive and well, it seems). Smile

One more question, when using ahk_class CabinetWClass is there a way to differentiate between say Control Panel and My Computer without using the displayed window name as the criteria? It's not that big a deal, but seeing as the "My Computer" name is something that can be changed, it would be nice if the script carried on working for that particular shell folder regardless of how it has been named by the user.

Window Spy doesn't seem to show any other info relating to 'Window Title & Class ' that I can use.
Back to top
View user's profile Send private message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 8:24 pm    Post subject: Reply with quote

Renfro wrote:
One more question, when using ahk_class CabinetWClass is there a way to differentiate between say Control Panel and My Computer without using the displayed window name as the criteria? Window Spy doesn't seem to show any other info relating to 'Window Title & Class ' that I can use.

I think I've answered my own question here. It seems like I need to use ahk_PID (which I will have to determine using Winget, rather than looking in Window Spy).
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6264

PostPosted: Sun Jul 06, 2008 8:27 pm    Post subject: Reply with quote

Renfro wrote:
when using ahk_class CabinetWClass is there a way to differentiate between say Control Panel and My Computer without using the displayed window name as the criteria?


You could test contents of the address bar ( presuming explorer window has one )
Refer AHK Doc for ControlGetText.

Smile
_________________
Back to top
View user's profile Send private message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Sun Jul 06, 2008 8:46 pm    Post subject: Reply with quote

GodlyMario wrote:
# doesnt exist for ifwinnotactive, so you can use brackets like this:

IfWinNotActive [, WinTitle, WinText, ExcludeTitle, ExcludeText]
{
script of yours
}


I tried that, but it didn't work. So out of curiosity I tried putting a hash in front of IfWinNotActive and surprisingly the script did what I wanted it to.

Code:
#NoTrayIcon
#SingleInstance force
#IfWinNotActive ahk_class Progman
#x::Send !{F4}
<^>!x::ExitApp


I found that removing the hash symbol from the ifWinNotActive stopped it from working as intended. However adding the hash symbol makes the script work as desired.
Back to top
View user's profile Send private message
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Mon Jul 07, 2008 12:30 am    Post subject: Reply with quote

I'm really stuck now with something else.

I've made a script to allow inverting a file selection in Explorer by using Ctrl + i as a hotkey. Windows (for some inexplicable reason) uses this hotkey to show the Favourites bar, so I had to over-ride this behaviour. I got it working fine by doing the following:

Code:

$^i::
IfWinActive ahk_class ExploreWClass
Send !ei
return


However, it was not being applied to the My Computer window because I did not include ahk_class CabinetWClass. So I tried putting

Code:

$^i::
IfWinActive ahk_class ExploreWClass|CabinetWClass
Send !ei
return


but while that does make it work in My Computer, it also makes it work in Notepad for some reason, and I want it to work only in Explorer.

I've tried using the square bracket layout instead of | and all sorts of other combinations (including even trying to exclude all window types except Explorer windows) but my syntax is not up to scratch, and so despite following examples in the Help file I still can't get it to work.

Where am I going wrong?
Back to top
View user's profile Send private message
Guest






PostPosted: Mon Jul 07, 2008 4:42 am    Post subject: Reply with quote

Code:

$^i::
IfWinActive ahk_class ExploreWClass|CabinetWClass
Send !ei
return


use #IfWinActive instead:

Code:

#IfWinActive ahk_class ExploreWClass|CabinetWClass
$^i::
Send !ei
return
#IfWinActive
Back to top
Renfro



Joined: 06 Jul 2008
Posts: 8

PostPosted: Mon Jul 07, 2008 10:46 pm    Post subject: Reply with quote

Hi,

I had already tried adding the hash symbol in front of IfWinActive, but it doesn't work.

In my example:
Code:
$^i::
#IfWinActive ahk_class ExploreWClass|CabinetWClass
Send !ei
return

This works in Explorer and Windows shell folders, but it also works in every other window too.



In your code example - i.e. adding the hash, but also placing $^i:: below the IfWinActive statement, and then adding #IfWinActive after the return command:
Code:
#IfWinActive ahk_class ExploreWClass|CabinetWClass
$^i::
Send !ei
return
#IfWinActive

This breaks the script entirely. It doesn't work at all in any window (not even Explorer).

Sad
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group