While the AutoHotkey FileInstall Command Works Great When Packaging External Files inside Compiled EXE Files, You Can Also Package and Extract Data and Text Files with Uncompiled AHK Script Files
In Chapter Six and Chapter Thirty-eight of the book AutoHotkey Applications, I discuss using the FileInstall command when combining various types of support files (e.g. jpg, wav, ini) into one compiled EXE file. Upon loading the app for the first time, a double-click of the left mouse button extracts the embedded files and places each at its proper location on the drive. After downloading an AutoHotkey app, this command alleviates the need to keep track of each external file required by the package. However, the FileInstall command only works for compiled EXE files.
Due to the ever-present risk from computer viruses, it gets harder and harder to send and download EXE files. Both Web and computer security systems give user warnings—if they don’t outright block all EXE files. The safest and easiest way to share AutoHotkey apps is through the text-based .ahk script files which contain only human-readable code. (Users can later compile the script themselves.) If you want to include more files than merely the AutoHotkey script, you can package all the pieces inside a ZIP file for later unzipping. But, if you want to include a text-based data or ReadMe.txt file for the AutoHotkey app which automatically extracts into the working folder, then you can use the following technique—no FileInstall command required.
Auto-Extract Data or Text Files from an AutoHotkey Script File (.ahk)
In the InstantHotstring.ahk script, I include a sample Hotstring file which, the first time the script runs, AutoHotkey automatically extracts and saves in a new Hotstring subfolder. Since I didn’t compile the script into an EXE file, I couldn’t use the FileInstall command. Plus, since the sample file includes Hotstring code, I must ensure that those sample Hotstrings do not automatically activate.
Since everything in .ahk files appears as text or some form of Unicode, you’ll find this approach to installing extra files limited when compared to FileInstall. The FileInstall command allows you to package and extract virtually any type of file—including images (e.g. jpg, gif, png), audio files (e.g. wav), and PDFs. But, you can include in .ahk scripts the text for data files such as Comma Separated Value (CSV) files, INI files, other .ahk files, and text ReadMe files. (You’ll find both CSV and INI data files discussed in the book AutoHotkey Applications.)
I wrote the following AddSaveSample subroutine to extract and save the sample Hotstring file:
AddSaveSample: ; Variable contains data destined for the new file SampleFile := " ( /* This SampleSaveFile.ahk represents the file type saved by the InstantHotstring.ahk script. You can use it to load the Hotstrings into the InstantHotstring app or run the Hotstrings directly with AutoHotkey. */ :*:btw::by the way :*:fyi::for your information :*:imho::in my humble opinion :*:imo::in my opinion :*:lol::laugh out loud :*:tmi::too much information :OT:cew::http://www.computoredge.com )" ; If missing, create the \Hotstring folder If (InStr(FileExist("\Hotstrings"), "D") = 0) FileCreateDir, Hotstrings ; If missing, write SampleSaveFile.ahk FileName := A_ScriptDir . "\Hotstrings\SampleSaveFile.ahk" If !FileExist(A_ScriptDir . "\Hotstrings\SampleSaveFile.ahk") FileAppend , %SampleFile%, %FileName%, UTF-8 Return
The subroutine places all of the text destined for the extracted file inside the variable SampleFile. This allows the embedding of the entire new file without using any special commands.
Normally, any Hotstring lines of code placed in an AutoHotkey script would automatically load and activate—regardlessly of their location in a script. However, since the Hotstrings appear inside the variable SampleFile within a continuation section bounded by parentheses “(…)”, AutoHotkey does not trigger the Hotstring code.
The FileExist() function returns a string of the file attributes for an existing file. Assuming the “\Hotstrings“ folder exists in the A_ScriptDir working directory, the “D” (for directory) will appear in that string (If Instr()). If not, AutoHotkey creates the folder (FileCreateDir command).
When loading, the script uses a similar technique in the auto-execute section to check for the existence of the sample Hotstring file—if not, AutoHotkey runs the AddSaveSample subroutine:
If !FileExist(A_ScriptDir . "\Hotstrings\SampleSaveFile.ahk") GoSub, AddSaveSample
While the entire file text must appear inside the AutoHotkey script (.ahk), this approach to extracting separate files provides a method for packaging setup files into the original script without compiling it into an EXE file. This works for ReadMe.txt files, other .ahk script files, and any all-text data files—including CSV and INI files.
Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)