Autohotkey: Restart a "screensaver" script/part of a script (Starting VLC) - automation

I wanted to create a "VLC-screensaver" script:
When the user/system is idle for a certain time VLC should start and play a video from a specified folder. I can start the script and VLC is being executed by it after the set time. Now I exit it with "Esc" and VLC closes.
After I closed it the AHK-tray is visible but VLC/the script is not starting again after the set time...
Where is the mistake? Thank you in advance!
#Persistent
SetTimer, Check, 1000
return
Check:
If (A_TimeIdle>=10000)
{
run C:\Program Files\VideoLAN\VLC\vlc.exe --repeat --fullscreen "D:\video"
SetTimer, Check, Off
}
return
#IfWinActive ahk_exe vlc.exe
Escape::Send !{F4}
#IfWinActive
return

Got it by myself:
#IfWinActive ahk_exe vlc.exe
Escape::
Send !{F4}
Reload
#IfWinActive
Return
Forgot that you can assign more than one action/paramter/value to one
key...

Related

autohotkey variables not updating on keypresses

I have a number of hotkeys which go through various processes and sleep for certain periods of time to allow animations to be shown. I want to setup a variable to allow only 1 to activate at a time...so if I hit the first and then the second....the second doesn't do anything until the first completes. Similarly I don't want to double activate any of them by mashing the same key right after I first press it.
F4:ExitApp
#IfWinActive ahk_class notepad
a::
if (work:=true){
work:=false
soundbeep
;do something
sleep 1000
work:=true
}
return
b::
if (work:=true){
work:=false
soundbeep
;do something else
sleep 2000
work:=true
}
return
i.e. if I hit 'a'....'b' cannot activate until the sleep of 'a' ends. Nor should 'a' be able to activate a second time...at least not until its sleep ends and work=true again.
This leaves 2 problems. I need to first somehow specify the initial value of 'work=true'. I do not know how to do this in the traditional sense. Simply putting it at the top of the code doesn't work. Second problem, putting this in another key...like enter:: work:=true return....and pressing that key initially...this doesn't work as it allows b to beep while a is still in its sleep phase. So maybe the code above is flawed to begin with as well. Individual keys seem to respect themselves and not re-initialize until after the first instance has completed...so the beeps work if I just mash 1 key. Also I don't want to have to press enter to get the code to work after loading the script.
Is there an easy cheat here? Like in lua undeclared variables are automatically false... so I could just swap to if (work=false){...., I can't find any such behaviour with autohotkey though.
As requested, here is a different solution that uses variable logic to allow or prevent hotkey commands from executing:
#IfWinActive ahk_class Notepad
work:=true
a::
if(work){
work:=false
soundbeep
;do something
sleep 1000
work:=true
}
return
b::
if(work){
work:=false
soundbeep
;do something else
sleep 2000
work:=true
}
return
Update: Breakdown of how it works
First, the script begins in the auto-execute section. As summarized elegently by maul-esel
When an AutoHotkey script launches, it just starts execution from the
first line of your script. It continues from there line by line until
it hits an end point. This is called the auto-execute section.
As such, by including work:=true in this section of the script, not only are we initializing the value of the variable before any hotkeys are triggered, we set the scope of work to be a global variable, accessible across different hotkeys.
The rest of the script is a bit more straightforward, where each hotkey is essentially (in pseudocode):
When the hotkey is triggered
if work is true
set work to false
Beep and etc.
Wait for some amount of time
Then set work back to true so that another command can be run
End the hotkey
One simple way to do it would be to declare #MaxThreads 1 at the top of your script.
As a side note, there appear to be a few other syntax errors in the script. For example, when comparing values (like in an if statement), you still use the regular, single = and not the assignment :=. Also, the ahk_class is case sensitive, and as such #IfWinActive ahk_class notepad should be replaced with #IfWinActive ahk_class Notepad.
Updated Code (Work Variable logic was removed as it is no longer needed):
#MaxThreads 1
#IfWinActive ahk_class Notepad
a::
soundbeep
;do something
sleep 1000
return
b::
soundbeep
;do something else
sleep 2000
return

AutoHotKey: wait for process, breaking on user input

A script is waiting for a process to start; it shall do so indefinitely, but only as long as there is no human input. In another words: the script shall wait for either process start or human input, whichever comes first (also, the process may already be running when the script starts, in which case the script shall close immediately). I have thought of something like this, but there’s likely a better way, since this loop doesn’t break with input:
while (A_TimeIdlePhysical > 100) {
Process, Wait, SomeProcess.exe
}
Any ideas?
Tested with notepad:
#Persistent
SetTimer, DetectProcess, 50
return
DetectProcess:
If (ProcessExist("notepad.exe")) ; if the process is already running
ExitApp
; otherwise:
If (A_TimeIdlePhysical > 100) ; as long as there is no human input
{
If (ProcessExist("notepad.exe")) ; wait for either process start
ExitApp
}
else ; or human input
ExitApp
return
ProcessExist(ProcessName){
Process, Exist, %ProcessName%
return Errorlevel
}

Sh Loop command until return value =0

How to loop a command until a return value =0 in a sh script?
I need to run a ftp upload many times until this works (return value=0) maybe with a little sleep command.
This should do the job:
until command here; do; done
You may want to sleep inside to not DoS the server or waste a lot of bandwidth:
until command here; do sleep 1; done
If your command contains semicolons, parenthesise it.

Killing processes - AHK

So far I have:
Process, Exist notepad.exe
Process, Close, %p_id%
How do you set ahk to kill the process if it exists? I read it's something to do with the PID, but don't know how to implement that.
Have a look at the Documentation.
You can kill by simply using the name of the process:
Process, Close, notepad.exe
If the process does not exist, it will do nothing.
If you still would like to kill the process by using the pid instead, you must use the WinGet command in order to retrieve the pid.
there are at least two ways to get the PID from a window that I can think of immediately
1:
WinGet, My_PID, PID, WinTitle
2:
Run, ProgramFilePath "Args", Options, My_PID
The first one is to get an already running window PID and the second is to get a PID when opening the program with AHK. In both cases the variable "My_PID" now contains the window process ID
To answer your question of closing a process if it exists you could try a couple of methods.
ifWinExist ahk_pid %My_PID%
Process, Close, %My_PID%
; OR
Process, Exist, %My_PID% ; from my examples above
;Process, Exist, notepad.exe ; from your example above
If ErrorLevel ; Errorlevel is set to matching PID if found
Process, Close, %ErrorLevel%
I think that should answer your immediate question
This AHK script kills the active process when pressing Ctrl+Alt+K:
^!k::
{
WinGet, xPID, PID, A
Process, Close, %xPID%
}
return

Best AutoHotKey macros? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I use AutoHotKey for Windows macros. Most commonly I use it to define hotkeys that start/focus particular apps, and one to send an instant email message into my ToDo list. I also have an emergency one that kills all of my big memory-hogging apps (Outlook, Firefox, etc).
So, does anyone have any good AHK macros to share?
Very simple and useful snippet:
SetTitleMatchMode RegEx ;
; Stuff to do when Windows Explorer is open
;
#IfWinActive ahk_class ExploreWClass|CabinetWClass
; create new folder
;
^!n::Send !fwf
; create new text file
;
^!t::Send !fwt
; open 'cmd' in the current directory
;
^!c::
OpenCmdInCurrent()
return
#IfWinActive
; Opens the command shell 'cmd' in the directory browsed in Explorer.
; Note: expecting to be run when the active window is Explorer.
;
OpenCmdInCurrent()
{
WinGetText, full_path, A ; This is required to get the full path of the file from the address bar
; Split on newline (`n)
StringSplit, word_array, full_path, `n
full_path = %word_array1% ; Take the first element from the array
; Just in case - remove all carriage returns (`r)
StringReplace, full_path, full_path, `r, , all
full_path := RegExReplace(full_path, "^Address: ", "") ;
IfInString full_path, \
{
Run, cmd /K cd /D "%full_path%"
}
else
{
Run, cmd /K cd /D "C:\ "
}
}
Here is so simple but useful script:
^SPACE:: Winset, Alwaysontop, , A
Use CTRL + Space to set any window always on top.
Add surrounding quotes on selected text/word
Useful when writing emails or during coding...
Doubleclick word, hit Win+X, have quotes around
; Win + X
#x:: ; Attention: Strips formatting from the clipboard too!
Send ^c
clipboard = "%clipboard%"
; Remove space introduced by WORD
StringReplace, clipboard, clipboard,%A_SPACE%",", All
Send ^v
return
; I have this in my start menu so that I won't ruin my ears when I put on my headphones after rebooting my computer
sleep, 5000
SoundSet, 1.5 ; really low volume
I create new Outlook objects with AutoHotKey
; Win+Shift+M = new email
#+m:: Run "mailto:"
; Outlook
#^M:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE" /recycle
; Win+Shift+A = create new calendar appointment
#+A:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.appointment
; Win+Shift+T = create new Task
; Win+Shift+K = New task
#+T:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.task
#+K:: Run "%ProgramFiles%\Microsoft Office\Office12\OUTLOOK.EXE"/c ipm.task
Here's a dead-simple snippet to quickly close the current window using a mouse button.
It's one of the actions you perform most often in Windows, and you'll be surprised at how much time you save by no longer having to shoot for that little X. With a 5-button mouse, I find this a very useful reassignment of the "Forward" button.
#IfWinActive ;Close active window when mouse button 5 is pressed
XButton2::
SendInput {Alt Down}{F4}{Alt Up}
Return
#IfWinActive
To take into account programs that use tabbed documents (like web browsers), here's a more comprehensive version:
;-----------------------------------------------------------------------------
; Bind Mouse Button 5 to Close Tab / Close Window command
;-----------------------------------------------------------------------------
; Create a group to hold windows which will use Ctrl+F4 instead of Alt+F4
GroupAdd, CtrlCloseGroup, ahk_class IEFrame ; Internet Explorer
GroupAdd, CtrlCloseGroup, ahk_class Chrome_WidgetWin_0 ; Google Chrome
; (Add more programs that use tabbed documents here)
Return
; For windows in above group, bind mouse button to Ctrl+F4
#IfWinActive, ahk_group CtrlCloseGroup
XButton2::
SendInput {Ctrl Down}{F4}{Ctrl Up}
Return
#IfWinActive
; For everything else, bind mouse button to Alt+F4
#IfWinActive
XButton2::
SendInput {Alt Down}{F4}{Alt Up}
Return
#IfWinActive
; In FireFox, bind to Ctrl+W instead, so that the close command also works
; on the Downloads window.
#IfWinActive, ahk_class MozillaUIWindowClass
XButton2::
SendInput {Ctrl Down}w{Ctrl Up}
Return
#IfWinActive
Visual Studio 2010 can't easily be added to the CtrlCloseGroup above, as it's window class / title aren't easily predictable (I think). Here's the snippet I use to handle it, including a couple additional helpful bindings:
SetTitleMatchMode, 2 ; Move this line to the top of your script
;-----------------------------------------------------------------------------
; Visual Studio 2010
;-----------------------------------------------------------------------------
#IfWinActive, Microsoft Visual Studio
; Make the middle mouse button jump to the definition of any token
MButton::
Click Left ; put the cursor where you clicked
Send {Shift Down}{F2}{Shift Up}
Return
; Make the Back button on the mouse jump you back to the previous area
; of code you were working on.
XButton1::
Send {Ctrl Down}{Shift Down}{F2}{Shift Up}{Ctrl Up}
Return
; Bind the Forward button to close the current tab
XButton2::
SendInput {Ctrl Down}{F4}{Ctrl Up}
Return
#IfWinActive
I also find it useful in Outlook to map ALT+1, ALT+2, etc. to macros I wrote which move the currently selected message(s) to specific folders (eg. "Personal Filed", "Work Filed", etc) but that's a bit more complicated.
There are tons of good ones in the AutoHotKey Forum:
http://www.autohotkey.com/forum/forum-2.html&sid=8149586e9d533532ea76e71e8c9e5b7b
How good? really depends on what you want/need.
I use this one all the time, usually for quick access to the MySQL command line.
http://lifehacker.com/software/featured-windows-download/make-a-quake+style-command-prompt-with-autohotkey-297607.php
Fix an issue when copying file to FTP server when the "Copying" dialog appears on top of the "Confirm File Replace" dialog (very annoying):
SetTimer, FocusOnWindow, 500
return
FocusOnWindow:
IfWinExist, Confirm File Replace
WinActivate
return
One to deactivate the useless Caps-lock key:
Capslock::
return
CTRL + shift + c will copy colour below cursor to the clipboard (in hexadecimal)
^+c::
MouseGetPos,x,y
PixelGetColor,rgb,x,y,RGB
StringTrimLeft,rgb,rgb,2
Clipboard=%rgb%
Return
Write your email address in the active field (Win key + m)
#m::
Send, my#email.com{LWINUP}
Sleep, 100
Send, {TAB}
return