Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 09, 2016, 09:10:06 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Finally started really using Template.ahk  (Read 2704 times)

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Finally started really using Template.ahk
« on: June 24, 2011, 12:01:44 AM »
Duh!!  After copying and pasting Tray Menu stuff, about box code, yadda yadda that I put in 90% of my AHK scripts, I finally made them generic and put them in Template.ahk.

So now when I right click in Explorer to make a new ahk script, then edit, it has all that custom tray menu, name the .ini file boiler plate already in.



AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,115
    • View Profile
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #1 on: June 24, 2011, 01:28:42 AM »
Good idea  :Thmbsup:

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,785
    • View Profile
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #2 on: June 24, 2011, 01:50:42 AM »
If you are as productive as you've been the last couple of weeks (months) it's these 'tricks' that keeps your productivity on par :up:

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #3 on: June 24, 2011, 02:13:46 AM »
Thanks for the comments. Sometimes I'm a cup short of a coffee or I would have thought of it earlier. :)



jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #4 on: June 24, 2011, 04:32:18 AM »
Miles, maybe you could share the template so that others may have a starting point for their templates? ;)

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,368
    • View Profile
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #5 on: June 24, 2011, 01:40:59 PM »
Sometimes I'm a cup short of a coffee or I would have thought of it earlier.

You just need a little sweetener...

40hz

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 11,768
    • View Profile
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #6 on: June 24, 2011, 03:30:31 PM »
Sometimes I'm a cup short of a coffee or I would have thought of it earlier.

You just need a little sweetener...

+1 ;D

Sweet dreams and caffeine is what the tech world runs on.

(Not to be confused with three fingers of single-malt Scotch and a dry bowl of Corn Flakes: the REAL "Breakfast of Champions." :P )

« Last Edit: June 24, 2011, 03:32:23 PM by 40hz »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #7 on: June 24, 2011, 04:21:57 PM »
Miles, maybe you could share the template so that others may have a starting point for their templates? ;)

That's no problem.  Usually if Scite4AHK is installed there's a file in Windows\ShellNew folder called Template.ahk.  Next I have to work on templates for AutoIt3 and FreeBASIC.  Although FB is more likely to be command line unless I start playing around with the dialog resource editors.

Here's my Template.ahk.  Also since it uses a few routines in MilesAhead.ahk I'll include that as it helps to make some things generic.

Template.ahk

; AutoHotkey Script Template for MilesAhead
;#SingleInstance force
#SingleInstance ignore
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#Include MilesAhead.ahk
progname := _FileBaseName(A_ScriptFullPath)
Menu Tray,NoStandard
Menu Tray,Add,Donate,DoDonate
Menu Tray,Add,Visit Hotkey Page,DoVisit
Menu Tray,Add,About,DoAbout
Menu Tray,Add
Menu Tray,Add,Quit,DoQuit
Menu Tray,Default,About
Menu Tray,Tip,%progname%

; boiler plate ini file naming
;
;~ IniFile = %A_ScriptDir%\%A_ScriptName%
;~ IniFile := RegExReplace(IniFile,"i)ahk$","ini")
;~ IniFile := RegExReplace(IniFile,"i)exe$","ini")


;change hotkey and do something here
!+F10::
Return

DoAbout:
  FileGetVersion,filever,%progname%.exe
  MyMsg =
  (
%progname% v %filever% Copyright (c) 2011 www.FavesSoft.com`n
Enter Usage info here`n
  )
  MsgBox, 4160, About %progname%, %MyMsg%
Return

DoDonate:
  Run,"http://www.favessoft.com/donate.html"
Return

DoVisit:
  Run,"http://www.favessoft.com/hotkeys.html"
Return

DoQuit:
  ExitApp

MilesAhead.ahk

;
; MilesAhead include script with a few system related functions
;

; returns non 0 if program is running
_IsRunning(program)
{
Process,Exist,%program%
Return errorlevel
}

_LoWord(arg)
  {
    Return arg & 0xFFFF
  }

_HiWord(arg)
  {
    Return arg >> 16
  }

_LoByte(arg)
  {
    Return arg & 0x00FF
  }

_HiByte(arg)
  {
    Return (arg & 0xFF00) >> 8
  }

_WinVersionMajor()
  {
    Return _LoByte(_LoWord(DllCall("kernel32.dll\GetVersion", "UInt", -1)))
  }

_WinVersionMinor()
  {
    Return _HiByte(_LoWord(DllCall("kernel32.dll\GetVersion", "UInt", -1)))
  }

_WinVersion()
  {
    dwVersion := DllCall("kernel32.dll\GetVersion", "UInt", -1)
    Return _LoByte(_LoWord(dwVersion)) . "."
    . _HiByte(_LoWord(dwVersion))
  }

;call EmptyWorkingSet in AHK
_EmptyWorkingSet()
  {
    Return DllCall("psapi.dll\EmptyWorkingSet", "UInt", -1)
  }

;reduce memory footprint by calling EmptyWorkingSet
_ReduceMemory()
  {
    If _WinVersionMajor() > 4
    {
      Return _EmptyWorkingSet()
    }
    Return false
  }

;function by HotKeyIt on AHK forums
FormatMessageFromSystem(ErrorCode)
  {
    VarSetCapacity(Buffer, 2000)
    DllCall("FormatMessage"
            , "UInt", 0x1000      ; FORMAT_MESSAGE_FROM_SYSTEM
            , "UInt", 0
            , "UInt", ErrorCode
            , "UInt", 0x800 ;LANG_SYSTEM_DEFAULT (LANG_USER_DEFAULT=0x400)
            , "UInt", &Buffer
            , "UInt", 500
            , "UInt", 0)
    VarSetCapacity(buffer,-1)
    Return Buffer
  }

