User Tools

Site Tools


15.1 Class Desktop (gb.desktop)

The Desktop class of the component with the same name allows you to use desktop related routines based on the xdg-utils scripts of the Portland project. The number of properties and methods is manageable. Selected properties and methods are listed and described in a table:

ActiveWindow IntegerThe active current top-level window is returned or set.
CountIntegerThe number of virtual desktops is returned or set.
CurrentIntegerThe number of the current desktop is returned or set.
Passwords_DesktopPasswordsReturns a virtual object. At present, neither properties (except key) nor methods are known.
PathStringReturns the path to the desktop directory
RootWindowIntegerReturns the X11 handle of the root window
ScreenSaver_Desktop_ScreenSaverManage the screensaver
ShowingBooleanWith TRUE all windows are minimized and you can see the desktop
TypeStringReturns the name of the current desktop (KDE, KDE4, GNOME, XFCE or “?” for an unknown desktop)
Windows_Desktop_WindowsReturns a virtual object representing a list of all top-level windows

Table 15.1.1: Overview of properties of the Desktop class (gb.desktop)

The virtual class _Desktop_ScreenSaver allows you to manipulate the screen saver and has the following property:

_Desktop_ScreenSaver.Enabled → Returns TRUE if the screen saver is turned on.

The following is a list of the methods of the virtual class _Desktop_ScreenSaver and a short explanation of the action triggered:

  • _Desktop_ScreenSaver.Activate - Enables the screen saver immediately
  • _Desktop_ScreenSaver.Reset - Disables the screen saver immediately
  • _Desktop_ScreenSaver.Lock - Locks the screen saver immediately (→ unlock password)
  • _Desktop_ScreenSaver.Resume - screen saver function is activated
  • _Desktop_ScreenSaver.Suspend - screen saver function is deactivated

For a brief description of the methods of the Desktop class, see the following table, which contains a detailed description of selected methods in other (sub)chapters:

