Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Create self-compiling scripts


  • Please log in to reply
2 replies to this topic
MikeG
  • Members
  • 34 posts
  • Last active: Jul 17 2011 05:13 AM
  • Joined: 23 Apr 2004
Here's some code I have been using to make scripts "self-compiling". This makes things much easier for testing when you might want to re-compile many times. You just double click the .ahk file and you're prompted to confirm the compilation with the options shown. #include it in your scripts, as shown in the example comments.

; Include this code at the beginning of a script to provide self-compiling functionality.
; When the .ahk file is run it will prompt to compile, but the .exe will run as intended.
;
; You can set the following compile parameter variables:
; OUT    optional folder path (create exe with same name as script in that folder),
;        or full path including a different exe name if required.
; ICON   optional path & name of an icon file, default is AutoHotkey icon.
; PASS   optional password to encrypt the script within the exe.
;
; Example:
; OUT=D:\Temp\
; ICON=C:\Windows\System\Icons\Setup.ico
; PASS=encrypt
; #include,!compile.ahk
;
IfInString,A_ScriptName,.ahk
{
  StringTrimRight,name,A_ScriptName,4
  RegRead,cmd,HKEY_CLASSES_ROOT,AutoHotkeyScript\Shell\Compile\Command
  StringTrimRight,cmd,cmd,4
  IfEqual,out,,StringReplace,out,A_ScriptFullPath,.ahk,.exe ; default to same folder & name
  IfNotInString,out,.exe,SetEnv,out,%out%\%name%.exe ; same name in specified folder
  StringReplace,out,out,\\,\,All
  SetEnv,cmd,%cmd%  "%A_ScriptFullPath%" /out "%out%"
  SetEnv,msg,Input`t`t%A_ScriptFullPath%`nOutput`t`t%out%
  If icon>
  {
    SetEnv,cmd,%cmd% /icon "%icon%"
    SetEnv,msg,%msg%`nIcon`t`t%icon%
  }
  If pass>
  {
    SetEnv,cmd,%cmd% /pass "%pass%"
    SetEnv,msg,%msg%`nPassword`t`t%pass%
  }
  MsgBox,1,Compile this script?,%msg%
  IfMsgBox,ok,Run,%cmd%
  Exit
}


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Self-compiling scripts, great idea. I like how you discovered that A_ScriptName contains .exe vs. .ahk if the script is compiled. I had forgotten that this is a great way for a script to detect if it is running in compiled form or not.

Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
very nice idea. thanx for sharing!

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat