New AutoHotkey Script Template

Talk about anything
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

New AutoHotkey Script Template

17 Sep 2015, 08:49

I wanted to share my current Template.ahk file for new scripts. I have borrowed parts from different scripts I have found on the forums until I was satisfied with the result. It's setup to include a basic GUI window by default, which is easily removable if not required.

Code: Select all

; =======================================================================================
; Name ..........: Script Name
; Description ...: Script Description
; AHK Version ...: AHK_L 1.1.22.06 (Unicode 32-bit) - September 13, 2015
; Platform ......: Windows 2000+
; Language ......: English (en-US)
; Author ........: FirstName LastName <[email protected]>
; =======================================================================================

; Changelog =============================================================================
; Legend: (+) NEW, (*) CHANGED, (!) FIXED
; ---------------------------------------------------------------------------------------
; YYYY-MM-DD  FirstName LastName  <[email protected]>
; + Change Description
; ---------------------------------------------------------------------------------------
; YYYY-MM-DD  FirstName LastName  <[email protected]>
; + Change Description
; =======================================================================================

; Global ================================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep script permanently running until terminated
#NoEnv ; Avoid checking empty variables to see if they are environment variables
#Warn ; Enable warnings to assist with detecting common errors
;#NoTrayIcon ; Disable the tray icon of the script
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability
SetWorkingDir, %A_ScriptDir% ; Change the working directory of the script
SetBatchLines, -1 ; Run script at maximum speed
OnExit, ExitSub ; Run a subroutine or function automatically when the script exits
; =======================================================================================

; Script ================================================================================

Return ; End automatic execution
; =======================================================================================

; Labels ================================================================================
MenuHandler:
	MsgBox, You selected %A_ThisMenuItem% from menu %A_ThisMenu%.
Return

GuiEscape:
GuiClose:
ExitSub:
	ExitApp ; Terminate the script unconditionally
Return
; =======================================================================================

; Functions =============================================================================
GUIMain()
{
	Static GUICreate := GUIMain()
	
	Global
	
	GUIWidth := 600, GUIHeight := 400
	
	Menu, Tray, Icon, Shell32.dll, 174 ; Change the tray icon
	Menu, Tray, Tip, Script Name ; Change the tooltip of the tray icon
	Menu, Tray, NoStandard ; Remove all standard tray menu items
	Menu, Tray, Add, Exit, ExitSub

	Gui, +LastFound -Resize
	;Gui, Color, FFFFFF
	Gui, Margin, 10, 10

	Gui, Add, Button, % " x" 10 " y" 10 " w" 73 " h" 26, Button1

	Gui, Show, % " w" GUIWidth " h" GUIHeight, Application
	Return
}
; =======================================================================================
Last edited by TheDewd on 17 Sep 2015, 09:27, edited 3 times in total.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: New AutoHotkey Script Template

17 Sep 2015, 09:18

This is how my Tamplate looks like
The ========== are 130 chars long (GitHub max. lenght without a scrollbar)

Code: Select all

