Author Topic: Simple app to add menu items on Lubuntu  (Read 377 times)

SteveDee

  • Newbie
  • *
  • Posts: 33
Simple app to add menu items on Lubuntu
« on: February 04, 2012, 10:08:56 AM »
Lubuntu is a lightweight version of Ubuntu and does not include a menu editor. This appears to exercise many users on the Ubuntu forum, so here is a beginners project which simply allows menu items (apps and other shortcuts) to be added to the existing menu structure.

Menu items automatically appear in the Lubuntu menu structure when a .desktop file is added to: /usr/share/applications/

The essential elements of these files appear to be:-
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name={name of application}
Exec={command line}
Comment={a mouse-over type tooltip}
Categories={a category name}

...and here is a simplified example;
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Calculator
Exec=galculator
Comment=Its a calculator
Categories=Utility;

So all this simple program will do is allow the user to enter 3 parameters; Name, Command & Comment, and to select the destination menu.

One other point is that we need root priviliges to write the new .desktop file to: /usr/share/applications

To do this we launch this program using gksu (i.e. modify the launcher command for this application to: gksu /usr/bin/Launcher4Lulu.gambas).

This application needs the following components on the main form:-
Labels   (4)   to label the textboxes and listbox
Textbox   (3)   txtName, txtCommand, txtComment
Listbox   (1)   lstCategories
Checkbox (1)   chkTest
Button   (1)   btnSave

The code:-
Code: [Select]
' Gambas class file
'=======================================================
'Launcher4Lulu
'-------------
'This simple app can create menu entries to
' launch applications for Lubuntu.
'As it needs to write to /usr/share/applications
' it must be launched using gksu
' So after creating the .deb and installing, modify
' the Launcher4Lulu properties to include gksu
'i.e. Command: gksu /usr/bin/Launcher4Lulu.gambas
'
'steve davis
'Jan 2012
'========================================================
CONST FILE_HEAD AS String = "[Desktop Entry]"
CONST ENCODING AS String = "Encoding=UTF-8"
CONST TYPE AS String = "Type=Application"

PUBLIC SUB btnSave_Click()
DIM strName AS String     'this is both the menu name and file name
DIM strCommand AS String  'the command to launch your target app
DIM strComment AS String 
DIM strCategories AS String 'used to determine menu position
DIM strFileContent AS String  'contains complete .desktop file content
DIM hFile AS File
DIM strFilePath AS String     'tafget path will be /usr/share/applications/

  IF txtName.Text <> "" AND txtCommand.Text <> "" AND lstMenu.Text <> "" THEN
    'at least the important fields are not empty...
    strName = "Name=" & txtName.Text
    strCommand = "Exec=" & txtCommand.Text
    strComment = "Comment=" & txtComment.Text
    strCategories = lstMenu.Text
    'menu names may differ from Categories
    SELECT CASE lstMenu.Text
      CASE "Accessories"
      strCategories = "Categories=Utility;"
      CASE "Graphics"
      strCategories = "Categories=Graphics;"
      CASE "Internet"
      strCategories = "Categories=Network;"
      CASE "Office"
      strCategories = "Categories=Office;"
      CASE "Programming"
      strCategories = "Categories=Development;"
      CASE "Sound & Video"
      strCategories = "Categories=AudioVideo;"
      CASE "System Tools"
      strCategories = "Categories=System;"
      CASE "Preferences"
      strCategories = "Categories=Settings;"
      CASE ELSE
      strCategories = "Categories=Utility;"
    END SELECT
    ' Now build the file contents...
    strFileContent = FILE_HEAD & Chr(10) & ENCODING & Chr(10) & TYPE
    strFileContent = strFileContent & Chr(10) & strName & Chr(10) & strCommand
    strFileContent = strFileContent & Chr(10) & strComment & Chr(10) & strCategories & Chr(10)
   
    '***this block can be removed or commented out when testing is complete...
    Message.Info(strFileContent) '<<<this message line should be deleted after testing
    IF chkTest.Value THEN
      'test program without root priviliges
      strFilePath = "/home/steve/Desktop/"
    ELSE
      'will need root priviliges to write here...
      strFilePath = "/usr/share/applications/"
    ENDIF
    '***test block ends
   
    hFile = OPEN strFilePath & strName & ".desktop" FOR CREATE
    WRITE #hFile, strFileContent, Len(strFileContent)
    CLOSE #hFile   
  ELSE
    Message.Info("Please enter required data", "close")
  ENDIF
CATCH
  Message.Error("Ooops! We have a problem!")
END

Any application that has been added to the menu structure can then also be added to the Desktop or the Panel. So if you wanted a launcher in the Panel to load the BBC website, you would first create a menu entry with a command line like: firefox www.bbc.co.uk

This application could be extended to load existing .desktop files and move them within the menus by simply editing the Categories parameter.

Linux Basic

Simple app to add menu items on Lubuntu
« on: February 04, 2012, 10:08:56 AM »