A hotkey which brings me back to the previous application?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
KWiz
Junior Coder
Posts: 30
Joined: Sat Oct 07, 2006 1:43 pm

A hotkey which brings me back to the previous application?

Post by KWiz » Sat Feb 20, 2010 7:57 pm

Hi,

I want to create a macro (a hotkey specifically) which will toggle my current window (/application) back to the previous window (/application) that I was looking at most recently. Is there a way to do this?

For example, let's say that I have 5 windows (/applications) open:
1. Notepad
2. Wordpad
3. Macroscheduler
4. IE
5. OE

And let's say that I start by looking at notepad. Then I switch over to looking at my wordpad document. Then I want to switch back to my previous application (obviously notepad in this case), but I want to do it with a hotkey (/macro). How would I create a macro that would do that for me? Which commands would I use? Would the macro have to be running constantly in order to keep a running list of my most recently viewed windows??

Thanks,
KWiz

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sat Feb 20, 2010 8:12 pm

Hi KWiz,

You certainly could write a Macro that did that... but why not just use the hotkey (keyboard shortcut keys) that already come with Windows itself.

Just hold the Alt key down, tap the Tab key then let go of them both... and it will switch you back to the previous app that had focus.

If you Google for "windows keyboard shortcuts" you'll find all kinds of information. Here's a link from Microsoft:

http://support.microsoft.com/kb/126449

ALT+TAB: Switch between open programs

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

KWiz
Junior Coder
Posts: 30
Joined: Sat Oct 07, 2006 1:43 pm

Post by KWiz » Sat Feb 20, 2010 8:59 pm

The Alt+Tab feature does not work properly if the previous window is minimized. I guess that is what the problem is here. So that is something else to consider when trying to write this macro.

Again, I have no idea where to start really.

Thanks for any help from anyone,
KWiz

KWiz
Junior Coder
Posts: 30
Joined: Sat Oct 07, 2006 1:43 pm

Post by KWiz » Sun Feb 21, 2010 12:36 am

Did I stump you guys? lol

KWiz

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Feb 21, 2010 1:45 am

ALT-TAB does go to minimized windows in XP PRO-SP3.

Don't know where to start? Have you tried to create any script? Show us and let us help you.

The steps are the same as you would do manually.
To do the ALT-TAB, you need to follow a structure something like this:
Press The ALT key
Press the TAB key
Release the ALT key.


Here are a few ways to get started.....

Have you tried to record macro, then read it and edit as necessary if needed? Have you checked Help for the PRESS command?
Have you done a Single Step through your script if it does not work?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

KWiz
Junior Coder
Posts: 30
Joined: Sat Oct 07, 2006 1:43 pm

Post by KWiz » Sun Feb 21, 2010 1:56 am

Well, Alt+Tab works, but not the way I want it to. Sure, Alt+Tab will bring up a new window with all of the icons of all the currently open applications. But if you minimized your previous application, that application will not be NEXT in line. You will have to tab over, as you say, until you find the right icon.

Sure it works. It's just a tad slow for my tastes, that's all. I'm constantly having this little problem. Some sort of a hotkey would be very useful for me.

No, I haven't started a script yet because I don't really know where to start. Sure, I could have the script press Alt+Tab, but then what? How can I tell MacroScheduler what the last application I had open was, even though it may or may not be currently minimized?

Like I was asking in my original post, does a macro need to continuously run in order to "know" which application was the most recently used?

Like really, I have no idea where to start here.

Thanks for any help,
KWiz

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sun Feb 21, 2010 2:58 am

Macro Scheduler can automate anything that you can do manually. It can also read other files including INI and Registry files. I am not aware of any reigstrty settings or INI files that keep track of the last window you had focus in. There may be one, I just don't know it. ALT-SHIFT-TAB also works with minimized windows, you could press that 2 times to wrap in the other direction and activate the last item in the ALT-TAB window, but I am not sure that the last program used is the last one listed....(I just did some testing, the initial test looked like the last pgm was next with ALT-TAB, but when they were minimized, that no longer happens, that idea is no good).

