Creating a function to shorten script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Ocelot
Posts: 8
Joined: 04 Oct 2020, 16:21

Creating a function to shorten script?

12 Jan 2024, 00:07

I can't think of how to properly create a function to shorten this bit of code here to toggle menu trays checks which disable sections of a script. Any help with that?

Code: Select all

#Persistent
#SingleInstance force

Menu, Tray, NoStandard
Menu, Tray, Add, Section 1, Section1
Menu, Tray, Add, Section 2, Section2
Menu, Tray, Add, 
Menu, Tray, Add, Exit, Exit
Return

Section1:
If (Section1 = 1)
{
Menu, Tray, Uncheck, Section 1
Section1 = 2
}
Else
{
Menu, Tray, Check, Section 1
Menu, Tray, Uncheck, Section 2
Section1 = 1
Section2 = 2
}
Return

Section2:
If (Section2 = 1)
{
Menu, Tray, Uncheck, Section 2
Section2 = 2
}
Else
{
Menu, Tray, Uncheck, Section 1
Menu, Tray, Check, Section 2
Section1 = 2
Section2 = 1
}
Return

Exit:
ExitApp

#If (Section1 = 1) && WinActive("ahk_exe NOTEPAD.EXE")

$F1::
Msgbox, Hello Section One
Return

#If (Section2 = 1) && WinActive("ahk_exe NOTEPAD.EXE")

$F1::
Msgbox, Hello Section Two
Return
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Creating a function to shorten script?  Topic is solved

12 Jan 2024, 00:48

Code: Select all

#Requires AutoHotkey v1.1.35
Global selection := 0
Menu Tray, NoStandard
Menu Tray, Add, Section 1, S1
Menu Tray, Add, Section 2, S2
Menu Tray, Add
Menu Tray, Add, Exit, Done

#If WinActive("ahk_exe notepad.exe")
F1::MsgBox 64, Selection, % selection
#If

S1:
S2:
toggle(SubStr(A_ThisLabel, 0))
Return

Done:
ExitApp

toggle(section) {
 Switch {
  Case section = selection:
   Menu Tray, Uncheck, % "Section " section
   selection := 0
  Case !selection:
   Menu Tray, Check  , % "Section " selection := section
  Default:
   Menu Tray, Check  , % "Section " selection := section
   Menu Tray, Uncheck, % "Section " 3 - section
 }
 SoundBeep 900 + 300 * selection
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 201 guests