How to switch between Chrome windows in Powershell? - windows

I am writing a powershell script that goes through 3 chrome windows (not tabs, these windows are on 3 different monitors) and want to refresh every window.
I cannot figure out a way to switch between windows.
I have the following code:
while(1) { # Loop forever
sleep -Seconds 15 # Wait 15sec
$wshell = New-Object -ComObject wscript.shell
if($wshell.AppActivate('Chrome')) { # Switch to Chrome
Sleep 1 # Wait for Chrome to "activate"
$wshell.SendKeys('{F5}') # Send F5 (Refresh)
} else { break; } # Chrome not open, exit the loop
}
This only refreshes one page. I tried to use
[System.Windows.Forms.SendKeys]::SendWait("%+{ESC}")
to switch between apps, but since there are other open apps it is not possible.
Is there a way to switch between Chrome windows? Haven't found any shortcuts or solutions.
Thank you!

Add this before your refresh:
$wshell.SendKeys('^{%TAB}'); # Performs an Alt + Tab to switch windows
Of course this implies ONLY chrome windows are open
Since multiple programs are open you could give this a shot:
($wshell.AppActivate((get-process chrome).MainWindowTitle))

If you are looking for a shortcut, try this:
If Chrome icon is on the first place (leftmost) on your taskbar, press Win + 1 TWICE to switch to the second window, press Win + 1 three times to switch to the third window.
About emulating Win key in Powershell, the simplest way is Ctrl + Esc; but it doesn't work as Win + x combination. This answer may help: https://stackoverflow.com/a/51868925

Related

Close Win10 'Start Menu' if open using AHK?

I've made a AHK script to keep an application open and focused as soon as Windows starts / logs in. I've since found though that WinActivate doesn't work in Windows 10 if the Start Menu is open. And apparently the last time "WinClose, Start Menu" worked was in WinXP.
Here's my code changed to use notepad so you can easily see what I'm trying to do (WinClose Start Menu step):
#WinActivateForce
FocusTimer = 3000 ;Time spent between refocusing on window (in milliseconds)
APP_TITLE = Untitled - Notepad ;Title of application window
APP_EXE = C:\Windows\System32\notepad.exe ;Application executable filepath
Sleep, %FocusTimer% ;Allows time for Windows to login and run other startup processes
While(TRUE) ;Infinite loop for program commands
{
Run, %APP_EXE% ;Opens application
Sleep, %FocusTimer% ;Allows time for previous action
While WinExist(APP_TITLE) ;Infinite loop for while application window is available
{
WinClose, Start Menu ;Closes Start Menu if open so as not to interrupt WinActivate command
WinActivate, %APP_TITLE% ;Commands focus on the application window
WinWaitActive, %APP_TITLE%, , 0 ;Waits for application window to have focus
Sleep, %FocusTimer% ;Wait timer before starting the loop again
}
Sleep, %FocusTimer% ;Allows clean-up time before starting the program commands again
}
I don't want to do anything drastic by disabling the Start Menu altogether. I just want to close it to remind the user that "this PC is only to run this application". It'll also alert the fact if randoms walk in and say "The Start Menu keeps closing???" If they want to interrupt the application then they have to manually pause the script via the system tray icon.
TIA
The Start Menu's title is no longer "Start Menu". This seems to work for me:
WinClose ahk_class Windows.UI.Core.CoreWindow ahk_exe SearchUI.exe
WinClose ahk_class Windows.UI.Core.CoreWindow ahk_exe StartMenuExperienceHost.exe ; Suggested by 0x464e
It will also close search and Cortana.

Windows 10: command window does not close, but closes in Windows 7

