Jump to content

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

Loop parse to create menu


  • Please log in to reply
10 replies to this topic
Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012
This script parse a string as %MenuLevel1% %MenuLevel2% %MenuLevel3% (separate by a ">" ) then add to a menu
MenuList=
(
AHK > Reload AHK > Script.ahk
AHK > Environment > Environment.ahk
AHK > Stop AHK > E:\2System\Nirsoft\nircmd\nircmd.exe killprocess autohotkey.exe
AHK > Restart Explorer > E:\2System\Nirsoft\nircmd\nircmd.exe restartexplorer
AHK > RegHack > %A_ScriptDir%\RegHack.ahk
AHK > AHKspy > E:\7Utilities\Launcher\AutoHotkey\AU3_Spy.exe
Nirsoft1 > Curr.Process  > E:\2System\Nirsoft\Current Process\CProcess.exe
Nirsoft2 > My Uninstaller  > E:\2System\Nirsoft\My Uninstaller\myuninst.exe
)

Loop, parse, MenuList, `n
{
  StringSplit, MenuLevel,A_LoopField, >
  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
}

Menu, MyMenu, Show

Action:
Msgbox, %MenuLevel3%
exitapp
However, whichever menu item i select, the action (%MenuLevel3%) is always E:\2System\Nirsoft\My Uninstaller\myuninst.exe
Is there any way to retrieve the correct %MenuLevel3%?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Your problem is that you're showing a variable that is not really realted to the menu you clicked. (Msgbox, %MenuLevel3%)
If you used MyArray instead of MenuLevel, you'd be showing Msgbox, %MyArray3%.
See A_ThisMenu* stuff here: <!-- m -->http://www.autohotke...Variables.htm#h<!-- m -->

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012
I tried the array method as below:
MenuList=
(
AHK > Reload AHK > Script.ahk
AHK > Environment > Environment.ahk
AHK > Stop AHK > E:\2System\Nirsoft\nircmd\nircmd.exe killprocess autohotkey.exe
AHK > Restart Explorer > E:\2System\Nirsoft\nircmd\nircmd.exe restartexplorer
AHK > RegHack > %A_ScriptDir%\RegHack.ahk
AHK > AHKspy > E:\7Utilities\Launcher\AutoHotkey\AU3_Spy.exe
Nirsoft1 > Curr.Process  > E:\2System\Nirsoft\Current Process\CProcess.exe
Nirsoft2 > My Uninstaller  > E:\2System\Nirsoft\My Uninstaller\myuninst.exe
)

Loop, parse, MenuList, `n
{
  StringSplit, MenuLevel,A_LoopField, >
  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
Command%A_Index% := MenuLevel3
}

Menu, MyMenu, Show

Action:
Msgbox, % Command%A_Thismenuitempos%
exitapp
This works fine for the first menu item (AHK) but not for Nirsoft1 and Nirsoft2. Everytime I click on Nirsoft1 > Curr.Process, it comes up with Script.ahk.
The problm is %A_Thismenuitempos% is return to 1

  • Guests
  • Last active:
  • Joined: --
Look at this
Action:

MsgBox % A_ThisMenu ">" A_ThisMenuItem ">" A_ThisMenuItempos
you see your problem and how to solve it?

Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012
Thanks, I solved it:
MenuList=

(

AHK > Reload AHK > Script.ahk

AHK > Environment > Environment.ahk

AHK > Stop AHK > E:\2System\Nirsoft\nircmd\nircmd.exe killprocess autohotkey.exe

AHK > Restart Explorer > E:\2System\Nirsoft\nircmd\nircmd.exe restartexplorer

AHK > RegHack > %A_ScriptDir%\RegHack.ahk

AHK > AHKspy > E:\7Utilities\Launcher\AutoHotkey\AU3_Spy.exe

Nirsoft1 > Curr.Process  > E:\2System\Nirsoft\Current Process\CProcess.exe

Nirsoft2 > My Uninstaller  > E:\2System\Nirsoft\My Uninstaller\myuninst.exe

)



