AutoHotkey webinar- Intro to GUIs

In this AutoHotkey Webinar we covered: Introduction to GUIs  (Here is a link to scripts shared during the webinar)

Video Hour 1:  High Level:

  • Why you should create a GUI
  • Pros / Cons about Using a GUI
  • Pros / Cons about Programming a GUI
  • Rules of Thumb / Tips
  • Overview of various GUIs available in AutoHotkey
  • Resources: Tutorials & Tools for building GUIs

Video Hour 2: Coding and Q&A

Script Highlight: TaskDialog from just me and TaskDialog Creator from


When should you consider a GUI

  • Help select from a list of items that changes frequently
  • Use dropdowns , radio buttons, etc. to control what can be selected
  • Target audience using the tool is non-tech savvy
  • The list you want to choose from is available progromatically (files in folder, different accounts, etc)
  • You want to display data and be able to sort by columns keeping the rows intact
  • You want to lock-down use of the tool
  • You want to make sure certain values/fields get updated (tags for an email, subject line, etc)
  • Your code is long and key data points that you change are mixed throughout the script
  • Dealing with Images / Video files- Preview of image helps humans classify what “it” is

Pros / Cons of Using GUIs

Pros of Using GUIs

  • Non-programmers can do a lot while knowing little
  • Color / Animation can ease functionality / Usage
  • Graphics can convey data
  • Build in ways to reduce errors
  • input box for path, etc
  • “wizard” can be a template for end-user

Cons of Using GUIs

  • Non-programmers can do a lot while knowing little (make sure you build-in safeguards*)
  • Not as flexible / can typically only do what has been defined by designer
  • Manually have to repeat process each use
  • Poorly designed UI can be very painful

* Hawaii Missile-Alert Crisis


Pros / Cons of Programming GUIs

Pros of Programming GUIs

  • Can “lock down” interaction (have drop-downs, radio buttons, etc. ) with specific commands (instead of free-form typing by user)
  • Can reduce errors / mistakes by limiting options

Cons of Programming GUIs

  • Users can do actions, in any order, at any time (Not procedural)
  • Different screen resolutions
  • Various input (Mouse, Keyboard, touch,etc.)

Rules of Thumb / Tips

  • KISS- As they say, “less is more”.
  • Use Tabs to help separate tasks
  • Always “destroy” the GUI before you create it (yes, even the first time)
  • Name your GUIs
  • Label your inputs with pre-selected text or text control (e.g. edit fields)
  • Keep your design-focus on the End-User
  • Remember GUIs are EVENT driven (not linear – user can start “anywhere”)
  • Listviews & Treeviews- Disable redraw, Put in new info, Enable redraw
  • Build-in safeguards against users making mistakes

Quick Demonstrations  (See this file for the scripts)

  • Input box (multiple edit fields)
  • Tabs, Checkboxes, Radio Buttons
  • Resizable Edit
  • ListView
  • TreeView
  • DropdownList, ComboBox, Context sensitive help
  • On-Screen Display
  • Installation / Progress Bar
  • Editor with Menu bar
  • Menus

Tutorials

Use HTML & CSS in your GUIs by JoeDF

Tools


AutoHotkey webinar- Copying / Editing / Saving files & Folders

In this AutoHotkey Webinar we covered: Working with Files & Folders (Here are the files demonstrated during the webinar)

Video Hour 1: High-level:

  1. Moving / Copying / Deleting Files / Folders
    1. File Copy / Move
    2. File Delete
    3. Loop Files & Folders
  2. Creating / Editing Files
    1. File Encoding
    2. FileRead & FileAppend
    3. Loop FileRead & FileReadLine
    4. File Object

Video Hour 2: Q&A

Script Highlight-  Locker

  • Kill power to monitors & block most keys & mouse
  • Great to use instead of using Windows + L and having to enter yoiur crazy corporate password

Copy, Move, Delete : Files & Folders

FileCopy – SourcePattern, DestPattern [, Flag]

  • FileCopy copies files only. To copy a single folder (including its subfolders), use FileCopyDir. To instead copy the contents of a folder (all its files and subfolders), see the examples section of FileCopy

FileCopyDir – FileCopyDir, Source, Dest [, Flag]

  • Copies a folder along with all its sub-folders and files (similar to xcopy)
  • FileCopyDir copies a single folder. If the destination directory structure doesn’t exist it will be created if possible

FileMove – FileMove, SourcePattern, DestPattern [, Flag]

  • FileMove moves files only. To move or rename a single folder, use FileMoveDir

FileDelete – FileDelete, FilePattern

  • To remove an entire folder, along with all its sub-folders and files, use FileRemoveDir
  • To delete a read-only file, first remove the read-only attribute. For example: FileSetAttrib, -R, C:\My File.txt