i'm using windows 10 and trying to start an exe in diffrent folders sequentially. The idea is to use popen from subprocess and wait for the process to finish. The problem is, that the window does only close if I click on it or hit a key. On a windows 7 machine however the window is closing and subprocess.wait() finishes.
import os
import subprocess
for path in ["C:\tmp\", "C:\tmp2\"]:
os.chdir(path)
mywindow = subprocess.Popen(["my.exe",
#stdin=subprocess.PIPE,
#stdout=subprocess.PIPE,
#stderr=subprocess.PIPE,
#shell=True,
creationflags=subprocess.CREATE_NEW_CONSOLE,
env=dict(os.environ, **{"test":"AUTO"}))
mywindow.wait()
I used creationflags=subprocess.CREATE_NEW_CONSOLE, because I need to be able to start my.exe multiple times and be able to read the output on another console. But in some cases I want to wait for my process to end.
Update:
After eryksuns comment I'm pretty sure that this more of a windows problem than a python one.
On Windows 7 "my.exe" exits and wait() returns afterwards. On windows 10 however, I have to click somewhere on the screen or press any key, in order to close the console window "my.exe" opens. Afterwards the wait()returns. Disabeling the cmd quick edit mode had no effect.

WinActivate not working in autohotkey script

I have the following AutoHotKey script to help me switch between different open apps, using a shortcut:
^!c::ToggleWindow("Chrome")
^!p::ToggleWindow("PowerShell")
ToggleWindow(TheWindowTitle)
{
SetTitleMatchMode,2
DetectHiddenWindows, Off
IfWinActive, %TheWindowTitle%
{
WinMinimize, %TheWindowTitle%
}
Else
{
IfWinExist, %TheWindowTitle%
{
WinActivate
;;; Tried using WinMaximize/WinRestore here but same result
}
Else
{
DetectHiddenWindows, On
IfWinExist, %TheWindowTitle%
{
WinShow
WinActivate
}
}
}
}
The problem is that for some apps it works only sometimes (for example, PowerShell and TortoiseHG Workbench) which is really frustrating. For other apps (Chrome, Thunderbird) it works always.
Here is what I've found so far:
If you explicitly minimize a "problematic" app then you can never activate/maximize the window using the AHK shortcuts. I'm not sure if there are other scenarios that prevent the shortcuts from working but this is one certain way to replicate the problem (at least for me).
Even in the cases where the shortcuts don't work, I can see that the target app icon in the taskbar is getting highlighted. I guessing it works somewhat halfway, activating the window but not actually showing it.
I think the problem is not restricted to AutoHotKey only because I can replicate this behavior just with the Task Manager. If I go to the 'Applications' tab, right click on on target app and choose 'Bring to front', the same thing happens. BUT, if I choose 'Switch to' instead, it works!
So, I guess my question is what exactly does "Task Manager->Applications->Switch to" do and is there an equivalent that I can use in AHK. My OS is Win7.
Try to put a #WinActivateForce in your script.
If you run AutoHotkey as normal user, it may be unable to manage program running as administrator (for example PowerShell). If that's the case, try to run AutoHotkey as administrator.
If you explicitly minimize a "problematic" app then you can never
activate/maximize the window using the AHK shortcuts. I'm not sure if
there are other scenarios that prevent the shortcuts from working but
this is one certain way to replicate the problem (at least for me).
According to AutoHotkey's release notes, this is a known issue which was fixed in AutoHotkey v1.1.20. (Released 1 month after this question was asked)
1.1.20.00 - March 8, 2015
Changes
Changed WinActivate to restore the window if already active but minimized.
Changed WinActivate to look for a visible window to activate if DetectHiddenWindows is off and the active window is hidden, instead of
doing nothing

Can I make a shortcut to specify a switched tab in Mac OSX Terminal?

In Chrome, it's possible to switch tabs with cmd + tab_number, like cmd + 3 to go to the third tab. Is there anyway to specify this for Terminal on a Mac running Mavericks?
I know how to change a shortcut via System Preferences, but I'm not the exact name of the Menu Command I should add, if it's even possible.
You can Shift + Command + Left or Right Arrow to cycle through tabs:
Shift ⌘ ←→
or
⌘{}
If you want to use a specific # as you mentioned then you'll have to script it or use windows instead of tabs.

How to map a global key to refresh?

Winamp has a neat feature. Global keys. That way I can change the playing song even if Winamp GUI doesn't have focus.
I am looking for a similar solution to Firefox or Chrome.
I use Eclipse to code PHP.
It auto SSH's and saves to another machine (testing)
I could use something like XRefresh with a mapped virtual drive but I cant install Samba on the testing machine.
Right now I have to:
CTRL+S (save and auto-update)
ALT+TAB (switch to Firefox GUI)
F5 (refresh current Firefox page)
ALT+TAB (back to Eclipse)
I am looking for something like:
CTRL+S (save and auto-update)
CTRL+X (refresh Firefox - while keeping focus on Eclipse)
I've looked over Firefox Plug-ins but found nothing to my needs. Chrome neither. XRefresh would be a perfect solution but, as said, cant SSH/Samba into the testing machine.
Autohotkey
Let me add my result for the awesome.ahc file:
^s::
SetTitleMatchMode, 2
IfWinExist, Mozilla Firefox
{
WinActivate, Mozilla Firefox
ControlSend, ahk_parent, {F5}, Mozilla Firefox
}
Return
This will switch to firefox and refresh the currently active tab.
As demoncodemonkey greatly suggested you can use Autohotkey. This is my script sample.
^x:: ; listen for a CTRL+x
Send ^s ; sends a CTRL+s save command to Eclipse
Sleep 500 ; sleeps a bit to allow SSH to transfer file
Send !{tab}^r ; alt-tab followed by a browser refresh
Sleep 100 ; firefox, needs just a bit to allow ALT-TAB
Send !{tab} ; tabs back to eclipse
This is even better than I had in mind as I can do it all with only a single command. Very impressive. Thanks again demoncodemonkey.
Using Frankie's answer, I developed something a bit more advanced. My editor is Aptana, but you can easily change it into anything else:
$^s:: ; only capture actual keystrokes
SetTitleMatchMode, 2 ; match anywhere in the title
IfWinActive, Aptana Studio 3 ; find aptana
{
Send ^s ; send save command
IfWinExist, Mozilla Firefox ; find firefox
{
WinActivate ; use the window found above
Sleep 500 ; sleeps to allow SSH to transfer file
Send ^r ; send browser refresh
WinActivate, Aptana Studio 3 ; get back to Aptana
}
}
else
{
Send ^s ; send save command
}
return

Resources