But maybe you could create a file or registry key of your own. You would want to update that key value when you lose focus on a window, writing that window name to the registry. You would need a macro running constantly with a small delay to check when you lost focus. Then when you hit the hot key, you would read the registry key and open that window. The constantly running macro would overwrite the registry key with the current window that is being closed. There is a command IfWindowClosed, that could be a trigger if a window is minimized, but that does trigger if you just jump between windows.

I don't know of a key or a trigger that happens when you change focus. If someone can identify if an event happens, then the rest of the script should be pretty straightforward.

Hmmm, just remembered GetActiveWindow> and GetWindowProsess> commands. Perhaps a loop like this:

Code: Select all

Label>MainLoop
Wait>.01
GetActiveWindow>vCurrentWindow,x,y
GetWindowProcess>%vCurrentWindow%,vProcessID,vProcessName
If>%VProcessID=%vLastProcessID,SameWindow,LastWindow
Wait>.01
GoTo>MainLoop

SRT>SameWindow
Let>vLastWindow=%vCurrentWindow%
Let>vLastProcessID=%vProcessID%
End>SameWindow

SRT>LastWindow
WindowAction>1,%vLastWindow%
SetFocus>%vLastWindow%
End>LastWindow
This is a sample skeleton structure, untested, may need some delays or corrections to the loops/syntax. No access to Macro Scheduler right now. May have a problem the first time it is read since there will be no %vLastProcessID% the first time around, so there will be no %VLastWindow% to maximize and get the focue, that might cause an error......
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sun Feb 21, 2010 8:23 am

Hi KWiz,

I think I see some of what you're talking about. Let's say you have a bunch of apps running. Notepad has focus and then you minimize all apps... maybe by hitting Windows Key-D.

At that point, if you hit ALT-TAB, its true, Notepad is not the first choice it gives you. Yes, you have to "go digging" through what ALT-TAB gives you.

I hacked together the following macro that remembers the last two window titles that had focus.

Yes you have to keep it running all the time in the background... or it won't be able to watch and keep track of what windows you open.

It sets its own hotkeys internally... no need to set a hotkey in the Macro Scheduler app itself... unless you just want to use one to start the macro running.

If you have the Pro version, I'd recommend just compiling it and pointing a desktop icon to the exe... you can set a hotkey on that desktop icon if you like to fire it up.

Once running, hitting ALT+F11 will flip you back to the previous window you had open and hitting ALT+F12 will exit the macro.

In the special case where you just minimized all apps, not only will hitting ALT+F11 flip you back to your last app... hitting ALT+F11 again will flip you back further to the one before that.

Code: Select all

//pressing ALT+F12 will exit
OnEvent>KEY_DOWN,VK123,3,KeyPress_Exit

//pressing ALT+F11 will flip back to previous app that had focus
OnEvent>KEY_DOWN,VK122,3,KeyPress_Flip

MDL>App-Flip-Back%CRLF%%CRLF%Hit Alt+F11 to flip back%CRLF%%CRLF%Hit ALT+F12 to Exit

GetActiveWindow>title,x,y
Let>previous_title=title
Let>previous_previous_title=title

Let>empty=
Let>first_new_window_seen=yes

//Note: title will be the empty string when desktop has focus.
//Do not save an empty string into the variables previous_title and previous_previous_title
//as the SetFocus>command will fail if you use that value.

Label>start
Wait>0.01
GetActiveWindow>title,x,y

If>title=empty
  Let>desktop_has_focus=yes
Else
  Let>desktop_has_focus=no
EndIf

//The following is to keep the mysterious window with a title value
//of one character that looks like a little box from messing us up.
//An unfortunate side-effect is, our switcher won't remember windows
//with titles less than 2 characters long. Ahh well, if you find out why... post!
Length>title,title_length
If>title_length<2,start

If>{(%title% <> %previous_title%) AND (%title% <> %empty%) AND (%first_new_window_seen% = "yes")}
  //initialize title variables with non-desktop (non-empty) window titles
  Let>previous_title=title
  Let>previous_previous_title=title
  Let>first_new_window_seen=no
