Library: AllProgramsMenu

Tested with opsi 4.0.2
required WInst Version: 4.11.3
By Frank Thommen, 2014/04/24 20:25

Tree:

%SCRIPTDRIVE%\lib\
  ├ UseAllProgramsMenu.sub
  ├ AllProgramsMenu_Variables.opsiinc
  └ AllProgramsMenu_Functions.opsiinc

UseAllProgramsMenu.sub

;
; -------------------------------------------------------------
;  UseAllProgramsMenu.sub - Manage "All Programs" Menu Entries
; -------------------------------------------------------------
;
; AUTHOR: Frank Thommen, EMBL Heidelberg
; SHORTNAME (for variable names): allprogramsmenu
;
; PREREQUISITE: none
;
;
; PURPOSE: Manage entries in the global "All Programs" menu. Entries
;          can be created and removed in a easy fashion
;     
;
; USAGE:
;          ; Create a menu All Programs -> My Menu -> MyApp
;          ; pointing to C:\Program Files (x86)\My Application\A.exe
;          ;
;          Sub "%SCRIPTDRIVE%\lib\UseAllProgramsMenu.sub"
;           Set $_allprogramsmenu.entry      = "My Menu\MyApp"
;           Set $_allprogramsmenu.targetfile = "C:\Program Files (x86)\My Application\A.exe"
;           Set $_allprogramsmenu.targetwd   = ""
;          Sub_AddAllProgramsMenuEntry
;
;          ; Remove All Programs -> My Menu -> MyApp and cleanup the
;          ; "My Menu" folder if it is empty
;          ;
;          Sub "%SCRIPTDRIVE%\lib\UseAllProgramsMenu.sub"
;           Set $_allprogramsmenu.entry = "My Menu\MyApp"
;          Sub_DeleteAllProgramsMenuEntry
;
;          The target working directory ("Start In" in the links' properties)
;          can be controlled via $_allprogramsmenu.targetwd:
;
;            Use the base directory of $_allprogramsmenu.targetfile:
;            $_allprogramsmenu.targetwd = ""
;
;            Use a custom base directory:
;            $_allprogramsmenu.targetwd = "C:\a\b\c"
;
;            Use no base directory:
;            $_allprogramsmenu.targetwd = "none"
;
;
; POSSIBLE ISSUES:
;          Sub_AddAllProgramsMenuEntry overwrites already existing
;          entries!
;
; HISTORY: 12-JUN-2014: Initial version
;
;
 
include_insert "%SCRIPTDRIVE%\lib\AllProgramsMenu_Variables.opsiinc"
include_append "%SCRIPTDRIVE%\lib\AllProgramsMenu_Functions.opsiinc"

AllProgramsMenu_Variables.opsiinc

DefVar $_allprogramsmenu.entry
Set    $_allprogramsmenu.entry = ""
 
DefVar $_allprogramsmenu.targetfile
Set    $_allprogramsmenu.targetfile = ""
 
DefVar $_allprogramsmenu.targetwd
Set    $_allprogramsmenu.targetwd = ""

AllProgramsMenu_Functions.opsiinc

;
; AddStartMenu -- Add a new entry in the "All Programs" menu
;
[Sub_AddAllProgramsMenuEntry]
ScriptErrorMessages = off
FatalOnSyntaxError  = false
 DefVar        $__allprogramsmenu.menuname
 DefVar        $__allprogramsmenu.cmdname
 DefStringList $__allprogramsmenu.entries
ScriptErrorMessages = on
FatalOnSyntaxError  = true
 
 
if NOT ($_allprogramsmenu.entry = "")
 
  ; Sanitize $_allprogramsmenu.targetwd
  ;
  if ($_allprogramsmenu.targetwd = "")
    Set $_allprogramsmenu.targetwd = ExtractFilePath($_allprogramsmenu.targetfile)
  else
    if ($_allprogramsmenu.targetwd = "none")
      Set $_allprogramsmenu.targetwd = ""
    endif
  endif ;test
 
  Set $__allprogramsmenu.entries = splitString($_allprogramsmenu.entry, "\")
 
  ; The command is the last element...
  Set $__allprogramsmenu.cmdname = takestring(-1, $__allprogramsmenu.entries)
  ; ...while the rest is the path
  Set $__allprogramsmenu.menuname = composeString(getSubList(:-2, $__allprogramsmenu.entries), "\")
 
  LinkFolder_AllProgramsMenu_AddAllProgramsMenuEntryLink
 
  ; Clean up variables to avoid unwanted settings in case Sub_AddAllProgramsMenuEntry
  ; is run again
  ;
  Set $__allprogramsmenu.menuname  = ""
  Set $__allprogramsmenu.cmdname   = ""
  Set $__allprogramsmenu.entries   = createStringList("")
  Set $_allprogramsmenu.entry      = ""
  Set $_allprogramsmenu.targetwd   = ""
  Set $_allprogramsmenu.targetfile = ""
 
else
  COMMENT '****** ERROR in Sub_AddAllProgramsMenuEntry: Skipped creation of empty "All Programs" entry! ******'
 
endif
;
; --- End Sub_AddAllProgramsMenuEntry ---
 
 
;
; Add a specific All Programs menu entry
;
[LinkFolder_AllProgramsMenu_AddAllProgramsMenuEntryLink]
set_basefolder common_programs
set_subfolder "$__allprogramsmenu.menuname"
set_link
  name:        "$__allprogramsmenu.cmdname"
  target:      "$_allprogramsmenu.targetfile"
  parameters:
  working_dir: "$_allprogramsmenu.targetwd"
  icon_file:   "$_allprogramsmenu.targetfile"
  icon_index:
end_link
 
 
 
;================================================
 
 
;
; Delete a specific All Programs menu entry specified by
; $__allprogramsmenu.entry
;
[Sub_DeleteAllProgramsMenuEntry]
ScriptErrorMessages = off
FatalOnSyntaxError  = false
 DefVar        $__allprogramsmenu.menuname
 DefVar        $__allprogramsmenu.cmdname
 DefStringList $__allprogramsmenu.entries
ScriptErrorMessages = on
FatalOnSyntaxError  = true
 
if NOT ($_allprogramsmenu.entry = "")
 
  Set $__allprogramsmenu.entries = splitString($_allprogramsmenu.entry, "\")
 
  ; The command is the last element...
  Set $__allprogramsmenu.cmdname = takestring(-1, $__allprogramsmenu.entries)
  ; ...while the rest is the path
  Set $__allprogramsmenu.menuname = composeString(getSubList(:-2, $__allprogramsmenu.entries), "\")
 
  LinkFolder_AllProgramsMenu_RemoveAllProgramsMenuEntryLink
  Sub_CleanupAllProgramsMenu
 
  ; Clean up variables to avoid unwanted settings in case Sub_DeleteAllProgramsMenuEntry
  ; is run again
  ;
  Set $__allprogramsmenu.menuname  = ""
  Set $__allprogramsmenu.cmdname   = ""
  Set $__allprogramsmenu.entries   = createStringList("")
  Set $__allprogramsmenu.dirs      = createStringList("")
  Set $__allprogramsmenu.rmpath    = ""
  Set $_allprogramsmenu.entry      = ""
  Set $_allprogramsmenu.targetwd   = ""
  Set $_allprogramsmenu.targetfile = ""
 
else
  COMMENT '****** ERROR in Sub_DeleteAllProgramsMenuEntry: Skipped removal of empty "All Programs" entry! ******'
 
endif
;
; --- End Sub_DeleteAllProgramsMenuEntry ---
 
 
;
; Start from "%CommonStartMenuPath%\Programs\$_allprogramsmenu.name" down
; to "%CommonStartMenuPath%\Programs\" and remove all empty directories
;
[Sub_CleanupAllProgramsMenu]
ScriptErrorMessages = off
FatalOnSyntaxError  = false
 DefStringList $__allprogramsmenu.dirs
 DefVar        $__allprogramsmenu.rmpath
ScriptErrorMessages = on
FatalOnSyntaxError  = true
 
Set $__allprogramsmenu.dirs = splitString($__allprogramsmenu.menuname, "\")
for %s% in $__allprogramsmenu.dirs do Sub_AllProgramsMenu_CleanupAllProgramsMenu
 
;
; Construct path to the directory to be currently scheduled for removal 
;
[Sub_AllProgramsMenu_CleanupAllProgramsMenu]
Set $__allprogramsmenu.rmpath = "%CommonStartMenuPath%\Programs\" + composeString($__allprogramsmenu.dirs, "\")
DosInAnIcon_rmdir
Set $__allprogramsmenu.dirs = getSubList(:-2, $__allprogramsmenu.dirs)
 
;
; Remove a specific directory only if it is empty
;
[DosInAnIcon_rmdir]
rmdir "$__allprogramsmenu.rmpath"
 
 
;
; Remove a specific link
;
[LinkFolder_AllProgramsMenu_RemoveAllProgramsMenuEntryLink]
set_basefolder common_programs
set_subfolder "$__allprogramsmenu.menuname"
delete_element "$__allprogramsmenu.cmdname"

QR Code
QR Code userspace:library:allprogramsmenu (generated for current page)