Loop, parse, MenuList, `n

{

  StringSplit, MenuLevel,A_LoopField, >

  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action

  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%

  Command := varize(MenuLevel2)   ;Remove illegal characters

  %Command% = %MenuLevel3%

}



Menu, MyMenu, Show

sleep, 2000

exitapp



Action:

selected := varize(A_ThisMenuItem)

Run % %selected%

exitapp



varize(var, autofix = true) {

   Loop, Parse, var

   { c = %A_LoopField%

      x := Asc(c)

      If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)

         or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"

         IfEqual, autofix, 1, SetEnv, nv, %nv%%c%

         Else er++

   } If StrLen(var) > 254

      IfEqual, autofix, 1, StringLeft, var, var, 254

      Else er++

   IfEqual, autofix, 1, Return, nv

   Else Return, er

}


Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012
Functionality added: Now you can define 3rd level sub-menu: example: Utilitites > Magnify > sssxs> E:\7Utilities\magnify.exe
MenuList=
(
AHK > Reload AHK > Script.ahk
AHK > Environment > Environment.ahk
AHK > Stop AHK > E:\2System\Nirsoft\nircmd\nircmd.exe killprocess autohotkey.exe
AHK > Restart Explorer > E:\2System\Nirsoft\nircmd\nircmd.exe restartexplorer
AHK > RegHack > %A_ScriptDir%\RegHack.ahk
AHK > AHKspy > E:\7Utilities\Launcher\AutoHotkey\AU3_Spy.exe
Nirsoft1 > Curr.Process  > E:\2System\Nirsoft\Current Process\CProcess.exe
Nirsoft2 > My Uninstaller > asdas > E:\2System\Nirsoft\My Uninstaller\myuninst.exe
)

Loop, parse, MenuList, `n

{
  StringSplit, MenuLevel,A_LoopField, >

if(MenuLevel4 = "" )
{
  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
  Command := varize(MenuLevel2)   ;Remove illegal characters
  %Command% = %MenuLevel3%
}
else
{
  Menu, %MenuLevel2%, Add, %MenuLevel3%, Action
  Menu, %MenuLevel1%, Add, %MenuLevel2%, :%MenuLevel2%
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
  Command := varize(MenuLevel3)   ;Remove illegal characters
  %Command% = %MenuLevel4%
  VarSetCapacity(MenuLevel4,0)
}

}

Menu, MyMenu, Show
sleep, 2000
exitapp

Action:
selected := varize(A_ThisMenuItem)
Run % %selected%
exitapp
varize(var, autofix = true) {
   Loop, Parse, var
   { c = %A_LoopField%
      x := Asc(c)
      If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)
         or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"
         IfEqual, autofix, 1, SetEnv, nv, %nv%%c%
         Else er++
   } If StrLen(var) > 254
      IfEqual, autofix, 1, StringLeft, var, var, 254
      Else er++
   IfEqual, autofix, 1, Return, nv
   Else Return, er
}


Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012
Functionality added:
Now you can use any command, not just limited to Run
MenuList=
(
AHK > Reload AHK > Run Script.ahk
AHK > Environment > Run Environment.ahk
System > Mute > Send {Volume_Mute}
)

Loop, parse, MenuList, `n
{
  StringSplit, MenuLevel,A_LoopField, >

if(MenuLevel4 = "" )
{
  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
  Command := varize(MenuLevel2)   ;Remove illegal characters
  %Command% = %MenuLevel3%
}
else
{
  Menu, %MenuLevel2%, Add, %MenuLevel3%, Action
  Menu, %MenuLevel1%, Add, %MenuLevel2%, :%MenuLevel2%
  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
  Command := varize(MenuLevel3)   ;Remove illegal characters
  %Command% = %MenuLevel4%
  VarSetCapacity(MenuLevel4,0)	;Reset Menulevel4 to blank
}

}

Menu, MyMenu, Show
sleep, 2000
exitapp

Action:
selected := varize(A_ThisMenuItem) ;Remove illegal characters
exe = % %selected%
DynaRun("#notrayicon `n"exe)
exitapp