; ===============================================================================================================================
; Title .........: FILE_NAME / FUNCTION_NAME / CLASS_NAME
; AHK Version ...: 1.1.22.06 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: FILE_DESCRIPTION
; Version .......: v1.00
; Modified ......: 2015.09.17-1618
; Author(s) .....: YOUR_NAME_HERE
; ===============================================================================================================================
;@Ahk2Exe-SetName FILE_NAME
;@Ahk2Exe-SetDescription FILE_DESCRIPTION_SHORT
;@Ahk2Exe-SetVersion v1.00
;@Ahk2Exe-SetCopyright Copyright (c) 2015-2015`, YOUR_NAME_HERE
;@Ahk2Exe-SetOrigFilename FILE_NAME.ahk
; ===============================================================================================================================

; GLOBAL SETTINGS ===============================================================================================================

#Warn
#NoEnv
#SingleInstance Force
SetBatchLines -1

global VarName := ""

#Include FileName.ahk

; GUI ===========================================================================================================================

Gui, Margin, 5, 5
Gui, Add, Text, xm ym w145 h22 0x2001, % "Hello World!"
Gui, Add, Button, xm y+5 w70 gBUTTON_OK, % "OK"
Gui, Add, Button, x+5 yp w70 gBUTTON_CLOSE, % "CLOSE"
Gui, Show, AutoSize
return

; SCRIPT ========================================================================================================================

BUTTON_OK:
    MsgBox % "OK"
return

BUTTON_CLOSE:
    ExitApp
return

; Function ======================================================================================================================

MyFunction()
{
    ;return
}

; Class =========================================================================================================================

Class MyClass
{
    ;__New()
}

; EXIT ==========================================================================================================================

GuiClose:
GuiEscape:
ExitApp

; ===============================================================================================================================
ps and yes I see some borrowed parts from me :D
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: New AutoHotkey Script Template

20 Sep 2015, 23:49

For a while I used a template like this:

Code: Select all

FileOpen(A_ScriptFullPath, "w", "UTF-8")  ; This OVERWRITES/ERASES the current script file.
Run edit %A_ScriptFullPath%
... because after using "New AutoHotkey Script" from the Explorer context menu, the next thing I did was always open the script for editing and delete the template lines. With this I just double-click the new file.
User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: New AutoHotkey Script Template

21 Sep 2015, 02:48

I added this helpmenu to my Template:

Code: Select all

#Persistent
help = 
(
program help and hotkeys
)

; cut it to max 127 chars
tiptext = 
(
traytip has max 127 chars.
)

;msgbox,,,%Help%,5
traymenutitle:="title of the traymenu (normally the programname)"
Menu, tray,add,  info
Menu, tray,Disable,  info
Menu, tray,Rename, info , %traymenutitle%
Menu, tray, add  
Menu, Tray, Tip, %tiptext%
Menu, tray, add, Help
Menu, tray, add, Exit
Menu, tray, NoStandard

return

Help:
Msgbox, %Help%
return

info:
;change traymenutitle above
return

Exit:
exitapp

User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: New AutoHotkey Script Template

16 Jan 2016, 16:52

I have since updated my Template file and want to share it here...

Code: Select all

; Header =======================================================================
; Name .........: New AutoHotkey Script
; Description ..: Script Description
; AHK Version ..: 1.1.23.00 (Unicode 32-bit) - January 16, 2016
; OS Version ...: Windows 2000+
; Language .....: English (en-US)
; Author .......: FirstName LastName <[email protected]>
; Licence ......: Unlicense <http://www.unlicense.org>
; Modified .....: YYYY-MM-DDTHH:MM:SS+00:00
; Filename .....: New AutoHotkey Script.ahk
; ==============================================================================

; Changelog ====================================================================
; YYYY-MM-DD  FirstName LastName  <[email protected]>
; * Change Description
; ==============================================================================

; Globals ======================================================================
#SingleInstance, Force ; Allow only one running instance of the script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#Warn ; Enable warnings to assist with detecting common errors
;#NoTrayIcon ; Disable the tray icon of the script
SendMode, Input ; Method for sending keystrokes and mouse clicks
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
;SetBatchLines, -1 ; Run the script at maximum speed
;SetWinDelay, -1 ; The delay to occur after modifying a window
;SetControlDelay, -1 ; The delay to occur after modifying a control
OnExit, ExitSub ; Run a subroutine or function when the script exits

Application := {} ; Create Application Object
Application.Name := "New AutoHotkey Script"
Application.Version := "0.1"

Window := {} ; Create Window Object
Window.Width := 600
Window.Height := 400
Window.Title := Application.Name
; ==============================================================================

; Script =======================================================================
Menu, Tray, Icon, Shell32.dll, 3
Menu, Tray, Tip, Application.Name
Menu, Tray, NoStandard ; Remove all standard menu items from the tray menu
Menu, Tray, Add, Exit, ExitSub

Gui, +LastFound -Resize +HwndGui1
;Gui, Color, FFFFFF
;Gui, Margin, 10, 10

Gui, Add, Button, % " x" 10 " y" 10 " w" 75 " h" 23, % "Button1"

Gui, Show, % " w" Window.Width " h" Window.Height, % Window.Title
return ; End automatic execution
; ==============================================================================

; Labels =======================================================================
MenuHandler:
	MsgBox,, MenuHandler, % "Menu Item: " A_ThisMenuItem "`nMenu: " A_ThisMenu
return

GuiEscape:
GuiClose:
ExitSub:
	ExitApp ; Terminate the script unconditionally
return
; ==============================================================================

; Functions ====================================================================
; // Reserved
; ==============================================================================
User avatar
SnowFlake
Posts: 368
Joined: 28 Apr 2015, 05:41
Contact:

Re: New AutoHotkey Script Template

16 Jan 2016, 17:44

a little simple but it does its job and saves me time

Code: Select all

/*

Script Name: --
Scritp Version: --
Made by: Flow_Snake//SnowFlake
Autohotkey version: v1.1.22.09 or higher
Tested on: Windows 7 64bit & Windows 8 32bit
Date: --

*/

IF NOT A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}

#SingleInstance Force

