Closing a window whenever it pops up inside other AHK script? - autohotkey

I have made an AHK script that will take file names from an Excel spreadsheet, open the file in a different program, then export that file to two different formats.
It's working pretty well, except sometimes, the file has some little tweak that needs to be done with it (wrong orientation, hidden layers, etc) that create a popup.
I've tried to program around that by using ifwinexist conditions, so that if I know when a window is SUPPOSED to pop up, the script can deal with it.
The problem I have is that the popup windows can happen at times that I do NOT expect.
So, is there a way to run a thread until the window pops up, closes the window, but then will continue to look for the window?
Thank you

You are looking for SetTimer, which spawns another thread. The following code checks for the window with partial name Notepad every second (1000ms), and closes it if found.
SetTitleMatchMode, 2
SetTimer, CheckWin, 1000
CheckWin:
IfWinExist, Notepad
WinClose, Notepad
Return

Related

Unsure why AHK script isn't working as intended

I use chrome bookmarks a lot, and I also often have to send people screen snippets very often, however I don't want all my bookmarks on display (some are sorta private). I know I could add them to the Other Bookmarks folder, but would rather easy access. What I've been doing for a while is hitting the bookmark shortcut (CTRL+SHIFT+B), then the snipping tool (WIN+SHIFT+S), taking my screenshot and then putting the Bookmarks back (CTRL+SHIFT+B). Eventually I decided to bite the bullet and spend some time automating it, so that hitting CTRL+SHIFT+S would close the Bookmark Bar, and letting go of the mouse (after taking the clipping) would put it back. This is what I came up with:
~#+s:: Send, ^+B
KeyWait, LButton, D
Send, ^+B
return
Although the first half works (Bookmarks go away, snipping tools open) at no point does the bar return. I've tried many things including setting up a timer, and waiting for the space bar instead of the mouse button, which i'd only hit when ready. I have also tested, and manually pressing the keys immediately after letting go the mouse button did indeed re-open the Bookmarks.
Would anyone be able to explain why this is happening? I would really appreciate any help!
First problem is that you put the first command on the same line as the hotkey definition.
This will create a one liner hotkey and the rest of the code below wont run.
Second problem is that you're sending the input to show bookmarks again while the screenshotting window is active. You're going to want to wait until chrome is active again.
This works:
#IfWinActive, ahk_exe chrome.exe
~#+s::
SendInput, ^+b
Sleep, 2000
WinWaitActive, ahk_exe chrome.exe
SendInput, ^+b
return
#IfWinActive
A bit of sleep so the screenshot window has time to open, and also added in #IfWinActive, because I'd assume you only want that hotkey to be active while you're on chrome.
Also switched over to SendInput and made the b lower case. Having it as uppercase would send Shift+B (on most keyboard layouts).

Close pop up windows that rises during powershell script execution

I'm new with powershell and I'm trying to execute a third party program from command line. In one of the executions path, suddenly a pop up window emerges and the script is paused until I click on the accept button or close that window.
What I need is to avoid that click.
Is it possible to tell the script to close any emerging window or to send an event to close it?
Those windows that suddenly pop up make it impossible for me to achieve my task... Is there any other way to handle this?

Have to "Reload This Script" in AutoHotkey for some programs to pickup remappings

I have a very simple AutoHotkey.ahk file, where I remap Capslock to Esc and Esc to Capslock (swapping the keys):
Capslock::Esc
Esc::Capslock
However, this doesn't work in games like Battlefield 4 or League of Legends. It simply does not do anything.
However, I can press Reload This Script in AutoHotkey, and the remapping will start working in those games. It does however work in other games without the need to reload the script first.
I hope you can help me. I would really like this to just work, instead of manually reloading the script.
Try using the #IfWinActive directive so that those hotkeys only work in the programs you want - then you don't need to ever reload.
Check in the documentation for "Context-sensitive hotkeys" (under "hotkey" in the index). Here is an example from the documentation:
#IfWinActive, ahk_class Notepad ^a::MsgBox You pressed Ctrl-A while Notepad is active. Pressing Ctrl-A in any other window will pass the
Ctrl-A keystroke to that window.
#c::MsgBox You pressed Win-C while Notepad is active.
But I think I know why it begins to "not work". It may be that certain programs set those keys as hotkeys. Whenever you start those programs they set their own hotkeys and it overrides your script. You see, the hotkey combination belongs to whatever program sets it last. When you reload the script, it resets your script as having the authourity.
So if you want the script to never be overridden, and you don't want to manually reload it, find out what programs the combinations don't work in, and use ifwinactive to reload your script using autohotkey's reload command.