DynaRun(TempScript, pipename="")
{
   static _:="uint",@:="Ptr"
   If pipename =
      name := "AHK" A_TickCount
   Else
      name := pipename
   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
   if (__PIPE_=-1 or __PIPE_GA_=-1)
      Return 0
   Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID
   If ErrorLevel
      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """"
   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0)
   DllCall("CloseHandle",@,__PIPE_GA_)
   DllCall("ConnectNamedPipe",@,__PIPE_,@,0)
   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) TempScript
   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0)
      Return A_LastError
   DllCall("CloseHandle",@,__PIPE_)
   Return PID
}

varize(var, autofix = true) {
   Loop, Parse, var
   { c = %A_LoopField%
      x := Asc(c)
      If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)
         or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"
         IfEqual, autofix, 1, SetEnv, nv, %nv%%c%
         Else er++
   } If StrLen(var) > 254
      IfEqual, autofix, 1, StringLeft, var, var, 254
      Else er++
   IfEqual, autofix, 1, Return, nv
   Else Return, er
}


Binocular222
  • Members
  • 99 posts
  • Last active: Jul 25 2014 04:28 AM
  • Joined: 26 Feb 2012

Functionality added: Show icon

You will need Menu Icon v2 here: http://www.autohotke...-menu-icons-v2/

#SingleInstance force    ;Skips the dialog box and replaces the old instance automatically
#notrayicon
stringleft, Adrive, A_ScriptDir, 2
XYplorerPath = E:\7Utilities\XYplorer\XYplorer.exe

;↓ Detect Win 64bits ↓
if (DllCall("IsWow64Process", "uint", DllCall("GetCurrentProcess"), "int*", isWow64process) && isWow64process  OR   IfInString, Processor_Architecture, 64)
{
NircmdPath = %A_ScriptDir%\nircmdx64\nircmd.exe
RegWorkshopPath = E:\2System\Registry Workshop\RegWorkshopX64.exe
UnlockerPath = E:\2System\Unlocker\Unlocker x64\Unlocker x64.exe
NK2EditPath = E:\2System\Nirsoft\nk2edit-x64\NK2Edit.exe
}
else
{
NircmdPath = %A_ScriptDir%\nircmdx32\nircmd.exe
RegWorkshopPath = E:\2System\Registry Workshop\RegWorkshop.exe
UnlockerPath = E:\2System\Unlocker\Unlocker x32\Unlocker.exe
NK2EditPath = E:\2System\Nirsoft\nk2edit\NK2Edit.exe
}
;↑ Detect Win 64bits ↑

;↓ Define main menu items ↓
MenuList=
(
&AHK > Reload AHK > Run Script.ahk
&AHK > Environment > Run Environment.ahk
&AHK > Running AHK Scripts > Run Running AHK scripts.ahk
&AHK > WhatColor > Run WhatColor.ahk
&AHK > Stop AHK > Run %NircmdPath% killprocess autohotkey.exe
&AHK > AHKspy > Run %Adrive%\7Utilities\Launcher\AutoHotkey\AU3_Spy.exe
&Nirsoft > CurrProcess > Run E:\2System\Nirsoft\Current Process\CProcess.exe
&Nirsoft > My Uninstaller > Run E:\2System\Nirsoft\My Uninstaller\myuninst.exe
&Nirsoft > Network traffic view  > Run E:\2System\Nirsoft\Network traffic view\NetworkTrafficView.exe
&Nirsoft > NK2Edit > Run %NK2EditPath%
&Nirsoft > OfficeIns > Run E:\2System\Nirsoft\OfficeIns\OfficeIns.exe
&Nirsoft > Opened Files View > Run E:\2System\Nirsoft\Opened files view\OpenedFilesView.exe
&Nirsoft > WhatInStartup > Run E:\2System\Nirsoft\What in startup\WhatInStartup.exe
&Nirsoft > Icon Extract > Run E:\2System\Nirsoft\Icons extract\iconsext.exe
&System > Autorun > Run E:\2System\Autoruns\autoruns.exe
&System > CCleaner > Run E:\2System\CCleaner\CCleaner.exe
&System > Hosts > Run %Editpad% C:\Windows\System32\drivers\etc\hosts
&System > Driver Genius > Run E:\2System\Driver Genius 12\DriverGenius\DriverGenius.exe
&System > MiniTool Partition Wizard > Run E:\2System\Partition management\MiniTool Partition Wizard Professional\PartitionWizard.exe
&System > Registry Workshop > Run %RegWorkshopPath%
&System > Unlocker > Run %UnlockerPath%
&System > UltraISO > Run E:\2System\Virtual drive\UltraISO 9.3 Portable.exe
&System > ScreenSaver > PostMessage, 0x112, 0xF140,0,, Program Manager
&System > Shutdown timing > Run Shutdown.ahk
&System > Core settings > Mute > Send {Volume_Mute}
&System > Core settings > Set 800x600 > Run %NircmdPath% setdisplay 800 600
&System > Core settings > Set 1024x768 > Run %NircmdPath% setdisplay 1024 768
&System > Core settings > Restart Explorer > Run %NircmdPath% restartexplorer
&Office > One Note Sort > Run E:\3Office\OneNote sort\OneNoteSort.exe
&Office > Communicator > Run C:\Program Files\Microsoft Office Communicator\communicator.exe
&Office > Kalkules > Run E:\3Office\Kalkules\Kalkules.exe
&Office > Able2Extract > Run E:\3Office\OCR, Converter\Able2Extract Professional 7.0.exe
&Office > WhiteSmoke > Run E:\3Office\Whitesmoke 2011\WhiteSmoke 2011 Portable\Launch WhiteSmoke.exe
&Office > PDF Password remover > Run E:\3Office\PDF PASSWORD REMOVER 3.0.EXE
&Office > Outlook attach view > Run E:\2System\Nirsoft\Outlook attach view\OutlookAttachView.exe
)
;↑ Define main menu items ↑

;↓ Add menu items from recent document ↓
Loop, %A_AppData%\Microsoft\Windows\Recent\*.lnk
{
FileGetShortcut, %A_LoopFileFullPath%, OutTarget
SplitPath, OutTarget,,,FileExtension,Basename
If FileExtension
RecentItems = %RecentItems%%A_LoopFileTimeAccessed%`t%Basename%`t%OutTarget%`t%FileExtension%`n
}

Loop, %A_AppData%\Microsoft\Office\Recent\*.lnk
{
FileGetShortcut, %A_LoopFileFullPath%, OutTarget
SplitPath, OutTarget,,,FileExtension,Basename
If FileExtension
RecentItems = %RecentItems%%A_LoopFileTimeAccessed%`t%Basename%`t%OutTarget%`t%FileExtension%`n
}

Sort, RecentItems, R         ;Sort in reverse order (by time)

Loop, parse, RecentItems, `n
{
    if ProcessedItem > 25
        break
    if A_LoopField = ""  ; Omit the last linefeed (blank item) at the end of the list.
        continue

    StringSplit, FileItem, A_LoopField, %A_Tab%  ; Split into parts at the tab char.
    ProcessedItem++
    Menu, RecentDoc, Add, %FileItem2%, ShowRecentItemsAction
    MI_SetMenuItemIcon("RecentDoc", ProcessedItem, AssocQueryApp(FileItem4), 1, 16)
}
    Menu, RecentDoc, Add
    Menu, RecentDoc, Add, Goto Recent Doc folder, GotoRecentItemsAction
    Menu, RecentDoc, Add, Clear all, ClearRecentItemsAction
    Menu, RecentDoc, Add, Clear invalid, InvalidRecentItemsAction
    Menu, MyMenu, Add, RecentDoc, :RecentDoc
;↑ This is for recent document ↑

;↓ Add menu items from MenuList above ↓
Loop, parse, MenuList, `n
{
  StringSplit, MenuLevel,A_LoopField, >
    Menulevel1 = %MenuLevel1%	;Remove leading and trailing space thus avoid duplicate menu
    Menulevel2 = %MenuLevel2%	;Remove leading and trailing space thus avoid duplicate menu
  if(MenuLevel4 = "" )
    {
    Menu, %MenuLevel1%, Add, %MenuLevel2%, Action
    Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
    Command := varize(MenuLevel2)   ;Remove illegal characters
    %Command% = %MenuLevel3%
    if( InStr(MenuLevel3, ".exe") AND InStr(MenuLevel3, "Run") )
    IconPath := trim(SubStr(MenuLevel3, InStr(MenuLevel3, "Run") + 3, InStr(MenuLevel3, ".exe") + 1 - InStr(MenuLevel3, "Run")))
    Else If( InStr(MenuLevel3, "Run"))
    {
    Temp := trim(SubStr(MenuLevel3, InStr(MenuLevel3, "Run") + 3))
    SplitPath, Temp,,,FileExtension
    if( FileExtension = "Lnk")
    FileGetShortcut, %Temp%, IconPath
    Else
    IconPath := AssocQueryApp(FileExtension)
    }
    MI_SetMenuItemIcon(MenuLevel1, A_LoopField, IconPath, 1, 16)
    }
  else
    {
    Menu, %MenuLevel2%, Add, %MenuLevel3%, Action
    Menu, %MenuLevel1%, Add, %MenuLevel2%, :%MenuLevel2%
    Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%
    Command := varize(MenuLevel3)   ;Remove illegal characters
    %Command% = %MenuLevel4%
    if( InStr(MenuLevel4, ".exe") AND InStr(MenuLevel4, "Run") )
    {
    IconPath := trim(SubStr(MenuLevel4, InStr(MenuLevel4, "Run") + 3, InStr(MenuLevel4, ".exe") + 1 - InStr(MenuLevel4, "Run")))
    MI_SetMenuItemIcon(MenuLevel2, A_Index, IconPath, 1, 16)
    }
    VarSetCapacity(MenuLevel4,0)	;Reset Menulevel4 to blank
    }
}
;↑ Process MenuList above ↑

;↓ Add menu items from RegHack folder ↓
Loop, %A_ScriptDir%\RegHack\*.*
{
Menu, RegHack, Add, %A_LoopFileName%, ReghackAction
Menu, &System, Add, RegHack, :RegHack
Menu, MyMenu, Add, &System, :&System
%Command% = varize(A_LoopFileName)
IconPath := AssocQueryApp(A_LoopFileExt)
MI_SetMenuItemIcon("RegHack", A_Index, IconPath, 1, 16)
}
;↑ Read RegHack folder ↑

;Menu, MyMenu, Check, AHK
;Menu, AHK, Check, Reload AHK		For cosmetic only
CoordMode, Menu, Screen
X := A_ScreenWidth/2.3
Y := A_ScreenHeight/3.3
Menu, MyMenu, Show, %X%, %Y%
exitapp

Action:
selected := varize(A_ThisMenuItem) ;Remove illegal characters
exe = % %selected%
DynaRun("#notrayicon `n"exe)
exitapp

ReghackAction:
run Reghack\%A_ThisMenuItem%
exitapp

;↓ Recent document Action ↓
GotoRecentItemsAction:
If A_ThisMenuItem =
return
else
Run, %XYplorerPath% %A_AppData%\Microsoft\Windows\Recent
Run, %XYplorerPath% %A_AppData%\Microsoft\Office\Recent
exitapp

ClearRecentItemsAction:
FileDelete, %A_AppData%\Microsoft\Windows\Recent\*.lnk
FileDelete, %A_AppData%\Microsoft\Office\Recent\*.lnk
exitapp

ShowRecentItemsAction:
Run, %A_AppData%\Microsoft\Office\Recent\%A_ThisMenuItem%.Lnk, , UseErrorLevel
Run, %A_AppData%\Microsoft\Windows\Recent\%A_ThisMenuItem%.Lnk, , UseErrorLevel
exitapp

InvalidRecentItemsAction:
Loop, %A_AppData%\Microsoft\Windows\Recent\*.lnk
{
FileGetShortcut, %A_LoopFileFullPath%, RealFile
Ifnotexist, %RealFile%
FileDelete, %A_LoopFileFullPath%
}

Loop, %A_AppData%\Microsoft\Office\Recent\*.lnk
{
FileGetShortcut, %A_LoopFileFullPath%, RealFile
Ifnotexist, %RealFile%
FileDelete, %A_LoopFileFullPath%
}
exitapp
;↑ This is for recent document ↑

;Function Library:
AssocQueryApp(ext) {
	RegRead, type, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.%Ext%, Application
	If !ErrorLevel { ;Current user has overridden default setting
		RegRead, act, HKCU, Software\Classes\Applications\%type%\shell
		If ErrorLevel
			act = open
		RegRead, cmd, HKCU, Software\Classes\Applications\%type%\shell\%act%\command
		}
	Else {           ;Default setting
		RegRead, type, HKCR, .%Ext%
		RegRead, act , HKCR, %type%\shell
		If ErrorLevel
			act = open
	RegRead, cmd , HKCR, %type%\shell\%act%\command
	EXEPosition := InStr(cmd,".exe",false,0,1)
	exepath := Trim(SubStr(cmd,1,EXEPosition+3),"""")
}
 Return, exepath
}


