AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template for structured coding

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Mon Jan 23, 2006 4:53 pm    Post subject: template for structured coding Reply with quote

Since I often start new projects with AHK, I began to work out a certain structure to keep my code organized.
So I've created a template that contains the basic structure for a new project as well as a few standard routines.

I'm not sure if anyone can make use of this (as everyone's got his own style, oviously*), but it provides me with an opportunity to test my new AutoHotkey.net account... Wink

Anyways, here we go:
Code:
/*
[program name] v[#].[#]
 by [author name]

[short description]
*/

/*
To Do:
ŻŻŻŻŻŻ
- [...]
*/

/*
Revision History
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
# v[#].[#] ([yyyy]-[mm]-[dd])
* initial release
*/

/*
********** Settings, Variable Declarations **********
*/

#SingleInstance Force
#NoEnv
OnExit, quit

programName = [program name] ; DEBUG: to do
programVersion = [#].[#] ; DEBUG: to do
programFullName = %programName% v%programVersion%
programAuthor = [author name] ; DEBUG: to do

/*
********** Auto-Execute Section **********
*/

; backup clipboard contents -- DEBUG: optional
clipboardBak := ClipboardAll
; process command line parameters -- DEBUG: optional
GoSub, getParams
; construct tray menu -- DEBUG: optional
GoSub, trayMenu
; end of auto-execute section
Return

/*
********** Hotkeys **********
*/

; DEBUG - note: hotkeys make the script persistent!

; [ALT]+[ESC]: terminate script
!ESC::
   Suspend ; exempt from suspension -- DEBUG: optional
   GoSub, quit
Return

/*
********** Subroutines **********
*/

; process command line parameters -- DEBUG: optional (see above)
getParams:
   If 0 > 0
   {
      Loop, %0% ; for each parameter
      {
         param := %A_Index%
         ; check for switches
         StringLeft, paramType, param, 1
         If paramType = - ; switch indicator
         {
            ; determine type of switch
            StringMid, switch, param, 2, 1
            ; switch
            If switch = x ; DEBUG: template (replace "x")
            {
               ; access value (= next parameter)
               param = % A_Index + 1
               var_x := %param% ; DEBUG: template (replace "var_x")
            }
         }
      }
   }
Return

; construct tray menu -- DEBUG: optional
trayMenu:
   ; set tray tip
   Menu, Tray, Tip, %programFullName%
   ; disable standard menu items
   Menu, Tray, NoStandard
   ; show info message
   Menu, Tray, Add, &About, about
   ; separator
   Menu, Tray, Add
   ; terminate script
   Menu, Tray, Add, &Quit, quit
Return

; show info message -- DEBUG: to do, optional
about:
   MsgBox, 64, %programFullName%,
   ( LTrim
      %programFullName%
      %A_Space%by %programAuthor%

      [short description]

      Use [ALT]+[ESC] to terminate the program.
   )
Return

; terminate script
quit:
   ; restore clipboard contents -- DEBUG: optional (see above)
   Clipboard := clipboardBak
   ; terminate script
   ExitApp
Return

/*
********** Functions **********
*/
(I'd suggest you make the file read only so you can easily start new projects off of it)


* ... and likely a much better one than my own, since I'm not a very experienced coder at all


UPDATE: (17:00 GMT)
Slightly altered the way command line parameters are processed.

UPDATE: (21:30 GMT)
Added subroutine for processing command line parameters (see this thread).

UPDATE: (2006-12-01)
Posted the latest version and replaced the link to the file with the actual code.

UPDATE: (2007-07-19)
Another update.

UPDATE: (2007-07-20)
Removed "Join" from about: routine's MsgBox (thanks engunneer); fixed typo.


Last edited by Ace_NoOne on Fri Aug 03, 2007 10:35 pm; edited 8 times in total
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Tue Jan 24, 2006 8:37 am    Post subject: Reply with quote

Thank you for sharing. It is always good practice to have a template. I hope new users will accept this help and learn from it.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
holomind



Joined: 11 Mar 2006
Posts: 300
Location: Munich, Germany

PostPosted: Mon Aug 07, 2006 9:13 pm    Post subject: Reply with quote

hi, the post is quite old, but the idea of using a template to start an ahk script instead of scratch is good.
i would also add a section for configuration, as i realize its quite important to have user-configuration when you compile your script and the user cannot change the config easily in sourcecode.
Back to top
View user's profile Send private message Visit poster's website
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Thu Jul 19, 2007 7:58 pm    Post subject: Reply with quote

I have (again) updated the original template and edited the initial posting accordingly.

holomind wrote:
hi, the post is quite old, but the idea of using a template to start an ahk script instead of scratch is good.
That's what I thought... Wink
holomind wrote:
i would also add a section for configuration, as i realize its quite important to have user-configuration when you compile your script and the user cannot change the config easily in sourcecode.
Well, I usually use external files for any user settings if required; the built-in IniRead makes that quite easy.
However, feel free to create your own version of this template if you like.
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Fri Jul 20, 2007 5:38 pm    Post subject: Reply with quote

you should remove the Join from the about box
Code:

; show info message -- DEBUG: to do, otpional
about:
   MsgBox, 64, %programFullName%,
   ( LTrim
      %programFullName%
      %A_Space%by %programAuthor%
     
      [short description]
     
      Use [ALT]+[ESC] to terminate the program.
   )
Return


then you don't have to have `n everywhere
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Fri Jul 20, 2007 6:07 pm    Post subject: Reply with quote

engunneer wrote:
you should remove the Join from the about box
[snip]
then you don't have to have `n everywhere
Good catch - why didn't I think of that?! (Probably a remnant from my earlier days... )
Done.

Thanks!
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6772
Location: Pacific Northwest, US

PostPosted: Fri Jul 20, 2007 6:33 pm    Post subject: Reply with quote

I also tend to keep settings in an INI file alot - maybe you can add a rough outline of generic ini reading?

I usually write mine from scratch each time, but I have seen functions posted in the forum that read every setting from the ini file automagically.
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Ace_NoOne



Joined: 10 Oct 2005
Posts: 333
Location: Germany

PostPosted: Fri Jul 20, 2007 7:46 pm    Post subject: Reply with quote

engunneer wrote:
I also tend to keep settings in an INI file alot - maybe you can add a rough outline of generic ini reading?

I usually write mine from scratch each time, but I have seen functions posted in the forum that read every setting from the ini file automagically.
I write mine from scratch too - and to be honest, I usually like it that way.
IniWrite is extremely easy to use, so I don't see much of a benefit there.

However, feel free to create a fork - if I like it, I might be tempted to adopt it after all...
_________________
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey
Back to top
View user's profile Send private message
Superfraggle



Joined: 02 Nov 2004
Posts: 829
Location: London, UK

PostPosted: Fri Jul 20, 2007 10:32 pm    Post subject: Reply with quote

Ive just added somefunctions for loading and saving ini files.

see here
_________________
Steve F AKA Superfraggle

http://r.yuwie.com/superfraggle
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group