In notepad++ change tab size instantly

I'm using notepad++ (v6.5.3) and I constantly have to change the size of the tab for viewing some results. Not that it's taking me a lot of time to do it manually everytime, but it would be great if I could optimize that.
Is there a way to do so? Would a macro be the solution, or are they just for typing stuff?
Thanks a lot!
Ok, there's an easy way how you can achieve this - I have tested it right now:
Install AutoHotKey (or start portable version which runs without installation)
In Windows 7 and above, ensure you launched AutoHotKey as Administrator (otherwise you get inconsistencies in its behavior) - if not sure, exit it and restart it as administrator
Right click Autohotkey tray icon and select Edit This Script
Import the macro below this list at the end of the AutoHotKey script file and save the file
Right click Autohotkey tray icon and select Reload This Script.
–– This was end of general steps, now let's go with your macro: ––
In N++, display Preferences window and press its Close button1 at the bottom (NOT at the top-right corner)
Now you can use shortcuts Win+F2 and Win+F3 to switch different tab sizes instantly
SendMode Input
DetectHiddenWindows, On
SetTitleMatchMode, RegEx
;--------------------------------- Hotkeys for Notepad++ only
#IfWinActive ahk_class Notepad\+\+
#F2::Send {F10}{Right 6}{Down}{Enter}{Tab 2}{Space}16{Enter}{Tab 3}{Space}
#F3::Send {F10}{Right 6}{Down}{Enter}{Tab 2}{Space}4{Enter}{Tab 3}{Space}
#IfWinActive
1) Important: N++ user experience provided in dialog boxes is absolutely terrible. There are no anchors where you can fix focus when using keyboard. Thus you always need to perform step 4 manually when leaving Preferences dialog box otherwise the macros would send keys into incorect window page OR at correct page but incorrect control. Preferences dialog window remembers selected page and control. Macros I created for you therefore assume that correct page is already listed and button Close was recently focused.
Good news is Notepad++ windows with this weird behavior are rare exception from general user experience. In other places in N++ (or in other apps) where user interface components (menus, dialogs etc.) always start from the same point you do not need any special precautions like the one in step 4.
Adjust the macros as you like:
you can create more of them
you can adjust the numbers "16" and "4" typed into tab size input box
you can change shortcut keys to something else
you can replace sending keys with sending mouse clicks at desired screen/window positions
you can achieve many other useful shortcuts in N++ and in all other apps – check AHK deeper!

How to have program shortcut icon open script along with program?

I want to have a script that works with a specific program open when I open that program with the program's shortcut icon. I have tried pasting the path into the target line in the icon's properties, but that didn't work. What do I need to do?
I think that I read that you want to create an icon that looks like the original application launch icon, which will, when clicked, indeed launch the original application AND run some extra ahk code. You can create a Shortcut or Link to a ahk script that will launch your app (run, c:\path\yourapp.exe) and perform other tasks (WinWaitActive, your app) etc.. In the link to the ahk script (does not work on the real ahk script, ONLY with the lnk file) you can select the properties (right click) and change the icon to whatever you want (often you can use the icon of the original application, by pointing to the .exe (instead of selecting icons from the the standard shell32.dll). So by selecting the new icon to e.g. c:/program files/officexx/word.exe , your AHK script will get the icon as if it is the word launch icon. Hope that answers your question.
At the beginning of the script, but ater you execute the run command, you place the timer
Run, abc.exe
SetTitleMatchMode, 2
Sleep, 1000 ; Give the app some time to run before you start the test
SetTimer, CloseScript, 1000
Return
CloseScript:
IfWinExist, place the windows title here....
Return
ExitApp
This is one of the first things that I did with AutoHotKey. I wanted a 'wrapper' script for a program that I regularly used that lets me to chose different program options with a script GUI form when I press the F4 key while clicking the link. Just clicking the link starts the program normally. Eat your heart out Microsoft.
In the link's Start-In property I put the actual program's directory and my script gets the program name and other details from the script's command line arguments in the link's target property.