leuce Posted March 5, 2006 Share Posted March 5, 2006 G'day everyoneI'm sure my problem is a simple one to solve, but try as I might, I can't figure it out myself. I want to run a script for ever (until I kill it) which enables two hotkeys to be pressed at any time, at which time a certain set of script instructions are followed.It is for a "text editor" (roughly speaking, to keep it simple) and a spell-checker. The spell-checker takes quite a bit of time to load at first, so it's better to keep the spell-checker running continuously and simply activate the spell-checker once the text to be spell-checked has been copied to the clipboard.Here is my code:; Script Function:; Does a spell-check on some text from OmegaT (the text editor); in Kastrul (the spell-checker). The script should run in the; background forever, and two functions should be activated by; two hotkeys. The user will always use Ctrl+Enter, but may; sometimes not use Alt+Enter first before using Ctrl+Enter again.; Let the script stay resident in memory and let the two hotkeys; be active... I can't figure out if the HotKeySet must come before; or after or inbetween the while-loop.While 1Sleep (100)WEndHotKeySet ("+{ENTER}", "DoSpellCheck")HotKeySet ("!{ENTER}", "UnSpellCheck"); When Ctrl+Enter is pressed, wait for OmegaT window to be active; (it should be already), do Ctrl+A (highlight all), do Ctrl+C (copy),; activate Kastrul (the spell-checker), wait for it to be active,; wait 100 cycles, do Ctrl+V (paste, which in Kastrul activates the; spell-checker).Func DoSpellCheck()WinWaitActive("OmegaT", "")Send("+a")Send("+c")WinActivate ("Kastrul", "")WinWaitActive("Kastrul", "")Sleep(100)Send("+v")EndFunc; When Alt+Enter is pressed, activate the OmegaT window, and wait; for it to become active.Func UnSpellCheck()WinActivate ("OmegaT", "")WinWaitActive("OmegaT", "")EndFuncAny help is appreciated. The help files tell me how to set hotkeys and how to keep a script running indefinitely, but not how to do both, and the examples given seem to be for scripts that exit once the hotkey has been pressed.One alternative might be to forget about the endless loop, and simply let the script restart itself when it ends... what do you think? Not very elegant, though. Link to comment Share on other sites More sharing options...
cdkid Posted March 5, 2006 Share Posted March 5, 2006 HotKeySet("+{ENTER}","DoSpellCheck") HotKeySet("!{ENTER}","UnSpellCheck") While 1 sleep(1000000) WEnd Func UnSpellCheck() WinActivate ("OmegaT", "") WinWaitActive("OmegaT", "") EndFunc Func DoSpellCheck() WinWaitActive("OmegaT", "") Send("+a") Send("+c") WinActivate ("Kastrul", "") WinWaitActive("Kastrul", "") Sleep(100) Send("+v") EndFunc that ought to do it ~cdkid AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide! Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 I'm sure my problem is a simple one to solve, but try as I might, I can't figure it out myself....Well, this is embarrasing. I tried to use + for Ctrl instead of ^. Duh...As for the eternal loop, it seems that the following does the trick nicely:While 1HotKeySet ("^{ENTER}", "DoSpellCheck")HotKeySet ("!{ENTER}", "UnSpellCheck")WEndMy next project will be to use the same hotkey for both functions (an if statement ought to do it... if I can figure out where to put it). Link to comment Share on other sites More sharing options...
cdkid Posted March 5, 2006 Share Posted March 5, 2006 i wouldnt put the hotkeys IN the While statement, put them b4 and make sure you havea sleep (of at least 10) in your while loop, otherwise it really chews up memory/CPU Usage ~cdkid AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide! Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 My next project will be to use the same hotkey for both functions (an if statement ought to do it... if I can figure out where to put it).Easy... did it with Case statements:While 1$var = 0If WinActive ("OmegaT", "") Then $var = 1If WinActive ("Kastrul", "") Then $var = 2Select Case $var = 1 HotKeySet ("^{ENTER}", "DoSpellCheck") Case $var = 2 HotKeySet ("^{ENTER}", "UnSpellCheck")EndSelectWEnd Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 I wouldnt put the hotkeys IN the While statement ... it really chews up memory/CPU Usage.Thanks... which is better on the CPU (and other factors) -- a high sleep number (10000) or a low sleep number (10)? Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 Hmmm, if i put the While-WEnd statement separately, the following Case statement method doesn't work:$var = 0If WinActive ("OmegaT", "") Then $var = 1If WinActive ("Kastrul", "") Then $var = 2Select Case $var = 1 HotKeySet ("^{ENTER}", "DoSpellCheck") Case $var = 2 HotKeySet ("^{ENTER}", "UnSpellCheck")EndSelectWhile 1Sleep (100)WEndAnyone got any other ideas for me? Link to comment Share on other sites More sharing options...
cdkid Posted March 5, 2006 Share Posted March 5, 2006 (edited) higher, and if you only have the WHILE...WEnd there for keeping the script going i'd do like While 1 sleep(10000000) WEnd Edited March 5, 2006 by cdkid AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide! Link to comment Share on other sites More sharing options...
greenmachine Posted March 5, 2006 Share Posted March 5, 2006 If you make the loop outside of the Select statement, maybe you should use if WinExists rather than WinActive. With the current setup, you most likely don't have the window active in time for when the script runs through that line, and it doesn't change the $var from 0. Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 Anyone got any other ideas for me?Okay, I solved it by using only one function, and placing the If...ElseIf statement inside the function:HotKeySet ("^{ENTER}", "SpellCheck")While 1Sleep (100)WEndFunc SpellCheck()If WinActive ("OmegaT", "") ThenWinWaitActive ("OmegaT", "")Send ("^a")Send ("^c")WinActivate ("Kastrul", "")WinWaitActive ("Kastrul", "")Sleep (100)Send ("^v")ElseIf WinActive ("Kastrul", "") ThenWinActivate("OmegaT", "")WinWaitActive("OmegaT", "")EndIfEndFunc Link to comment Share on other sites More sharing options...
leuce Posted March 5, 2006 Author Share Posted March 5, 2006 If you make the loop outside of the Select statement, maybe you should use if WinExists rather than WinActive.Hmm, I'm not sure if that'll work, since both windows do exist all the time that the script runs, but only one of them is active. Link to comment Share on other sites More sharing options...
greenmachine Posted March 5, 2006 Share Posted March 5, 2006 Ok, I didn't know both existed all the time. I was just saying that because normally when I run a script, Scite is the active window if not compiled, and windows explorer (for the folder) is active when it is compiled. So I thought that your script wasn't getting the right active window because it checked too fast for you to react. Well it looks like you figured out what you wanted to do, so I don't need to make any more suggestions. Link to comment Share on other sites More sharing options...
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