topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday December 13, 2024, 4:41 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA: "WaitAndSend" - timer/keyboard macro combo program...  (Read 6818 times)

Onesimus Prime

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 15
    • View Profile
    • Donate to Member
IDEA: "WaitAndSend" - timer/keyboard macro combo program...
« on: February 18, 2010, 12:12 AM »
I may not know the depth of what I'm asking for, but here goes...

Would it be possible to have a program that, at a customizable system time or in 'x' mintes/seconds, makes sure that program 'y' is focused and sends it user-definable keystrokes?  And it'd be nice if this were "portable," not writing outside its own directory.

The situation prompting this request...
I'm recording a professor's lectures, and sometimes have to ditch class and leave the computer recording when he goes over-time.  After my next class, I come back and stop the recording--but this means that there's well over an hour of silence at the end of the recorded file!  And, even compressed, that takes up HD space, sometimes more than I have.  
So I was thinking it would be nice to have a program do something like, in my case, "in 10 minutes, switch to Reaper (going off of window titles?) and send key combination Ctrl+R (or whatever it is) to stop recording." (I guess by default, space is stop, but the possibility for key combinations--even a few combos in succession, maybe with "sleep" pauses in between, would maximize flexibility for other possible uses)

If anyone can help with this without too much trouble (or knows of something similar and portable), it'd be much appreciated!  Thanks!

*edit* - it's got a name now!
« Last Edit: February 25, 2010, 02:14 AM by Onesimus Prime »

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #1 on: February 18, 2010, 01:23 AM »
This is the perfect scenario for a simple personal AutoHotkey script.  http://www.autohotkey.com  The commands you're going to want to look up are Sleep, Send, WinActivate, and WinWaitActive.  If you're not able to write it yourself, let us know and we'll help out.
« Last Edit: February 18, 2010, 01:28 AM by skwire »

Onesimus Prime

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 15
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #2 on: February 18, 2010, 01:33 AM »
I forgot about "WinActivate and WinWaitActive" - I'll look into those.  I think I've used the other commands before. Thanks!

Oh, I forgot--will this still work if the computer's "locked"?
« Last Edit: February 18, 2010, 01:36 AM by Onesimus Prime »

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,287
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #3 on: February 18, 2010, 03:24 AM »
I forgot about "WinActivate and WinWaitActive" - I'll look into those.  I think I've used the other commands before. Thanks!

Oh, I forgot--will this still work if the computer's "locked"?
-Onesimus Prime (February 18, 2010, 01:33 AM)

Doubtful.

Onesimus Prime

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 15
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #4 on: February 18, 2010, 04:09 AM »
I so should have been sleeping... *chagrined*
Okay, I first wrote a simple one like what you described, skwire--about 4 lines is all it takes, I didn't realize it'd be so simple!  But then I couldn't leave well enough alone...  I did a forum search, grabbing the countdown timer code from https://www.donation...ex.php?topic=14895.0 and integrating the use of an .ini file (thanks to Skrommel's stuff), and the below is the (seemingly working) monstrosity that resulted  ;D :
applicationname=WaitAndSend
versionnumber=0.5

; by Onesimus Prime
; 2009
; For now, right-click on the tray icon to exit...