DynaRun(TempScript, pipename="")
{
static _:="uint",@:="Ptr"
If pipename =
name := "AHK" A_TickCount
Else
name := pipename
__PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
__PIPE_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)
if (__PIPE_=-1 or __PIPE_GA_=-1)
Return 0
Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID
If ErrorLevel
MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """"
DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0)
DllCall("CloseHandle",@,__PIPE_GA_)
DllCall("ConnectNamedPipe",@,__PIPE_,@,0)
script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) TempScript
if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0)
Return A_LastError
DllCall("CloseHandle",@,__PIPE_)
Return PID
}

varize(var, autofix = true) {
Loop, Parse, var
{ c = %A_LoopField%
x := Asc(c)
If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)
or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"
IfEqual, autofix, 1, SetEnv, nv, %nv%%c%
Else er++
} If StrLen(var) > 254
IfEqual, autofix, 1, StringLeft, var, var, 254
Else er++
IfEqual, autofix, 1, Return, nv
Else Return, er
}


Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

Amazing script, I recommend you post it to scripts section.


Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


ahkbear
  • Members
  • 8 posts
  • Last active:
  • Joined: 22 Apr 2013

I tried to run this but it says:

 

---------------------------
Popup Menu2.ahk
---------------------------
Error:  Menu name too long.
 
Line#
087: @ := "Ptr"
003: MenuList = 
AHK > Reload AHK > Run Script.ahk
 
AHK > Environment > Run Environment.ahk
 
System > Mute > Send {Volume_Mute}
 
017: Loop,parse,MenuList,
 
019: {
021: StringSplit,MenuLevel,A_LoopField,>
025: if (MenuLevel4 = "" )  
027: {
---> 029: Menu,%MenuLevel1%,Add,%MenuLevel2%,Action
031: Menu,MyMenu,Add,%MenuLevel1%,:%MenuLevel1%
033: Command := varize(MenuLevel2)
035: %Command% = %MenuLevel3%
037: }
039: Else
041: {
043: Menu,%MenuLevel2%,Add,%MenuLevel3%,Action
 
The current thread will exit.
---------------------------
OK   
---------------------------
 

 

Functionality added:
Now you can use any command, not just limited to Run


MenuList=

(

AHK > Reload AHK > Run Script.ahk

AHK > Environment > Run Environment.ahk

System > Mute > Send {Volume_Mute}

)



Loop, parse, MenuList, `n

{

  StringSplit, MenuLevel,A_LoopField, >



if(MenuLevel4 = "" )

{

  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action

  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%

  Command := varize(MenuLevel2)   ;Remove illegal characters

  %Command% = %MenuLevel3%

}

else

{

  Menu, %MenuLevel2%, Add, %MenuLevel3%, Action

  Menu, %MenuLevel1%, Add, %MenuLevel2%, :%MenuLevel2%

  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%

  Command := varize(MenuLevel3)   ;Remove illegal characters

  %Command% = %MenuLevel4%

  VarSetCapacity(MenuLevel4,0)	;Reset Menulevel4 to blank

}



}



Menu, MyMenu, Show

sleep, 2000

exitapp



Action:

selected := varize(A_ThisMenuItem) ;Remove illegal characters

exe = % %selected%

DynaRun("#notrayicon `n"exe)

exitapp



DynaRun(TempScript, pipename="")

{

   static _:="uint",@:="Ptr"

   If pipename =

      name := "AHK" A_TickCount

   Else

      name := pipename

   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)

   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)

   if (__PIPE_=-1 or __PIPE_GA_=-1)

      Return 0

   Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID

   If ErrorLevel

      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """"

   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0)

   DllCall("CloseHandle",@,__PIPE_GA_)

   DllCall("ConnectNamedPipe",@,__PIPE_,@,0)

   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) TempScript

   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0)

      Return A_LastError

   DllCall("CloseHandle",@,__PIPE_)

   Return PID

}