CoordMode, Mouse, Client
CoordMode, Pixel, Client
CoordMode, ToolTip, Client

§::
ToolTip, Script started!,0,0
WinActivate, Title
WinWaitActive, Title


return

MButton::
keywait, MButton
While(1)
{
IF (GetKeyState("Esc","p"))
{
break
}
MouseGetPos, PosX, PosY
ToolTip, x:%PosX% y:%PosY%

If (GetKeyState("LButton","p"))
{
keywait, LButton
MouseGetPos, PosX, PosY
Clipboard = click, %PosX% %Posy%
}

If (GetKeyState("RButton","p"))
{
keywait, RButton
MouseGetPos, PosX, PosY
Clipboard = click Right, %PosX% %Posy%
}

}
Tooltip
Return

ESC::Exitapp
:yawn:
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: New AutoHotkey Script Template

17 Jan 2016, 10:37

We are now at 1.1.23.0 ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: New AutoHotkey Script Template

17 Jan 2016, 11:49

Who's "we"? :P I still use 1.1.22.01 <lazy>
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: New AutoHotkey Script Template

17 Jan 2016, 12:07

my template:

Code: Select all

/*
Name: 
Version 1.0 (date)
Created: date
Author: tidbit
Credit: 

Hotkeys:
	esc --- Quit

Description:
	
*/

; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force


Esc::
	critical
	exitapp
return

rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: New AutoHotkey Script Template

18 Jan 2016, 01:59

tidbit... add a watermark :D

Code: Select all

  , _ ,
 ( o o )
/'` ' `'\
|'''''''|
|\\'''//|
   """
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
vasili111
Posts: 747
Joined: 21 Jan 2014, 02:04
Location: Georgia

Re: New AutoHotkey Script Template

19 Jan 2016, 03:01

Nice thread.
1. I am always putting in the top TODO list when script is not complete. It always helps to quickly realise what to do next if there was some pause in script development.
2. After always goes Special notes which describe some special notes that I should remember when working with this script.
All this helps me to quickly continue script development and if it have some special cases to always remember them.
Like:

Code: Select all

; TODO:
; 1. ------------------
; 2. ------------------
; 3. ------------------
; 4. ------------------


; Special notes:
; 1. ------------------
; 2. ------------------
; 3. ------------------
; 4. ------------------
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: New AutoHotkey Script Template

19 Jan 2016, 10:46

quite the nice owl. I have one aswell (yours is more realistic, though):

Code: Select all

,___, 
[O.o] 
/)__) 
―"――"―
I used it on my original version of the tutorial as an easter-egg (hover over a header) but it was removed :(
https://acorns.github.io/AHK_Tutorial/
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: New AutoHotkey Script Template

19 Jan 2016, 10:56

tidbit wrote:I used it on my original version of the tutorial as an easter-egg (hover over a header) but it was removed :(
I still see it... I don't think it has been removed... Or did you mean that your tutorial is no longer being used?
OwlHeader.png
OwlHeader.png (7.3 KiB) Viewed 20851 times
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: New AutoHotkey Script Template

19 Jan 2016, 11:18

my link is my repo, while it was a WIP. the official version removed it: https://autohotkey.com/docs/Tutorial.htm
(also note: the official version has sections like 1.1 1.2 1.3... but mine has 1.a 1.b 1.c... if you click 1.1, it still has "a. Downloading and installing AutoHotkey" :D)
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: New AutoHotkey Script Template

11 Mar 2016, 15:07

I have made another revision to my Template.ahk file... I think it's an improvement.

Code: Select all

; Script Information ===========================================================
; Name .........: New AutoHotkey Script
; Description ..: New Script Description
; AHK Version ..: 1.1.23.01 (Unicode 32-bit) - January 24, 2016
; OS Version ...: Windows 2000+
; Language .....: English - United States (en-US)
; Author .......: FirstName LastName <[email protected]>
; Filename .....: New AutoHotkey Script.ahk
; ==============================================================================

; Revision History =============================================================
; Revision 2 (YYYY-MM-DD)
; * Change description
; * Change description
; ------------------------------------------------------------------------------
; Revision 1 (YYYY-MM-DD)
; * Initial release
; ==============================================================================

; Auto-Execute =================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#Warn ; Enable warnings to assist with detecting common errors
;#NoTrayIcon ; Disable the tray icon of the script
SendMode, Input ; The method for sending keystrokes and mouse clicks
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
;SetBatchLines, -1 ; The speed at which the lines of the script are executed
;SetWinDelay, -1 ; The delay to occur after modifying a window
;SetControlDelay, -1 ; The delay to occur after modifying a control
OnExit("OnUnload") ; Run a subroutine or function when exiting the script

return ; End automatic execution
; ==============================================================================

; Labels =======================================================================
MenuHandler:
    MsgBox,, MenuHandler, % "Menu Item: " A_ThisMenuItem "`nMenu Name: " A_ThisMenu