; Countdown timer code (which is the majority of what's below) from

https://www.donationcoder.com/forum/index.php?topic=14895.0
; Some .ini-related code from Skrommel's templates

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey

releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and

reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#persistent

IfNotExist,%applicationname%.ini
{
  ini=[Settings]
  ini=%ini%`n
  ini=%ini%`nctimer=20
  ini=%ini%`n  `; This sets the countdown, unless it is over-ridden by

launching the script with a numeric parameter.
  ini=%ini%`n  `; It is measured in minutes (and decimals are valid).
  ini=%ini%`n
  ini=%ini%`ntargetprogram=ahk_class REAPERwnd
  ini=%ini%`n  `; this is the Title or Class of the window of the target

program, the program that you wish to send the below keystrokes to. 
  ini=%ini%`n  `; By default, this line includes "ahk_class REAPERwnd",

summoning a window with the class matching the REAPER recording program.
  ini=%ini%`n
  ini=%ini%`nkeypresses={Space}
  ini=%ini%`n  `;the keystrokes here will be sent to the program listed above.

 Your options, and the various formatting restrictions, are viewable at

http://www.autohotkey.com/docs/commands/Send.htm
  ini=%ini%`n  `; By default, this line simulates a press of the Spacebar

(using {Space}),  which (by Reaper's defaults) toggles "play" and "stop" (and

thus can be used to stop recording!)
  ini=%ini%`n  `; You could also use {CTRLDOWN}r{CTRLUP} to send Ctrl+R,

Reaper's default key combo to START recording.
  FileAppend,%ini%,%applicationname%.ini
  ini=
}

IniRead, ctimer, %applicationname%.ini, Settings, ctimer

IniRead, targetprogram, %applicationname%.ini, Settings, targetprogram
IniRead, keypresses, %applicationname%.ini, Settings, keypresses

CoordMode, ToolTip, Screen
tt = %1%
if not tt
   tt = %ctimer%
;tt = 120
time := Convert_Milliseconds(tt)
Sec := (tt * 60)

SetTimer, nMessage, %time%
countdown(Sec)
return

nMessage:
SetTimer, nMessage, Off

WinActivate %targetprogram%
WinWaitActive %targetprogram%
send %keypresses%
sleep 1000
; MsgBox Your keypresses have been sent to the requested program.
Gui, +MinimizeBox
GUi, -sysmenu
gui, +toolwindow
Gui, Add, Button, w130, DONE - hit 'O' for OK
Gui, Show, x1 y1, %applicationname% %versionnumber%

KeyWait, o, D
Tooltip ,, 2, 980
Gui Destroy
exitapp


Convert_MilliSeconds(time,MS = 1000,M = 60)
{
   return MS*M*time
}

countdown(p_count="") {
   countdown_time:=p_count
   countdown_date=16010101
   countdown_date+=%countdown_time%, s
   Loop {
      If (countdown_date >= 16010101010000){
        FormatTime, mmss, %countdown_date%, hh:mm:ss
     }else{
      FormatTime, mmss, %countdown_date%, mm:ss
      }
      ;msgbox %countdown_date%`n%hhmmss%
     ;//SB_SetText(mmss, 2)
      Tooltip, %mmss%, 2000,14
      Menu,Tray,Tip,Time Remaining: %mmss%
      Sleep, 1000
      countdown_date+=-1, s
      ;//IfLess, countdown_date, 16010101, break
      if (A_Index>countdown_time) {
         break
      }
   }
}

You were right about a "locked" computer--it seems to wait until the computer is unlocked before executing the after-timer stuff.  One workaround, I guess, might be to use KidSafe Portable to "lock" the computer instead.  "Windows + L can cut off some processes (e.g. it always pauses my virus scanner and media player, and turns off my Internet); KidSafe doesn’t do that" (according to a quote here).

Onesimus Prime

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 15
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #5 on: February 18, 2010, 05:41 AM »
Okay, in the last section I'm trying to make the floating tooltip that continuously counts down optional.
countdown(p_count="") {
   countdown_time:=p_count
   countdown_date=16010101
   countdown_date+=%countdown_time%, s
   Loop {
      If (countdown_date >= 16010101010000){
        FormatTime, mmss, %countdown_date%, hh:mm:ss
     }else{
      FormatTime, mmss, %countdown_date%, mm:ss
      }
      ;msgbox %countdown_date%`n%hhmmss%
     ;//SB_SetText(mmss, 2)
   global floatingtimer
      ifequal, %floatingtimer%, 1, Tooltip, %mmss%, 2000,14
      Menu,Tray,Tip,Time Remaining: %mmss%
      Sleep, 1000
      countdown_date+=-1, s
      ;//IfLess, countdown_date, 16010101, break
      if (A_Index>countdown_time) {
         break
      }
   }
}

If I read the variable with
      IniRead, floatingtimer, %applicationname%.ini, Settings, floatingtimer

then why doesn't
      global floatingtimer
      ifequal, %floatingtimer%, 1, Tooltip, %mmss%, 2000,14
seem to have any effect?  The variable has the right value, but the timer tooltip still isn't showing up...

*yet another edit*
Okay, I think I got it - not because I necessarily understand what's going on, but because I looked at the .chm a bit more and kept on trying things until something worked!
applicationname=WaitAndSend
versionnumber=0.6

; by Onesimus Prime
; 2009
; For now, right-click on the tray icon to exit...

; Countdown timer code (which is the majority of what's below) from https://www.donationcoder.com/forum/index.php?topic=14895.0
; Some .ini-related code from Skrommel's templates

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#persistent

IfNotExist,%applicationname%.ini
{
  ini=[Settings]
  ini=%ini%`n
  ini=%ini%`nfloatingtimer=0
  ini=%ini%`n  `; 0 is false (no), 1 is true (yes).
  ini=%ini%`n  `; Even without a floating timer, you can mouse over the tray icon to get the current countdown.
  ini=%ini%`n
  ini=%ini%`nctimer=20
  ini=%ini%`n  `; This sets the countdown, unless it is over-ridden by launching the script with a numeric parameter.
  ini=%ini%`n  `; It is measured in minutes (and decimals are valid).
  ini=%ini%`n
  ini=%ini%`ntargetprogram=ahk_class REAPERwnd
  ini=%ini%`n  `; this is the Title or Class of the window of the target program, the program that you wish to send the below keystrokes to. 
  ini=%ini%`n  `; By default, this line includes "ahk_class REAPERwnd", summoning a window with the class matching the REAPER recording program.
  ini=%ini%`n
  ini=%ini%`nkeypresses={Space}
  ini=%ini%`n  `;the keystrokes here will be sent to the program listed above.  Your options, and the various formatting restrictions, are viewable at http://www.autohotkey.com/docs/commands/Send.htm
  ini=%ini%`n  `; By default, this line simulates a press of the Spacebar(using {Space}),  which (by Reaper's defaults) toggles "play" and "stop" (and thus can be used to stop recording!)
  ini=%ini%`n  `; You could also use {CTRLDOWN}r{CTRLUP} to send Ctrl+R, Reaper's default key combo to START recording.
  ini=%ini%`n
  ini=%ini%`nendmsg=0
  ini=%ini%`n  `; This determines whether to show a message box after sending the keypresses, or simply to exit quietly.
  ini=%ini%`n  `; 0 is false (no MsgBox), 1 is true (yes).
  FileAppend,%ini%,%applicationname%.ini
  ini=
}

IniRead, floatingtimer, %applicationname%.ini, Settings, floatingtimer

IniRead, ctimer, %applicationname%.ini, Settings, ctimer

IniRead, targetprogram, %applicationname%.ini, Settings, targetprogram
IniRead, keypresses, %applicationname%.ini, Settings, keypresses
IniRead, endmsg, %applicationname%.ini, Settings, endmsg

CoordMode, ToolTip, Screen
tt = %1%
if not tt
   tt = %ctimer%
;tt = 120
time := Convert_Milliseconds(tt)
Sec := (tt * 60)

SetTimer, nMessage, %time%
countdown(Sec)
return

nMessage:
SetTimer, nMessage, Off

WinActivate %targetprogram%
WinWaitActive %targetprogram%
send %keypresses%
sleep 1000
ifequal, endmsg, 1, MsgBox Your keypresses have been sent to the requested program.
;Gui, +MinimizeBox
;GUi, -sysmenu
;gui, +toolwindow
;Gui, Add, Button, w130, DONE - hit 'O' for OK
;Gui, Show, x1 y1, %applicationname% %versionnumber%

;KeyWait, o, D
;Tooltip ,, 2, 980
;Gui Destroy

exitapp


Convert_MilliSeconds(time,MS = 1000,M = 60)
{
   return MS*M*time
}

countdown(p_count="") {
   countdown_time:=p_count
   countdown_date=16010101
   countdown_date+=%countdown_time%, s
   Loop {
      If (countdown_date >= 16010101010000){
        FormatTime, mmss, %countdown_date%, hh:mm:ss
     }else{
      FormatTime, mmss, %countdown_date%, mm:ss
      }
      ;msgbox %countdown_date%`n%hhmmss%
     ;//SB_SetText(mmss, 2)
      global floatingtimer
      ifequal, floatingtimer, 1, Tooltip, %mmss%, 2000,14
      Menu,Tray,Tip,Time Remaining: %mmss%
      Sleep, 1000
      countdown_date+=-1, s
      ;//IfLess, countdown_date, 16010101, break
      if (A_Index>countdown_time) {
         break
      }
   }
}
« Last Edit: February 18, 2010, 05:53 AM by Onesimus Prime »

Onesimus Prime

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 15
    • View Profile
    • Donate to Member
Re: IDEA: timer/keyboard macro combo program...
« Reply #6 on: February 25, 2010, 01:17 AM »
Heh - I'm making my own snax!  :lol:

You define which keypresses to send to which program in the .ini file (generated on first run).  You can copy & rename the .exe or script, have a matching-named .ini file (also automagically generated), and therefore run multiple unique instances at the same time.

There are 3 ways to set the countdown timer 'til when the above-mentioned keypresses are sent.  In order of precedence:
1) command-line parameter (I use this script through Executor); 2) user input via initial input box; 3) the 'ctimer' field in the .ini file.

Enjoy!
*edit* I hope to have an actual icon for this, maybe by sometime next week...

applicationname=WaitAndSend

versionnumber=0.5.4

; by Onesimus Prime
; 2010
; For now, right-click on the tray icon to exit...

; Countdown timer code (which is the majority of what's below) from https://www.donationcoder.com/forum/index.php?topic=14895.0
; Some .ini-related code from Skrommel's templates

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#persistent

; gets current script name and 'ditches' the file extension so that each script instance can have its own .ini file...
CurrentScriptNm=%A_ScriptName%
stringGetPos, WhereExtension, CurrentScriptNm, ., R
stringLeft, CurrentScriptNm, CurrentScriptNm, WhereExtension
; Msgbox %CurrentScriptNm% ;error-checking

IfNotExist,%CurrentScriptNm%.ini
{
  ini=[Settings]
  ini=%ini%`n
  ini=%ini%`nfloatingtimer=1
  ini=%ini%`n  `; 0 is false (no), 1 is true (yes).
  ini=%ini%`n  `; Even without a floating timer, you can mouse over the tray icon to get the current countdown.
  ini=%ini%`n
  ini=%ini%`nctimer=20
  ini=%ini%`n  `; This sets the countdown timer (in minutes), unless it is over-ridden by launching the script with a numeric parameter or by entering a number into the program's initial input box.
  ini=%ini%`n  `; Therefore the order of precedence is 1) command-line parameter; 2) user input via initial input box; 3) the above 'ctimer' field in this .ini file.
  ini=%ini%`n  `; It is measured in minutes (and decimals are valid).
  ini=%ini%`n
  ini=%ini%`ntargetprogram=ahk_class mp3DCWndClass
  ini=%ini%`n  `; This is the Title or Class of the window of the target program, the program that you wish to send the below keystrokes to.  
  ini=%ini%`n  `; By default, this line includes "ahk_class mp3DCWndClass", summoning a window with the class matching mp3DirectCut (mp3DC).
  ini=%ini%`n  `; Another option would be "ahk_class REAPERwnd", summoning a window with the class matching the REAPER recording program.
  ini=%ini%`n
  ini=%ini%`nkeypresses={Space}
  ini=%ini%`n  `; The keystrokes here will be sent to the program listed above.  Your options, and the various formatting restrictions, are viewable at http://www.autohotkey.com/docs/commands/Send.htm
  ini=%ini%`n  `; By default, this line simulates a press of the Spacebar(using {Space}),  which (by mp3DC's and Reaper's defaults) toggles "play" and "stop" (and thus can be used to stop recording!)
  ini=%ini%`n  `; You could also use r{Space} to send r, then Space, which should (hopefully) start recording in mp3DC;
  ini=%ini%`n  `; or {CTRLDOWN}r{CTRLUP} to send Ctrl+R, Reaper's default key combo to START recording.
  ini=%ini%`n
  ini=%ini%`nendmsg=0
  ini=%ini%`n  `; This determines whether to show a message box after sending the keypresses, or simply to exit quietly.
  ini=%ini%`n  `; 0 is false (no MsgBox), 1 is true (yes).
  FileAppend,%ini%,%CurrentScriptNm%.ini
  ini=
}

IniRead, floatingtimer, %CurrentScriptNm%.ini, Settings, floatingtimer

IniRead, ctimer, %CurrentScriptNm%.ini, Settings, ctimer

IniRead, targetprogram, %CurrentScriptNm%.ini, Settings, targetprogram
IniRead, keypresses, %CurrentScriptNm%.ini, Settings, keypresses
IniRead, endmsg, %CurrentScriptNm%.ini, Settings, endmsg



CoordMode, ToolTip, Screen
tt = %1%

if not tt
{
 InputBox, InputMin, Enter timer length, `nPlease enter below how many minutes the timer `nshould be set for. (Decimals are okay.) `nIf this is left blank`, the value set via the .ini file `n(currently %ctimer% minutes) will be used.,,,185
 if InputMin is number
 {
    tt = %InputMin%
 }
}

if not tt
   tt = %ctimer%
;tt = 120
time := Convert_Milliseconds(tt)
Sec := (tt * 60)

SetTimer, nMessage, %time%
countdown(Sec)
return

nMessage:
SetTimer, nMessage, Off

WinActivate %targetprogram%
WinWaitActive %targetprogram%
send %keypresses%
sleep 1000
ifequal, endmsg, 1, MsgBox Your keypresses have been sent to the requested program.
;Gui, +MinimizeBox
;GUi, -sysmenu
;gui, +toolwindow
;Gui, Add, Button, w130, DONE - hit 'O' for OK
;Gui, Show, x1 y1, %CurrentScriptNm% %versionnumber%

;KeyWait, o, D
;Tooltip ,, 2, 980
;Gui Destroy

exitapp


Convert_MilliSeconds(time,MS = 1000,M = 60)
{
   return MS*M*time
}

countdown(p_count="") {
   countdown_time:=p_count
   countdown_date=16010101
   countdown_date+=%countdown_time%, s
   Loop {
      If (countdown_date >= 16010101010000){
        FormatTime, mmss, %countdown_date%, hh:mm:ss
     }else{
      FormatTime, mmss, %countdown_date%, mm:ss
      }
      ;msgbox %countdown_date%`n%hhmmss%
     ;//SB_SetText(mmss, 2)
      global floatingtimer
      ifequal, floatingtimer, 1, Tooltip, %mmss%, 2000,14
      Menu,Tray,Tip,Time Remaining: %mmss%
      Sleep, 1000
      countdown_date+=-1, s
      ;//IfLess, countdown_date, 16010101, break
      if (A_Index>countdown_time) {
         break
      }
   }
}