varize(var, autofix = true) {

   Loop, Parse, var

   { c = %A_LoopField%

      x := Asc(c)

      If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)

         or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"

         IfEqual, autofix, 1, SetEnv, nv, %nv%%c%

         Else er++

   } If StrLen(var) > 254

      IfEqual, autofix, 1, StringLeft, var, var, 254

      Else er++

   IfEqual, autofix, 1, Return, nv

   Else Return, er

}


ahkbear
  • Members
  • 8 posts
  • Last active:
  • Joined: 22 Apr 2013

Also, how do you gosub instead of executing one command or invoking another ahk file? Thanks.

 

 

Functionality added:
Now you can use any command, not just limited to Run


MenuList=

(

AHK > Reload AHK > Run Script.ahk

AHK > Environment > Run Environment.ahk

System > Mute > Send {Volume_Mute}

)



Loop, parse, MenuList, `n

{

  StringSplit, MenuLevel,A_LoopField, >



if(MenuLevel4 = "" )

{

  Menu, %MenuLevel1%, Add, %MenuLevel2%, Action

  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%

  Command := varize(MenuLevel2)   ;Remove illegal characters

  %Command% = %MenuLevel3%

}

else

{

  Menu, %MenuLevel2%, Add, %MenuLevel3%, Action

  Menu, %MenuLevel1%, Add, %MenuLevel2%, :%MenuLevel2%

  Menu, MyMenu, Add, %MenuLevel1%, :%MenuLevel1%

  Command := varize(MenuLevel3)   ;Remove illegal characters

  %Command% = %MenuLevel4%

  VarSetCapacity(MenuLevel4,0)	;Reset Menulevel4 to blank

}



}



Menu, MyMenu, Show

sleep, 2000

exitapp



Action:

selected := varize(A_ThisMenuItem) ;Remove illegal characters

exe = % %selected%

DynaRun("#notrayicon `n"exe)

exitapp



DynaRun(TempScript, pipename="")

{

   static _:="uint",@:="Ptr"

   If pipename =

      name := "AHK" A_TickCount

   Else

      name := pipename

   __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)

   __PIPE_    := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0)

   if (__PIPE_=-1 or __PIPE_GA_=-1)

      Return 0

   Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID

   If ErrorLevel

      MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """"

   DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0)

   DllCall("CloseHandle",@,__PIPE_GA_)

   DllCall("ConnectNamedPipe",@,__PIPE_,@,0)

   script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) TempScript

   if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0)

      Return A_LastError

   DllCall("CloseHandle",@,__PIPE_)

   Return PID

}



varize(var, autofix = true) {

   Loop, Parse, var

   { c = %A_LoopField%

      x := Asc(c)

      If (x > 47 and x < 58) or (x > 64 and x < 91) or (x > 96 and x < 123)

         or c = "#" or c = "_" or c = "@" or c = "$" or c = "?" or c = "[" or c = "]"

         IfEqual, autofix, 1, SetEnv, nv, %nv%%c%

         Else er++

   } If StrLen(var) > 254

      IfEqual, autofix, 1, StringLeft, var, var, 254

      Else er++

   IfEqual, autofix, 1, Return, nv

   Else Return, er

}