Skip to content

Instantly share code, notes, and snippets.

@nylen
Created July 22, 2010 13:21
Show Gist options
  • Save nylen/485956 to your computer and use it in GitHub Desktop.
Save nylen/485956 to your computer and use it in GitHub Desktop.
#include <Misc.au3>
#include "__config.au3"
#NoTrayIcon
Const $LOOP_DELAY = 200 ; milliseconds
Const $WIN_DELAY = 20
Const $WIN_TITLE_TEXT_DELIM = "*|*"
FileChangeDir($AUTOMATION_SYSTEM_PATH)
Opt("WinWaitDelay", $WIN_DELAY)
$user32 = DllOpen("user32.dll")
Func OneProcessExists($pl, $ps)
For $fi = 1 To $pl[0][0]
If StringInStr("|" & $pl[$fi][0] & "|", "|" & $ps & "|") Then Return 1
Next
Return 0
EndFunc ;==>OneProcessExists
Func WinExists_($str)
If StringInStr($str, $WIN_TITLE_TEXT_DELIM) Then
$title = StringLeft($str, StringInstr($str, $WIN_TITLE_TEXT_DELIM)-1)
$text = StringMid($str, StringInStr($str, $WIN_TITLE_TEXT_DELIM)+StringLen($WIN_TITLE_TEXT_DELIM))
If Not BitAND(WinGetState($title, $text), 2) Then Return 0
Return WinExists($title, $text)
Else
If Not BitAND(WinGetState($str), 2) Then Return 0
Return WinExists($str)
EndIf
EndFunc
Func WinActive_($str)
$not = 0
If StringLeft($str, 1) = "!" Then
$not = 1
$str = StringMid($str, 2)
EndIf
$active = 0
If StringInStr($str, $WIN_TITLE_TEXT_DELIM) Then
$title = StringLeft($str, StringInstr($str, $WIN_TITLE_TEXT_DELIM)-1)
$text = StringMid($str, StringInStr($str, $WIN_TITLE_TEXT_DELIM)+StringLen($WIN_TITLE_TEXT_DELIM))
$active = WinActive($title, $text)
Else
$active = WinActive($str)
EndIf
Return _Iif($not, Not($active), $active)
EndFunc
Func RunAsTempScript($str)
$f = FileOpen("tmp.au3", 2)
If @error Then MsgBox(48, "Automation Error", "Could not open temporary script")
FileWrite($f, $str & @CRLF)
If @error Then MsgBox(48, "Automation Error", "Could not write temporary script")
FileClose($f)
Run('"'&@AutoItExe&'" tmp.au3', $USER_COMMAND_PATH)
If @error Then MsgBox(48, "Automation Error", "Could not run generated temporary script")
EndFunc
Func RunCommand($str)
$working_dir = $AUTOMATION_SYSTEM_PATH
If Not FileExists($str) Then ; check system dir
If FileExists($USER_COMMAND_PATH & "\" & $str) Then ; check user dir
$str = $USER_COMMAND_PATH & "\" & $str
$working_dir = $USER_COMMAND_PATH
Else
MsgBox(48, "Automation Error", "Could not find command '" & $str & "'.")
Return
EndIf
EndIf
$str = '"' & $str & '"'
If StringLower(StringRight($str, 5)) = '.au3"' Then
$str = '"' & @AutoItExe & '" ' & $str
EndIf
Run($str, $working_dir)
If @error Then MsgBox(48, "Automation Error", "Could not run " & $str)
EndFunc
#include "actions.au3"
If WinExists("Au3.Main") Then
Run('tray.exe "Automation" "Main program already started" 1 2')
Exit
EndIf
Run('tray.exe "Automation" "Main program started" 1 1')
AutoItWinSetTitle("Au3.Main")
$cwt = 0;WinGetTitle("[ACTIVE]")
$cwh = 0;WinGetHandle("[ACTIVE]")
; main loop
While 1
$owh = $cwh
$owt = $cwt
$cwh = WinGetHandle("[ACTIVE]")
$cwt = WinGetTitle($cwh)
$pid = WinGetProcess($cwh)
If $cwt <> $owt Or $cwh <> $owh Then
; something changed
; TODO: currently, window-activated or program-activated actions will not
; run until the active window changes
For $i = 0 To $n_WindowActivated - 1
$should_run = 0
If $WindowActivated_RequireActive[$i] Then
$should_run = WinActive_($WindowActivated_Triggers[$i])
Else
$should_run = WinExists_($WindowActivated_Triggers[$i])
EndIf
If $should_run Then
If $WindowActivated_CommandTypes[$i] = "Command" Then
RunCommand($WindowActivated_Commands[$i])
ElseIf $WindowActivated_CommandTypes[$i] = "Script" Then
RunAsTempScript($WindowActivated_Commands[$i])
EndIf
EndIf
Next
For $i = 0 To $n_ProgramActivated - 1
$should_run = 0
If $ProgramActivated_RequireActive[$i] Then
$should_run = WinActive_($ProgramActivated_Triggers[$i])
Else
$should_run = WinExists_($ProgramActivated_Triggers[$i])
EndIf
If $should_run Then
If $ProgramActivated_CommandTypes[$i] = "Command" Then
RunCommand($ProgramActivated_Commands[$i])
ElseIf $ProgramActivated_CommandTypes[$i] = "Script" Then
RunAsTempScript($ProgramActivated_Commands[$i])
EndIf
EndIf
Next
If $n_ProgramSpecific > 0 Then
$p = ProcessList()
EndIf
For $i = 0 To $n_ProgramSpecific - 1
$h = 0
; TODO: move some of this logic into a function
If StringLeft($ProgramSpecific_ScopePrograms[$i], 1) = "!" Then
$h = 1
For $j = 1 To $p[0][0]
If $pid = $p[$j][1] And StringInStr("|" & StringMid($ProgramSpecific_ScopePrograms[$i], 2) & "|", "|" & $p[$j][0] & "|") Then $h = 0
Next
ElseIf OneProcessExists($p, $ProgramSpecific_ScopePrograms[$i]) Then
$h = 0
For $j = 1 To $p[0][0]
If $pid = $p[$j][1] And StringInStr("|" & $ProgramSpecific_ScopePrograms[$i] & "|", "|" & $p[$j][0] & "|") Then $h = 1
Next
EndIf
If $h = 0 Then
HotKeySet($ProgramSpecific_Hotkeys[$i])
Else
HotKeySet($ProgramSpecific_Hotkeys[$i], "ProgramSpecificHotkey_" & $i)
EndIf
Next
For $i = 0 To $n_WindowSpecific - 1
If WinActive_($WindowSpecific_ScopeWindows[$i]) Then
HotKeySet($WindowSpecific_Hotkeys[$i], "WindowSpecificHotkey_" & $i)
Else
HotKeySet($WindowSpecific_Hotkeys[$i])
EndIf
Next
EndIf
Sleep($LOOP_DELAY)
If WinExists("Au3.CloseMain") Then ExitLoop
WEnd
Run('tray.exe "Automation" "Main program stopped" 1 3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment