Code Samples

Python

AutoIT


Python

Level: Beginner

Define Variables

programming_languages = "Python", "VB", "C++", "C#"

Print Variables

print(programming_languages)

Basic for loops + variables

for language in programming_languages:
    print(language)

Basic Functions

def FuncExample():
    i = 1
    for language in programming_languages:
        #Concatinate Strings and Integers in print statements
        print("Language " + str(i) + ":" + language)
        #Increment Integer
        i += 1

#Remember to call the function after it is declared
FuncExample()

Functions with Variables #1 – Strings

def FuncVarExample1(fname, lname):
    #Print with CRLF
    print("First Name: " + fname + "\r\n" + "Last Name: " + lname)

FuncVarExample1("Joe","Bloggs")

Functions with Variables #2 – Integers + Returning Values

def FuncVarExample2(x, y):
    #Basic integer maths
    return x+y

#Concatenating Strings and Integers
print("33 + 42 = " + str(FuncVarExample2(33,42)))

AutoIT

Whilst AutoIT has not seen an update since Mar 16, 2018, it still proves to be a really useful windows scripting language for automating menial tasks. The help file and documentation for the language have been a great help to me over the years. Here are some of my most valuable and frequently used code snippets.

ShellExecuteWait

Runs an external program using the ShellExecute API and pauses script execution until it finishes.

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Execute Notepad and wait for the Notepad process to close.
    Local $iReturn = ShellExecuteWait("notepad.exe")

    ; Display the return code of the Notepad process.
    MsgBox($MB_SYSTEMMODAL, "", "The return code from Notepad was: " & $iReturn)
EndFunc   ;==>Example

WinGetHandle

Function WinGetHandle

This function allows you to use handles to specify windows rather than “title” and “text”.
Once you have obtained the handle you can access the required window even if its title changes.

#include <MsgBoxConstants.au3>
SpotifyPause()

Func SpotifyPause()
; Retrieve the handle of the Spotify window using the classname of SpotifyMainWindow.
    Local $hWnd = WinGetHandle("[CLASS:SpotifyMainWindow]")
	WinActivate($hWnd)
	Local $sText = WinGetTitle("[ACTIVE]")

	If $sText = "Spotify" Then
	   Exit
	Else
	   Send("{SPACE}") ; 'space' sends the pause commands to the active spotify window.
	   Exit
	EndIf

    If @error Then
        ConsoleWrite("An error occurred when trying to retrieve the window handle of Spotify")
        Exit
    EndIf
EndFunc
%d bloggers like this: