160 likes | 349 Views
Week4. If Statements. Things I’ve Used AutoIt for recently. I created a Mute Button for my PC using HotKeys . Now I can control the volume from my keyboard I created this code for drawing randomly in panit . Something Fun – Open Paint . HotKeySet ("{ESC}", "Quit")
E N D
Week4 If Statements
Things I’ve Used AutoIt for recently • I created a Mute Button for my PC using HotKeys. Now I can control the volume from my keyboard • I created this code for drawing randomly in panit.
Something Fun – Open Paint HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "MoveMouse") for $i = 0 to 500 TrayTip("", $i, 1) sleep(10) Next FuncMoveMouse() for $i = 0 to 1000 MouseClickDrag("left",random(0,500)+200,random(0,500)+200,random(0,500)+200,random(0,500)+200,0) next EndFunc Func Quit() Exit EndFunc
Email Me • Email me for attendance. • Let me know how this class is going for you. Too fast, too slow. • Do you want homework assignments, small ones? Would this help you? • What can I do to help you?
New Skeleton for future Programs HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") While 1 Sleep(100) Wend Func Start() ;MainCode EndFunc Func Quit() Exit EndFunc
Goal of this Class • Let’s make a program that let’s us access our favorite programs quickly
If-Statements • Remember If-Statements? • http://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm • Remember == is not the same as = If ($parameter…) Then … EndIf
;All future code should include this template http://cl1p.net/123456789 HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") While 1 Sleep(100) Wend Func Start() $val = inputbox("","Type in the #Notepad") If $val == 1 OR $val == 2 Then run("notepad.exe") EndIf EndFunc Func Quit() Exit EndFunc
Let’s modify this to have two programs open: • If someone types “notepad”, Notepad opens. • If someone types “firefox”, Firefox opens • Otherwise repeat the function • To runFirefox, you must locate the exact location of the executable (.exe) file. run("C:\Program Files\Mozilla Firefox\firefox.exe") • To call a function again just type it Start()
Solution to Previous Exercise ;All future code should include this template http://cl1p.net/123456789 HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") While 1 Sleep(100) Wend Func Start() $val = inputbox("","Type in your Program") If $val == "notepad" Then run("notepad.exe") ElseIf $val == "firefox" Then run("C:\Program Files\Mozilla Firefox\firefox.exe") Else Traytip("Wrong Input", $val, 10) Start() EndIf EndFunc Func Quit() Exit EndFunc
Closing Programs • Let’s make a hotkey “{F1}” that closes NotePad or Firefox if it exits • Check if the Window Exists with WinActive • Close a Window with WinClose • Find the Class/Title of the program with • Title is the title of the program’s window • Class is the type of program’s window
Solution to the Previous Problem ;All future code should include this template http://cl1p.net/123456789 HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") HotKeySet("{F1}","CloseWindow") While 1 Sleep(100) Wend Func Start() $val = inputbox("","Type in your Program") If $val == "notepad" Then run("notepad.exe") ElseIf $val == "firefox" Then run("C:\Program Files\Mozilla Firefox\firefox.exe") Else Traytip("Wrong Input", $val, 10) Start() EndIf EndFunc Func Quit() Exit EndFunc FuncCloseWindow() If WinActive("[Class:Notepad]") Then WinClose("[Class:Notepad]") ElseIfWinActive("[Class:MozillaUIWindowClass]") Then WinClose("[Class:MozillaUIWindowClass]") EndIf EndFunc
WinWaitActive • Let’s Send Text • Now, instead of the sleep command, we can wait for a window to activate with WinWaitActive • Send Text to Notepad waiting for it to Activate
;All future code should include this template http://cl1p.net/123456789 HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") HotKeySet("{F1}","CloseWindow") While 1 Sleep(100) Wend Func Start() $val = inputbox("","Type in your Program") If $val == "notepad" Then run("notepad.exe") WinWaitActive("[Class:Notepad]") Send("Hello") ElseIf $val == "firefox" Then run("C:\Program Files\Mozilla Firefox\firefox.exe") Else Traytip("Wrong Input", $val, 10) Start() EndIf EndFunc Func Quit() Exit EndFunc FuncCloseWindow() If WinActive("[Class:Notepad]") Then WinClose("[Class:Notepad]") ElseIfWinActive("[Class:MozillaUIWindowClass]") Then WinClose("[Class:MozillaUIWindowClass]") EndIf EndFunc
Controls • Controls are a way to simulate a click or keyboard without actual movements. It sends it directly to the Window without pretending there is an external hardware connected. • Look at ControlClick and ControlSend • Use this to navigate to Google • You may need to delay slightly between ControlClick and ControlSend
Solution to Previous Code ;All future code should include this template http://cl1p.net/123456789 HotKeySet("{ESC}", "Quit") HotKeySet("{SPACE}", "Start") HotKeySet("{F1}","CloseWindow") While 1 Sleep(100) Wend Func Start() $val = inputbox("","Type in your Program") If $val == "notepad" Then run("notepad.exe") WinWaitActive("[Class:Notepad]") Send("Hello") ElseIf $val == "firefox" Then run("C:\Program Files\Mozilla Firefox\firefox.exe") WinWaitActive("[Class:MozillaUIWindowClass]") ControlClick("[Class:MozillaUIWindowClass]","","[CLASS:MozillaWindowClass; INSTANCE:1]","left",1,384, 35) sleep(10) ControlSend("[CLASS:MozillaUIWindowClass]", "","[CLASS:MozillaWindowClass; INSTANCE:1]" , "http://www.google.com{ENTER}") WinWaitActive("Google") Else Traytip("Wrong Input", $val, 10) Start() EndIf EndFunc Func Quit() Exit EndFunc FuncCloseWindow() If WinActive("[Class:Notepad]") Then WinClose("[Class:Notepad]") ElseIfWinActive("[Class:MozillaUIWindowClass]") Then WinClose("[Class:MozillaUIWindowClass]") EndIf EndFunc