return

ControlHandler:
    If (A_GuiControl = "Button1") {
        MsgBox, % "You clicked the button!"
    }
return

GuiEscape:
GuiClose:
ExitSub:
    ExitApp ; Terminate the script unconditionally
return
; ==============================================================================

; Functions ====================================================================
OnLoad() { ; Execute before the Auto-Execute section of the script
    Global ; Assume-global mode
    Static Init := OnLoad() ; Call function

    ; Global Variables ---------------------------------------------------------
    ApplicationName := "New AutoHotkey Script"
    ApplicationVersion := "0.1"
    ResourceTemp := A_Temp . "\" . ApplicationName . "\"

    FileCreateDir, % ResourceTemp ; Create a directory for temporary files

    ;DllCall("Gdi32.dll\AddFontResourceEx", "Str", "filename.ttf", "UInt", 0x10, "UInt", 0)
}

OnUnload(ExitReason, ExitCode) { ; Execute when exiting the script
    Global ; Assume-global mode

    ;DllCall("Gdi32.dll\RemoveFontResourceEx", "Str", "filename.ttf", "UInt", 0x10, "UInt", 0)

    FileRemoveDir, % ResourceTemp, 1 ; Remove the directory for temporary files
}

WM_CTLCOLORBTN(wParam, lParam, Msg, Hwnd) {
    ; Set button border color to match the GUI color
    Global ; Assume-global mode
    Static Init := OnMessage(0x0135, "WM_CTLCOLORBTN") ; Call function
    Local B := SubStr(GuiColor, 5, 2) ; Blue
    Local G := SubStr(GuiColor, 3, 2) ; Green
    Local R := SubStr(GuiColor, 1, 2) ; Red
    return DllCall("Gdi32.dll\CreateSolidBrush", "UInt", "0x" . B . G . R, "UPtr")
}

GuiCreate() { ; Main GUI window
    Global ; Assume-global mode
    Static Init := GuiCreate() ; Call function

    ; Global Variables ---------------------------------------------------------
    GuiWidth := 600 ; Default width of the GUI window
    GuiHeight := 400 ; Default height of the GUI window

    ; Tray Options -------------------------------------------------------------
    Menu, Tray, Icon, Shell32.dll, 3
    Menu, Tray, Tip, % ApplicationName
    Menu, Tray, NoStandard ; Remove all standard menu items from the tray menu
    Menu, Tray, Add, Exit, ExitSub

    ; Graphical User Interface -------------------------------------------------
    Gui, +LastFound -Resize +HWNDhGui1
    Gui, Color, % (GuiColor := "000000")
    Gui, Margin, % (GuiMarginX := 10), % (GuiMarginY := 10)

    Gui, Add, Tab2, % " x" 0 " y" 0 " w" 0 " h" 0 " vTabControl", % "Tab1|Tab2"

    Gui, Tab ; Future controls are excluded from any tab control
    Gui, Add, Button, % " x" 10 " y" 10 " w" 80 " h" 26 " vButton1 gControlHandler", % "Button1"

    ;Gui, Tab, 1 ; Future controls are owned by the indicated tab
    ;Gui, Tab, 2 ; Future controls are owned by the indicated tab

    Gui, Show, % " w" GuiWidth " h" GuiHeight, % (GuiTitle := ApplicationName)
}

GuiSize(GuiHwnd, EventInfo, Width, Height) {
    ;TrayTip,, % "GuiHwnd: " GuiHwnd "`nEventInfo: " EventInfo "`nWidth: " Width "`nHeight: " Height
}
; ==============================================================================
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: New AutoHotkey Script Template

11 Mar 2016, 17:28

Can someone tell poor ignorant me how to edit shellnew/template.ahk? I suppose I'm just missing some simple concept, but I can't do anything to anything in that folder except read...
try it and see
...
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: New AutoHotkey Script Template

12 Mar 2016, 01:38

Search Google for the Take Ownership script. :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: New AutoHotkey Script Template

12 Mar 2016, 10:44

joedf wrote:Search Google for the Take Ownership script. :)
:P Does Microsoft like that? :D :lol: Really what are they afraid of, not letting you have ownership? :roll:
Worked great for me though, thanks.
try it and see
...

Return to “Off-topic Discussion”

Who is online

Users browsing this forum: No registered users and 95 guests