1FindWindow ([ Title As String, Application As String, Role As String]) As Integer[]Returns all X11 top-level windows in an array.
2GetFileIcon (Path As String, Size As Integer[, Preview As Boolean]) As PictureFor preview equal to False an icon of type Picture is returned from the specified file.
3Open (URL As String[, Wait AsBoolean])Opens a file or URL in the assigned standard application
4OpenTerminal ([ Dir As String]Opens a terminal in the current desktop environment. Optionally, a start directory can be specified.
5RunAsRoot (Command As String)Executes the specified command. The root password is requested in a terminal corresponding to the desktop.
6SendKeys (Keys As String)A keystroke or a combination of keys is generated. Keys is a list of keys to be generated one after the other.
7SendMail (To As String[] Cc As String[], Bcc As String[], Subject As String, Body As String, Attachment As String]An email is prepared with a recipient list, further recipient lists, the subject, the text content and an attachment.

Table 15.1.2: Overview of methods of the class Desktop (gb. desktop)

The FindWindow(..) method returns an array of X11 window identifiers as a function value. The following applies to the optional parameters in the method:

Desktop.FindWindow ( [ Title As String, Application As String, Role As String ] ) As Integer[]

Title is the window title. This can be found in the X11 window property WM_NAME.Application is the window class. It is usually the name of the application that creates the window. It is stored in the X11 window property WM_CLASS, which is the role of the window. It is stored in the X11 window property WM_WINDOW_ROLE.

The arguments Title, Application and Role are regular expressions as defined in conjunction with the LIKE operator (→ chapter 19.6.3 Excursus LIKE and chapter 8.6 Special Operators).

[1] Public Sub GetDTWindowsList()
[2]   Dim iCount As Integer
[3]   Dim DTWindow As DesktopWindow
[4]   Dim aDTWindowsList As Integer[]
[6]   txaWindowList.Clear
[7]   aDTWindowsList = Desktop.FindWindow(txbPatternBox.Text)
[8]   lblWindowCount.Text = aDTWindowsList.Count
[9]  For iCount = 0 To aDTWindowsList.Max
[10]      DTWindow = New DesktopWindow(aDTWindowsList[iCount])
[11]      If DTWindow.Id = aDTWindowsList[iCount] Then
[12]         txaWindowList.Text &= Str(iCount + 1) & "\t" & (DTWindow.Desktop + 1) & 
       "      " & DTWindow.Id & "\t" & DTWindow.Name & gb.NewLine
[13]      Endif ' DTWindow.Id = aDTWindowsList[iCount] ?
[14]  Next ' iCount
[16]  End ' GetDTWindowsList()
[18]  Public Sub btnGetDTWList_Click()
[19]    GetDTWindowsList()
[20]  End ' btnGetDTWList_Click()

The following results could be displayed in a TextArea. First, the regular expression [DGM]* (→ line 7) was used as the search pattern (window name begins with D, G or M):

Nummer	Desktop	Desktop.ID		Fenster-Name 
1	3      	52428816		DesktopWatcher 0.0.6 - Gambas 3 
2	1      	62914563		GHex 
3	4      	65011850		Mozilla Firefox 
4	3      	37748751		DesktopWatcher 

The search pattern was then set to * and resulted in the following extended list of X11 top-level windows:

Nummer	Desktop	Desktop.ID		Fenster-Name 
1	0      	23068676		Schreibtisch 
2	3      	52428816		DesktopWatcher 0.0.6 - Gambas 3 
3	1      	62914563		GHex 
4	1      	60817454		Unbenanntes Dokument 1 - gedit 
5	2      	48234565		15.1.odt - LibreOffice Writer 
6	4      	65011850		Mozilla Firefox 
7	3      	37748751		DesktopWatcher 
8	0      	14680084		Unteres Kanten-Panel, ausgedehnt 
9	0      	14680067		Oberes Kanten-Panel, ausgedehnt

The GetFileIcon(..)method returns the icon associated with the selected file:

  • Path is the file path
  • Size is the size of the icon (pixels)
  • Preview - The default value of the optional argument is FALSE and the icon file of type Picture is returned.
' Gambas class file
Public picFileIcon As Picture
Public Sub Form_Open()
  picFileIcon = Desktop.GetFileIcon(User.Home &/ "DesktopWatcher.gambas", 32, False)
' Saving the icon in the global variables picFileIcon of type Picture
  PictureBox1.W = 72
  PictureBox1.H = PictureBox1.W
  PictureBox1.Stretch = True
  PictureBox1.Picture = Desktop.GetFileIcon(User.Home &/ "Arbeitsfläche/GetFileIcon/GetFileIcon.gambas", 32, False)
End ' Form_Open

With the RunAsRoot(…) method, many people hoped to be able to execute a command that required root privileges quite simply from within a Gambas program. Implementation in a project is simple:

Public Sub RunAsRoot(sCommand As String)
End ' RunAsRoot(sCommand As String)
Public Sub btnRunAsRoot_Click()
  RunAsRoot("apt-get install bluefish")
End ' btnRunAsRoot_Click()

A terminal window opens and you are prompted to enter the root password:


Figure 15.1.1: Terminal

But currently (→ 30.11.2014) nothing happens after entering the root password. The terminal window closes and you return to the program window.

What it already is - at least under Ubuntu 12.04.

Internally, the instruction Exec() is used and the following command - here in connection with the command to install the program 'bluefish' - is used:

hans@linux:~$ /tmp/gambas.1000/10857/xdg-utils/xdg-su -c "apt-get install bluefish"

For the virtual class _Desktop_ScreenSaver as well as for the class Desktop with its methods, descriptions and the presentation of relevant source code excerpts are given in separate chapters:

Virtual class _Desktop_ScreenSaver to manipulate the screen saver Desktop.

  • Desktop.Open (URL As String[, Wait AsBoolean])
  • Desktop.OpenTerminal ([ Dir As String])
  • Desktop.SendKeys (Keys As String)
  • Desktop.SendMail (To As String[][…, Subject As String, Body As String,…])




The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k15/k15.1/start.txt · Last modified: 28.09.2023 by emma

Page Tools