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

Example Webservice / API call- Connecting to Zoom & extracting info

For the the AutoHotkey Webinars we use Zoom which is a great, robust, inexpensive tool for hosting online meetings/webinars.  They have a free version which allows you to connect with people for up to 45 minutes!

Below I walk through the following code where I demonstrate how I extract information about users & their meetings.   This is a great example of how many vendors offer APIs to connect to their tools.

Video walking through developing the Zoom API call

Example Webservice / API call- Connecting to Zoom & extracting info

AutoHotkey Webinar- Intro to Webservice / API calls

In this AutoHotkey Webinar we cover an Webservice / API calls.

Video Hour 1: High-level:

  • What is a Webservice API call
  • Web Scraping vs. WebServices / API
  • List of some “fun” WebService / APIs as well as links to resources to tens-of-thousands!
  • Differences between a Get vs. Post request
  • High-level look at oAuth1 vs. oAuth2
  • Resources
    • HTTP: Intro to HyperText Protocol, Types of Requests, oAuth, Parsing XML, XPath, JSON
    • AutoHotkey resource: Syntax writer, winHTTPrequest, Msxml2 vs. WinHTTPrequest, Parsing XML & JSON
  • Basic examples of API requests (compared using browser and winHTTP request)

Video Hour 2: Coding and Q&A

  • Played with several APIs
  • Discussed & demonstrated reverse-engineering API calls from a website
  • Passing key-value pairs (and using a function to keep it organized)
  • Reviewed additional APIs
  • Q&A

Script Highlight: Select text and “pretty” hyperlink text

The below script demonstrates how you can use AHK to automate highlighting text on a page and then, using the winClip library, constructing a “pretty” html link.  Here are links to the GetActiveBrowserURL and WinClipAPI / WinClip


What is a Webservice API? (Application Programming Interface?)


Examples of Webservice / API calls

  • Application / Software querying products for sale on Amazon.com
  • App on your phone getting latest Weather
  • Database pulling updated sales report
  • Using your Tablet to Select movies to watch on Netflix
  • DropBox application syncing files between your computer & cloud
  • Google places search

APIs are becoming increasingly available!

…Since 2005, we’ve seen APIs grow from a curiosity, to a trend, and now to the point where APIs are core to many businesses. APIs have provided tremendous value to countless organizations and developers, which is reflected in their continued growth.   Source: Programmable Web

growth in webservice APIs since 2005

 

 


Main Differences between WebService / API call & Web Browser

Webservice verse web browser calls


When to use API verse Web Scraping?

When to use webservice API call verse web scraping


Some Webservices / APIs

Some of the Amazing APIs out there…

Programmable Web – list of thousands of APIs as well as a great, six-part, article reviewing APIs

Short-list of APIs open to public on Wikipedia,   Google APIs– Over 100 APIs @ Google; Yahoo APIs

Social Sites : Twitter, Pinterest, Google+, YouTube, Facebook, LinkedIn, reddit, StumbleUpon, WordPress, Instagram

Contact / Business Lookup: ClearBit, Pipl, FullContact, Dunn & Bradstreet , Foursquare, Google Places, Yelp, GeoNames

Weather: Weather Underground, Yahoo Weather, World Air

Finance: Google Finance, Yahoo Finance, PayPal, Stripe, XigniteRealTime, Ally Invest

Government: A bunch of Government APIs, Cloud.Gov, US Census, 18F.gsa.gov

Additional: Wikipedia, Amazon Product, email to text, SmartSheet,SurveyGizmo, Bitly, SnapOCR, PasteBin, DropBox, Zoho, Zillow, imugr, Mailgun, MailChimp,  Microsoft: Graph, MS Office: Excel  Word  SharePoint  OneNote Outlook Yammer PowerPoint


Break-down of a REST API request via COM object

  • Create COM object
  • Open Endpoint (w/ parameters & Authentication if GET request)
  • Set RequestHeader(s)
  • Send (w/”payload & Authentication” for POST requests)
  • Get response (body or text)

Main differences of oAuth1 verse oAuth2

Unfortunately there is no “standard” implementation of oAuth1 or oAuth2 however, at a high-level, here are some of the main conceptual differences:

oAuth1:

  • Need a Key & Token from Webservice API (typically different than your username and password)
  • You use your Key & Token in your API call
  • oAuth1 is less secure and, generally, being phased out
  • While being phased out often the “developers” (us) can use oAuth1 for development of the “app”

oAuth2:

  • Need a Key and Token (same as oAuth1) however you use the Key & Token and some other parameters to perform a “handshake” which returns a secure token which typically times-out in seconds / minutes/hours
  • Your token is restricted to the level of your account (or what has been authorized)
  • The secure token is what is shared with your actual endpoint. (this allows other Social sites (like LinkedIn, Facebook, etc.) to assist your login but not have your username/password to the endpoint

HTTP and AutoHotkey Resources

HTTP Protocol & General Tutorials

AutoHotkey specific



Let’s make some Webservice / API calls!

Weather Underground

  1. Dallas forecast in XML
  2. Dallas forecast in JSON
  3. Using WinHttpRequest
  4. Example with browser

Yahoo business

  1. Pizza restaurants in zip code 75019 in XML
  2. Pizza restaurants in zip code 75019 in JSON
  3. Using WinHTTPRequest

AutoHotkey Webinar: 10/18/2016 Excel and COM

AutoHotkey webinarAutoHotkey Webinar Videos and Links

  • Here is a link to video of the First hour (Thank you Joe W for providing this.  I forgot to record the first hour!)
  • Video of Second hour where we chit-chat about how to best solve some problems.

Link to AutoHotkey Webinar resources Excel via COM:

 Resources covered during AutoHotkey webinar

 Featured Script- Toggle to other monitor

AutoHotkey webinars are planned for the third Tuesday of each month.  Signup to future webinars here