Looping Files & Folders

Loop (files & folders) – Loop, Files, FilePattern [, Mode]      Mode:  D=Directories, F=Files, R=Recursive

FilePattern: The name of a single file or folder, or a wildcard pattern

  • Retrieves the specified files or folders, one at a time
  • A file-loop is useful when you want to operate on a collection of files and/or folders, one at a time
  • The following Special Variables exist in any file-loop
    • A_LoopFileName
    • A_LoopFileExt
    • A_LoopFileFullPath
    • A_LoopFileLongPath
    • A_LoopFileShortPath
    • A_LoopFileShortName
    • A_LoopFileDir
    • A_LoopFileTimeModified
    • A_LoopFileTimeCreated
    • A_LoopFileTimeAccessed
    • A_LoopFileAttrib
    • A_LoopFileSize
    • A_LoopFileSizeKB
    • A_LoopFileSizeMB

File Encoding

  • Sets the default encoding for FileReadFileReadLineLoop ReadFileAppend, and FileOpen
  • Encoding can be one of the following values:
  • UTF-8: Unicode UTF-8, equivalent to CP65001
  • UTF-16: Unicode UTF-16 with little endian byte order, equivalent to CP1200
  • UTF-8-RAW or UTF-16-RAW: As above, but no Byte Order Mark (BOM*) is written when a new file is created
  • CPnnn: a code page with numeric identifier nnn. See Code Page Identifiers. (UTF-8 is CP65001, UTF-16 is CP1200)
  • Empty or omitted: the system default ANSI code page, which is also the default setting* The byte order mark (BOM) is a Unicode character, U+FEFF byte order mark (BOM), whose appearance as a magic number at the start of a text stream can signal several things to a program consuming the text

FileRead & FileAppend

File Read FileRead, OutputVar, Filename

  • Reads file’s content into a variable
  • FileRead, Var, *P65001 %file_path% ;-Read in the file using UTF-8 Encoding
  • When the goal is to load all or a large part of a file into memory, FileRead performs much better than using a file-reading loop.
  • FileOpen() provides more advanced functionality than FileRead, such as reading or writing data at a specific location in the file without reading the entire file into memory
  • When Reading / Writing to a file many times, FileObject is much faster as it does not open/close the file each time

FileAppend FileAppend [, Text, Filename, Encoding]

  • Writes text to the end of a file (first creating the file, if necessary)
  • FileAppend,%data%,%File_Name%.txt,UTF-8
  • To overwrite an existing file, delete it with FileDelete prior to using FileAppend
  • * don’t forget to add a line break at the end of each row!

LoopFileRead, FileReadLine

Loop (read file contents)   Loop, Read, InputFile [,OutputFile]

  • A file-reading loop is useful when you want to operate on each line contained in a text file, one at a time. It performs better than using FileReadLine because:
    • the file can be kept open for the entire operation
    • the file does not have to be re-scanned each time to find the requested line number.
  • Lines up to 65,534 characters long can be read. If the length of a line exceeds this, its remaining characters will be read during the next loop iteration
  • To load an entire file into a variable, use FileRead because it performs much better than a loop (especially for large files).

FileReadLine  FileReadLine, OutputVar, Filename, LineNum

  • Reads the specified line from a file and stores the text in a variable
  • It is strongly recommended to use this command only for small files, or in cases where only a single line of text is needed. To scan and process a large number of lines (one by one), use a file-reading loopfor best performance. To read an entire file into a variable, use FileRead

FileObject

FileOpen – file := FileOpen(Filename, Flags [, Encoding])

  • Flags (r=Read, w=Write, a=Append, rw=Read/Write , h=Handle, -rwd=Lock file/deny access)

ReadLine–  TextLine := File.ReadLine()

  • Reads a line of text from the file and advances the file pointer

Seek–  TextLine := File.Seek()

  • Distance to move, in bytes. Lower values are closer to the beginning of the file

AtEOF (End of File)- IsAtEOF := File.AtEOF

  • Retrieves a non-zero value if the file pointer has reached the end of the file, otherwise zero.

