Tutorial – Desktop News Feeds

.desktop Files

Many years ago – more or less when Gnome and KDE showed up – many free desktop developers realized they needed common standards and joined forces at freedesktop.org. The results of that effort that I use in this tutorial are the so-called Desktop Entry files and related menu specifications.

The purpose of these files with the .desktop suffix is to specify for each application how it launches, the icon(s) it uses, and where and how they are displayed in system menus. Everything you want to know about .desktop files and menus is simpler than you might fear [2]-[6].

Here, however, it is enough to have a quick look at the partial .desktop example file taken straight from the official specifications (Listing 5).

Listing 5

Partial .desktop file

 

A Desktop Entry can be of Type Application, Link, or Directory. Linux shows the Name value under the icon specified in the Icon field or in the menu entry for that application. The Exec field describes how to launch the application and takes arguments like %U (multiple URLs), %d (a single directory), or %v (a device name). Applications that only run inside a terminal will have the corresponding field set to True. Finally, the Categories field lists all the menu categories in which the current application should appear.

Gnome, KDE, Unity, and freedesktop.org-compliant window managers all look at .desktop (and other) files to know how to place icons or build system menus for each user.

The system-wide location for .desktop files is typically /usr/share/applications, and icons are normally stored in /usr/share/pixmaps. User-specific .desktop files, on the other hand, are placed in the $HOME/.local/share/applications/ folder of that user.

Applications like MenuLibre [7], which is available in the standard repositories of the main distributions, facilitate the customization of system menus and let users modify certain settings in .desktop files. Here, however, I look at how to create and reload .desktop files dynamically that, when selected, open a given web page in a browser.

The Icons Method

Figures 1 and 2 show that it is possible to make one clickable icon per headline appear right in the root window of the Linux GUIs that support the Desktop Entry standards.

Figure 1: The easiest and most visible, but probably least convenient, way to embed clickable links to the latest news in your desktop is to save them in the correct format in your Desktop folder.
Figure 2: Even the Unity dash can find your links to the latest news.

Personally, I do not like nor recommend this solution. If your root window is, like mine, almost completely covered by applications, icons are useless. I also found that getting the font and colors of icons to look good with every window manager and background requires more effort than I was willing to take; however, you might have different preferences, so if you want icons as shown in those figures, just modify the script that follows so it does not run the xdg-desktop-menu command and save the .desktop files in your $HOME/Desktop/ directory.

Automatically Generate and Reload Desktop Files

Besides the .desktop files, two other files are needed to create custom submenus and their categories. One file has the .directory extension, specifies name and purpose of the new category, and must be placed in the /usr/share/desktop-directories folder. Listing 6 shows the one I created for my custom MyNews category.

Listing 6

MyNews.desktop

 

The other file that defines a working submenu has the .menu extension and is normally placed in the /etc/xdg/menus/applications-merged folder. Unlike the first example, however, this MyNews.menu file must be recreated (but not manually) every time the content of that menu changes. The menugenerator script in Listing 7 creates the .menu file after generating a .desktop file for each headline.

Listing 7

menugenerator

 

The loop in lines 6 to 23 reads the rssmix file generated in Listing 3, one line at a time. Every time, title, and URL of the current headline are stored in POST_TITLE and POST_URL, and a corresponding unique .desktop file is generated (line 11) using the Bash trick known as the "here document" [8], which does two distinct things:

1. All the lines that fall between the EOD delimiters (lines 12-20) are dumped into myrss$CNT.desktop.

2. Before dumping, every occurrence of a shell variable (i.e., $CNT, $POST_TITLE, and $POST_URL) is replaced with its current value.

Line 22 increments the counter, and another headline is loaded. Please note how line 14 prepends a unique numeric string from the current count to the headline. I will explain why in a moment.

Line 25 removes all the files placed in the system folders by previous runs of the script, so you have fresh menus every time. To do this, the script must be launched by an account with write access to those folders.

Line 27 finally recreates both the MyNews.menu file and the .desktop files inside /usr/share/applications/ in the only way this is supposed to happen: by telling the xdg-desktop-menu command to do so. If each user of a computer wants their own private MyNews menu, the script above should be modified according to the instructions in the xdg-desktop-menu man page.

The cool results of the menugenerator script is shown in Figures 3 and 4. Every time rssmixer and menugenerator run in the background as cron jobs, the MyNews section of your system menu is rebuilt automatically with entries that open the corresponding web page in a new Firefox tab when selected. As promised, you have no distractions and no wasted screen space. News headlines only appear in a very compact format when you summon them.

Figure 3: A dynamic section with direct links to news from several sources in the root menu of the Enlightenment window manager.
Figure 4: Every time the scripts are run, the menus are rebuilt in a standard format reusable by any modern Linux window manager or desktop environment.

Figures 3 and 4 also show why I prepend a numeric value to each headline: It is the simplest way I have found to have headlines sorted, not alphabetically but in the same order in which they appear in the rssmix file. If that is not an issue for you, just remove the 0$CNT: substring from line 14.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Collecting News Headlines

    Instead of visiting news sites periodically to pick up the latest reports, most people prefer to let a news aggregator do the job.The aggregator automatically draws your attention to incoming news. If a website does not have an RSS feed,a new Perl module simplifies the task of programming an RSS feed for private use.

  • Workspace: RSS Aggregators

    Read and manage your favorite feeds with open source RSS aggregators.

  • Tutorial – Shell Scripting

    You do not need to learn low-level programming languages to become a real Linux power user. Shell scripting is all you need.

  • Makagiga

    Makagiga includes a bulletin board, an RSS reader, a notebook, a calendar, and a task scheduler, with all the components intelligently linked.

  • deskTOPia

    Even the smallest desktop has room

    for a panel. If your choice of a window

    manager does not support a suitable

    panel alternative,you can always add

    a panel like SuxPanel. Suxpanel provides

    many useful features for the

    desktop,and it even it even supports

    plug-ins.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News