walle Posted May 4, 2008 Posted May 4, 2008 This little nasty peace of code should shutdown the computer if it been inactive for more than 15 min. Before the shutdown sequence it should also save all open Word docs. So far so good. But if none Word documents are open after 15 min it screws ups, and I can't figure out why. Lend me a hand plz! expandcollapse popup#NoTrayIcon #include <Word.au3> AutoItSetOption("WinTitleMatchMode", 4) Dim $last_active = 0 Dim $timer = TimerInit() $i = 0 While (1) $not_idle = _CheckIdle($last_active) If $not_idle <> 0 Then $timer = TimerInit() Sleep(2000) If Int(TimerDiff($timer)/1000) >= 900 Then ; For each Word window $oDoc = "" $oWordApp = "" $handle = WinGetHandle("classname=OpusApp", "") If $handle = "" Then ExitLoop $oWordApp = _WordAttach($handle, "HWND") If Not @error Then ; For each DOC $colDocs = _WordDocGetCollection($oWordApp, -1) If @error = 0 Then For $oDoc In $colDocs RegWrite('HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce' , 'WordDoc' & $i, 'REG_SZ', '"' & @ScriptDir & "\Test" & $i & ".doc" & '"') _WordDocSave($oDoc) If @error = 1 Then ; Check if savepath is selected. _WordDocSaveAs($oDoc, @ScriptDir & "\Test" & $i & ".doc") $i += 1 EndIf Next Else ;MsgBox(16, "Error", "Error retreiving collection of Docs") ExitLoop EndIf Else ;MsgBox(16, "Error", "Error attaching to Word window by HWND") ExitLoop EndIf _WordQuit($oWordApp) If $I > 0 Then RegWrite('HKCU\Software\Auto shutdown' , 'Auto Shutdown', 'REG_SZ', $i) EndIf Shutdown(9) EndIf WEnd shutdown(9) Func _CheckIdle(ByRef $last_active, $start = 0) $struct = DllStructCreate("uint;dword"); DllStructSetData($struct, 1, DllStructGetSize($struct)); If $start Then DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) $last_active = DllStructGetData($struct, 2) Return $last_active Else DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) If $last_active <> DllStructGetData($struct, 2) Then Local $save = $last_active $last_active = DllStructGetData($struct, 2) Return $last_active - $save EndIf EndIf EndFunc;==>_CheckIdle
martin Posted May 4, 2008 Posted May 4, 2008 This little nasty peace of code should shutdown the computer if it been inactive for more than 15 min. Before the shutdown sequence it should also save all open Word docs. So far so good. But if none Word documents are open after 15 min it screws ups, and I can't figure out why. Lend me a hand plz! expandcollapse popup#NoTrayIcon #include <Word.au3> AutoItSetOption("WinTitleMatchMode", 4) Dim $last_active = 0 Dim $timer = TimerInit() $i = 0 While (1) $not_idle = _CheckIdle($last_active) If $not_idle <> 0 Then $timer = TimerInit() Sleep(2000) If Int(TimerDiff($timer)/1000) >= 900 Then ; For each Word window $oDoc = "" $oWordApp = "" $handle = WinGetHandle("classname=OpusApp", "") If $handle = "" Then ExitLoop $oWordApp = _WordAttach($handle, "HWND") If Not @error Then ; For each DOC $colDocs = _WordDocGetCollection($oWordApp, -1) If @error = 0 Then For $oDoc In $colDocs RegWrite('HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce' , 'WordDoc' & $i, 'REG_SZ', '"' & @ScriptDir & "\Test" & $i & ".doc" & '"') _WordDocSave($oDoc) If @error = 1 Then ; Check if savepath is selected. _WordDocSaveAs($oDoc, @ScriptDir & "\Test" & $i & ".doc") $i += 1 EndIf Next Else ;MsgBox(16, "Error", "Error retreiving collection of Docs") ExitLoop EndIf Else ;MsgBox(16, "Error", "Error attaching to Word window by HWND") ExitLoop EndIf _WordQuit($oWordApp) If $I > 0 Then RegWrite('HKCU\Software\Auto shutdown' , 'Auto Shutdown', 'REG_SZ', $i) EndIf Shutdown(9) EndIf WEnd shutdown(9) Func _CheckIdle(ByRef $last_active, $start = 0) $struct = DllStructCreate("uint;dword"); DllStructSetData($struct, 1, DllStructGetSize($struct)); If $start Then DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) $last_active = DllStructGetData($struct, 2) Return $last_active Else DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct)) If $last_active <> DllStructGetData($struct, 2) Then Local $save = $last_active $last_active = DllStructGetData($struct, 2) Return $last_active - $save EndIf EndIf EndFunc;==>_CheckIdle I tried it by removing the if loop If Int(TimerDiff($timer)/1000) >= 900 Then . .. endif so I didn't need to wait for the PC to be idle. I had no word docs open. I stepped through the code and it exited the while loop fine and got the the shutdown(9) line which I didn't execute. So I can't see what the problem is and I think you need to add more debugging. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
walle Posted May 4, 2008 Author Posted May 4, 2008 I have a bad day, so it's really frustrating. If you don't remove the If-clause and just change the 900 seconds till 5 second, does it still work? You mentioned debugging, could I somehow check what line it gets stuck at?
martin Posted May 4, 2008 Posted May 4, 2008 I have a bad day, so it's really frustrating. If you don't remove the If-clauseand just change the 900 seconds till 5 second, does it still work?You mentioned debugging, could I somehow check what line it gets stuck at?If instead of removing the if loop I added a line before the if line like this$timer = 0so that the time difference is bound to be more than 15 minutes, then it still works fine and doesn't get stuck for me. You would need to debug it on your own PC. I used my own debugger as the link in my signature. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
walle Posted May 5, 2008 Author Posted May 5, 2008 If instead of removing the if loop I added a line before the if line like this$timer = 0so that the time difference is bound to be more than 15 minutes, then it still works fine and doesn't get stuck for me. You would need to debug it on your own PC. I used my own debugger as the link in my signature.Thanks for your help Martin!Tried the script this morning, and guess what, it worked?Haven't changed anything
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now