_GetWorkArea(ByRef left, ByRef top, ByRef right, ByRef bottom)
  {
    VarSetCapacity(work_area,16,0)
    success := DllCall( "SystemParametersInfo", "uint", 0x30, "uint", 0, "uint", &work_area, "uint", 0 )
    If success =
    {
      Return 0
    }
    left := NumGet(work_area,0)
    top := NumGet(work_area,4)
    Right := NumGet(work_area,8)
    Bottom := NumGet(work_area,12)
    Return 1
  }

_MarginWorkArea(ByRef left, ByRef top, ByRef right, ByRef bottom, ByRef margin = 4)
  {
    If _GetWorkArea(left, top, Right, Bottom)
    {
      If Margin is Integer
      {
        If (Margin < 0) or (Margin > 12)
          Margin := 4
      }
      Else
        Margin := 4
      left += Margin
      top += Margin
      Right -= Margin
      Bottom -= Margin
      Return 1
    }
    Return 0
  }

_TaskbarGapArea(ByRef left, ByRef top, ByRef right, ByRef bottom,  percentgap = 5)
{
  _GetWorkArea(left, top, right, bottom)
  If (right - left = A_ScreenWidth) And (bottom - top = A_ScreenHeight)
  {
    Return
  }
  Else If (bottom < A_ScreenHeight)
  {
    bottom -= (bottom // 100 * percentgap)
  }
  Else If (top > 0)
  {
    top += (bottom - top) // 100 * percentgap
  }
  Else If (left > 0)
  {
    left += (right - left) // 100 * percentgap
  }
  Else
 {
    right -= (right - left) // 100 * percentgap
 }
}

_GlassEnabled()
  {
    isEnabled = false
    If _WinVersionMajor() < 6
    {
      Return false
    }
    DllCall("dwmapi.dll\DwmIsCompositionEnabled", "UInt", &isEnabled)
    Return %isEnabled%
  }

_EnableBlurBehind(hwnd)
  {
    VarSetCapacity(struct, 16, 0)
    NumPut(1, struct, 0, "UInt")  ; dwFlags=DWM_BB_Enable
    NumPut(1, struct, 4,  "Int")  ; fEnable=TRUE
    DllCall("dwmapi.dll\DwmEnableBlurBehindWindow", "UInt", hwnd, "UInt", &struct)
  }

;returns string, wrapped in quotes when conataining spaces
_WrapQuotes(str)
  {
    If InStr(str," ")
    {
      str := """" . str . """"
    }
    Return str
  }

_FileBaseName(path)
  {
    SplitPath,path,,,,basename
    Return basename
  }

_FileDir(path)
  {
    SplitPath,path,,fdir
    Return fdir
  }

_FileDirWithSlash(path)
  {
    Return _FileDir(path) . "\"
  }

_CheckForUpdate(url="http://www.favessoft.com/",ininame="FavesVersions.ini")
  {
    urlfile := url . ininame
    localfile := A_ScriptDir . "\" . ininame
    param := _FileBaseName(A_ScriptFullPath)
    If (FileExist(localfile))
    {
      FileDelete,%localfile%
    }
    ;MsgBox, 4160, , localfile is %localfile%
    URLDownloadToFile,%urlfile%,%localfile%
    If ErrorLevel
    {
      MsgBox, 4112, %param%, Server access failed!
      Return
    }
    IniRead,onlinever,%localfile%,Versions,%param%,%A_Space%
    IniRead,zipfile,%localfile%,Downloads,%param%,%A_Space%
    FileGetVersion,localver,%A_ScriptFullPath%
    If localver =
    {
      MsgBox, 4112, %param%, local Version Info Not Found!
      Return
    }
    If onlinever =
    {
      MsgBox, 4112, %param%, Online Version Info Not Found!
      Return
    }
    ;MsgBox, 4160, URLDown Online Verson, %onlinever%
    ;MsgBox, 4160, URLDown local Verson, %localver%

    If (onlinever > localver)
    {
      If zipfile =
      {
        MsgBox, 4112, %param%, Could not determine Download Filename!
        Return
      }
      MsgBox, 0x1024, %param%, Current Version is %localver% : Download %onlinever% ?
      IfMsgBox Yes
      {
        urlfile := url . zipfile
        ziplocalfile := A_ScriptDir . "\" . zipfile
        URLDownloadToFile,%urlfile%,%ziplocalfile%
        If ErrorLevel
        {
          MsgBox, 4112, %param%, Download attempt failed!!
          Return
        }
        Run,%ziplocalfile%
        Run,%A_ScriptDir%
        If (FileExist(localfile))
        {
          FileDelete,%localfile%
        }
        ExitApp
      }
    }
    Else
    {
      MsgBox, 4160, %param%, %localver% is the Latest Version
    }
    If (FileExist(localfile))
    {
      FileDelete,%localfile%
    }
    Return
  }
« Last Edit: June 24, 2011, 04:23:36 PM by MilesAhead »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,290
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: Finally started really using Template.ahk
« Reply #8 on: June 24, 2011, 04:29:42 PM »
Sometimes I'm a cup short of a coffee or I would have thought of it earlier.

You just need a little sweetener...

Then I'd be sweet AND sour.   heh heh