WriteLine–  File.WriteLine([String])

  • Writes a string of characters followed by n or r`n depending on the flags used to open the file. Advances the file pointer

Encoding– File.Encoding

  • RetrievedEncoding and NewEncoding is a numeric code page identifier (e.g. CP65001)

Close–  File.Close()

  • Although the file is closed automatically when the object is freed, it is recommended to close the file as soon as possible

COM Objects & DLL call

AutoHotkey Webinar- Working with Multiple Scripts & Sharing data / variables between them

In our first hour of today’s  AutoHotkey webinar we talked about working with multiple scripts in AutoHotkey.  How you can break out functions into separate files and leverage the library.

In the Second Hour we did Q&A and discussed other things we typically automate.

The two scripts we highlighted were: Dictionary by Fanatic Guru and iThesauras based of Dictionary by rommmecek.

Working with Multiple Files in AHK

  • Breaking out scripts into separate files / functions (Here is a page several videos on functions)
  • Using #include
  • Lib folder
  • Sharing data between scripts via:
  • Passing via command line parameters
  • OnMessage
  • COM object via Windows Temp Environment variables
  • COM object via custom object
  • Consolidating scripts from multiple files into one

Libraries and #Includes

Libraries

  • A script may call a function in an external file without having to use #Include. For this to work, a file of the same name as the function must exist in one of the following library directories:
  • Local library: %A_ScriptDir%\Lib\
  • User library: %A_MyDocuments%\AutoHotkey\Lib\
  • Standard library: path-to-the-currently-running-AutoHotkey.exe\Lib\

Order of search

  1. Original file
  2. Local Library
  3. User Library
  4. Standard Library
  • If a function is not found, but has an underscore in the name, it will search for a library with everything up to the underscore. (E.g. If your function is named “IE_Load” it will search for a file named IE.ahk and load it)

  Benefits of using Library: 1) Code maintenance, 2) Fewer files to keep track of, etc.  3) Easier to read code

  Negatives of using Library: Sharing code can be a bit more complex

Sharing data between scripts: Command line Parameters

AutoHotkey.exe [Switches] [Script Filename] [Script Parameters]

CompiledScript.exe [Switches] [Script Parameters]

Script Parameters:

  • The string(s) you want to pass into the script, with each separated from the next by a space.
  • Any parameter that contains spaces should be enclosed in quotation marks.
  • The script sees incoming parameters as the variables %1%, %2%, and so on.
  • In addition, %0% contains the number of parameters passed (0 if none). However, these variables cannot be referenced directly in an expression because they would be seen as numbers rather than variables.
  • In the receiver, use Param2 = %2% ;Note NO colon

Pros:  Simple, reliable

Cons: Only works when starting up, One-direction, Only passes strings/variables

Sharing data between scripts: OnMessage

Pass String between scripts with OnMessage

Pros: Simple to Use

Cons: Code is somewhat complex, A bit resource intensive, Pass single variable

Sharing data between scripts: Temp Environment Variable

Create a Temporary Environment Variable and stores it, until reboot, in Registery under: HKCU\VolatileEnvironment

Pros: Easy to use, Variable is Accessible after “sender” exits, Fast, Can use objects / dot-notation

Cons: Pass variables/strings, Character limitation

Sharing data between scripts: Custom COM Object

Registers a unique CLSID (Computer Licence Security ID) in registry  (A CLSID is a globally unique identifier that identifies a COM class object. If your server or container allows linking to its embedded objects, you need to register a CLSID for each supported class of objects.)

Pros:Can pass objects, Functions, Variables,Strings, Bi-directional, Super Fast

Cons: Code is more complex, Can’t use FOR loops to iterate over Objects, Can slow some when passing large data

Consolidating Multiple Scripts into One

  • AutoExec section – (Top of script until first: Return, Exit, hotkey/hotstring label)
  • Hotkeys – Conflicting Hotkeys prevents script from running (Make them context sensitive?)
  • Hotstrings – First Hotstring takes presedent
  • Re-using: Variables , Functions, Labels, Classes cause errors
  • Duplicate code – Often bring in things multiple times
  • Reasons why prefer one over the other
  • Multiple scripts-
    • Sharing scripts is easier
    • Troubleshooting can be easier
  • One Script-
    • One file to edit
    • One script in system tray (Can use #Notrayicon in multiple scritps to help negate this)
    • Ease sharing of code

Web Scraping with AutoHotkey 109a- Triggering an EventListener on a page


You ever plugging along on web scraping a page and have a problem with an element (drop down, edit field, radio button, etc) not updating?  Chances are the page has an EventListener watching that element for a specific Event type.  We used to be able to reliably “click” an element or send .fireEvent(“onchange”) / .fireEvent(“onclick”) howe

ver more and more pages are using this newer approach where they build an Event Listener and monitor for events on a given element.

If you’re a non-coder like me, this was very problematic to deal with as the EventListener is located in a different place in the DOM.  In the below video I walk through how to spot the problem and offer up a couple of solutions (like Visual Event) that should greatly help. The second video below demonstrates using my updated AutoHotkey syntax writer.

Triggering an EventListener on a page

Web Scraping with AutoHotkey 109a- Triggering an EventListener on a page

Updated AutoHotkey Syntax Writer

Here I demonstrate using my updated AutoHotkey Syntax writer to provide the needed information for the Events.

Web Scraping with AutoHotkey 109b- AHK syntax writer dealing with Events