EndIf

If>{(%title% <> %previous_title%) AND (%title% <> %empty%) AND (%first_new_window_seen% = "no")}
  Let>previous_previous_title=previous_title
  Let>previous_title=title
EndIf

Goto>start

SRT>KeyPress_Flip
If>desktop_has_focus=yes
  SetFocus>%previous_title%
Else
  SetFocus>%previous_previous_title%
EndIf
Wait>0.2
END>KeyPress_Flip

SRT>KeyPress_Exit
MessageModal>ALT+F12 was pressed%CRLF%%CRLF%Exiting App-Flip-Back...%CRLF%%CRLF%Have a nice day
Exit>0
END>KeyPress_Exit
If anyone improves the above, please post your improvements.

Note: I tested this macro in Macro Scheduler 10.1.21 and the latest version 11.1.20 and it works OK on XP SP3.

However there are issues running this on Win 7 Professional in 11.1.20.

Marcus, the SetFocus> command behaves differently... try this macro in Win 7. It seems to be able to switch focus away from the current window that has focus (you see the current window gray-out) however the other window that should be coming to the front... just doesn't.

However, same macro in XP, no problem. Marcus can you reproduce this?
Last edited by jpuziano on Mon Feb 22, 2010 3:15 am, edited 1 time in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

KWiz
Junior Coder
Posts: 30
Joined: Sat Oct 07, 2006 1:43 pm

Post by KWiz » Sun Feb 21, 2010 6:22 pm

Wow, there's a couple of good macros right there!

Now that you've gone to the trouble of writing those two wonderful macros, I'm feeling a little bit guilty. The reason? Well, I wasn't aware of two little features which Bob pointed out in his last message:

1. When a window is minimized, it goes to the end of your "open windows list"
2. Alt+Shift+Tab will switch you back to THAT window.

Wow, how neat is that!?! ...So really, I guess I don't need a macro at all in this situation, unless I want to create a simple hotkey which does the pressing of Alt+Shift+Tab for me (pressing F1 would be admittedly a little bit easier, especially if used often).

However, I will need to remain aware of whether the previous window that I am looking for was minimized or not. Time will tell as to whether or not that will be an easy task or not. If the remembrance (as to whether I minimized the previous window or not) turns out to be too difficult to do on a daily basis, then I may have to use one of your wonderful macros, and keep it running in the background as you suggested.

However, I believe that the vast majority of the time when I am looking to switch back to the previous window, that previous window had been minimized. In fact, I would hazard a guess that this is true more than 93% of the time.

So a simple hotkey macro should work fine for me, as mentioned above. And of course I can write that one myself :D

Thanks for the help from both of you, and good luck with Windows 7 :shock:

KWiz

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Feb 22, 2010 10:33 am

John,

Your macro works fine for me in Win7 Ultimate x64.

However I think it would be best to add this to the top:

Let>WF_TYPE=0

At present it may get and try to focus child windows. We only want main windows.

Anyway, worked either way for me.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Feb 23, 2010 5:39 am

Marcus,

I was afraid you'd say that. I added your line and it made no difference, macro still does not work properly on Win 7 Professional on my new machine.

I wonder if this has anything to do with: Program Compatibility Assistant issues with 11.1.20

Later I may:
  • re-install Macro Scheduler 11.1.20 on that Win 7 machine only this time, if the Microsoft "Compatibility Assistant" pops up again, I'll choose the other button choice that said "re-install program with recommended settings"... and see if that fixes things.
  • or if that doesn't work, I may try uninstalling Macro Scheduler from that Win 7 machine and then installing a much earlier version. Marcus, what is the earliest 11 version that was Win 7 compatible?
This is not urgent for me as it only on the home Win 7 machine... but I would like to get SetFocus> working eventually. If anyone else out there has Win 7 and Macro Scheduler, please try the macro and post your results here.

I have an Objective-C course next week to get prepared for so I may not be around to reply to forums posts for awhile... but whatever results or trouble-shooting advice you can give is appreciated.

Thanks and take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts