dynamic menus by akira on Wednesday 10 March 2004 after a little discussion in the #fluxbox-channel i decided to write a little article about dynamic menu-entries. other windowmanagers support perl or python in their menus, fluxbox doesnt. this article explains how to get things done even if no scripts can be executed from within the menu. there is no script-support in the menu (or at any other place) in fluxbox (yet). but what if one wants to have some dynamic content or whatever you like? well, thats easy. all we have to do is to get the content, save it into a file and do some magic in our fluxbox-menu. lets start from the end: the "magic" is called the [include] command. it works as the following: [submenu] (news) [include] (~/.fluxbox/news) [end] this will create a submenu in our menu called "news" and in that submenu there are, lets say, hourly updated headlines. at least thats the plan so, first we need is the included file. lets assume we have a nifty newsfetcher. i ll call it "rnews" since its .. uhm .. my newfetcher it grabs a rdf-page and puts the content to the stdout. rnews -m -n sl this will fetch the titles and the urls to the corresponding news and give some output as: 1.[SCO Consultant S2 Strategic Consulting In Depth] http://slashdot.org/article.pl?sid=04/03/10/001215 2.[Protecting Our Parents' PCs?] http://slashdot.org/article.pl?sid=04/03/09/2134215 3.[Microsoft Customers Get No Bang for Buck] http://slashdot.org/article.pl?sid=04/03/09/220233 4.[KDE 3.2.1 Released] http://slashdot.org/article.pl?sid=04/03/09/2134232 5.[US Government Upgrades RAM] well, thats a good start. this content we need to convert into the fluxbox-menu-syntax. first we need to bring titles and the urls on one line. rnews -m -n sl | sed '/^$/d' | awk '{ printf $0" " ; getline ; print }' so, now it looks like: » rnews -m -n sl | sed '/^$/d' | awk '{ printf $0" " ; getline ; print }' 1.[Kodak Sues Sony Over Digital Camera Patents] http://slashdot.org/article.pl?sid=04/03/10/0054250 2.[SCO Consultant S2 Strategic Consulting In Depth] http://slashdot.org/article.pl?sid=04/03/10/001215 3.[Protecting Our Parents' PCs?] http://slashdot.org/article.pl?sid=04/03/09/2134215 4.[Microsoft Customers Get No Bang for Buck] http://slashdot.org/article.pl?sid=04/03/09/220233 5.[KDE 3.2.1 Released] http://slashdot.org/article.pl?sid=04/03/09/2134232 beautiful, isnt it? next step is as easy as the rest before. we need each line been transformed as: [exec] ( title ) { command url } i decided to use konqueror as the browsercommand. the resulting commandline is: rnews -m -n sl | sed '/^$/d' | awk '{ printf $0" " ; getline ; print }' | sed -e 's/^\(.*\)[[:space:]]\+\(http.*\)/\[exec\](\1) {konqueror \2\}/g' this results in [exec](1.[Kodak Sues Sony Over Digital Camera Patents] ) {konqueror http://slashdot.org/article.pl?sid=04/03/10/0054250} [exec](2.[SCO Consultant S2 Strategic Consulting In Depth] ) {konqueror http://slashdot.org/article.pl?sid=04/03/10/001215} [exec](3.[Protecting Our Parents' PCs?] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/2134215} [exec](4.[Microsoft Customers Get No Bang for Buck] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/220233} [exec](5.[KDE 3.2.1 Released] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/2134232} [exec](6.[US Government Upgrades RAM] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/1952234} [exec](7.[Can Software Kill?] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/180258} [exec](8.[Implementing CIFS] ) {konqueror http://slashdot.org/article.pl?sid=04/03/05/2119222} [exec](9.[Peer to Peer and Spam in the Internet] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/1724211} [exec](10.[Pocket PCs Masquerade as iPods] ) {konqueror http://slashdot.org/article.pl?sid=04/03/09/1654250} ah, damn sexy. pipe this to ~/.fluxbox/news and .. well .. thats it. now you have a submenu called "news" in your fluxbox menu with all the headlines from slashdot. yeah, perfect. and even better, if you click on the titles in the submenu, a new konqueror will open up and surf to the related url well .. there is a last problem. the headline are getting really fast really old. there must be a way to update the nrews-file. *thik think* and a voice from nowhere whsipers the magic words: cron yeah, we put the complete commandline in a script and use the crondaemon of the system to run the script every x minutes, hours, whatever. or, use "batch" or "at" advantages of this method to create the menus: 1. it keeps fluxbox small and clean if no script-code is added, no further deps, less potential bugs. 2. the display of the menu will always be as fast as hell. if the script takes some time, who cares? its not the menurendering that is slowed down 3. you can use any input u want: scripts, mails, sms, whatever you want. the only thing thats necassary is the format for the file disadvantages: 1. its not obvious 2. the transform process requires some script-skills. but .. hey .. the topic was: how to get the output of a script into fluxbox-menus" so at least a tiny bit of script-skillz are necessary i hope you find this article usefull, regards, akira >> List articles in this category <