Episode: 2877 Title: HPR2877: Using Zenity with Pdmenu Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2877/hpr2877.mp3 Transcribed: 2025-10-24 12:38:30 --- This is HPR Episode 2877 entitled, using Menetit with BD menu, and in part of the series, ash-crypting, it is hosted by Dave Morris, and in about 23 minutes long, and carry an explicit flag. The summary is, Menetit in a rather cool program that will display GDK Plus dialogues from a crypt. This episode of HPR is brought to you by archive.org. Support universal access to all knowledge, by heading over to archive.org, forward slash donate. Hello, everybody. Welcome to Hacker Public Radio. This is Dave Morris. Now, today I'm going to talk about a tool called Zenity, in particular the way I use it with PD menu, but I'm hoping that you'll be able to extrapolate from that and use it in other contexts. So, I use PD menu quite a lot to do things on my main desktop PC. I did an HPR show on PD menu on the 13th of December 2017, and was amazed to hear from Joey Hess, the author of this tool, who was the author of this tool, who was the author of this tool, who did a response show in number 2459, soon afterwards. In the intervening time, since then, I've still used PD menu a lot, but I've also integrated Zenity into the various menus that I eat. Now, this is a GUI tool, which will generate a number of different pop-up windows, standalone windows, if you like, known as dialogues, and these can display information, or you can type stuff into them, click buttons, or whatever. The capabilities provided by PD menu to do this sort of stuff, displays fine, no problem, but inputting stuff is a little bit basic for my needs. So, I found that Zenity was a way to make good these shortcomings. So, I thought it might be of interest to show some examples of what I do with Zenity and give you a quick overview of the tool itself. So, if you look at the Zenity manual, which is linked from the notes, it's a rewrite of G dialogue, which is the GNOME port of dialogue, which allows you to display dialogue boxes from the command. But I think the dialogue boxes are not GUI things. They're just curses or encurses type boxes that are displayed on your terminal. But the point of it is that you can make menus and display them from the command line and from shell scripts, which is a great convenience. The Zenity runs on Linux, BSD, Windows, and there's a version for Mac OS 10 as well. And it's invoked from the command line or a script simply as the command Zenity all in lowercase, that's ZEN ITY. We're the bunch of options. So, the most critical option, I guess, and the one that you definitely need to provide anyway, is one of a list that I've put into the notes, which I won't go through in massive detail, just maybe pick out a few. But the options select what type of dialogue box is required. So, for example, you might have hyphen, hyphen, calendar, which pops up a display, a calendar dialogue. You might have hyphen, hyphen, question, which shows a question dialogue. I think you can put yes-nose in that type of thing. You can have a password dialogue, which is just a box you can type into, which doesn't echo what you type. And so on and so forth. Oh, I guess I could do a whole series of shows on this, but I don't think it would be that interested to hear it, especially since the manual page, just if you type man's entity, you get a lot of quite useful stuff. Could be formatted a little nicer, but you can also go on the web and see the manual there. Manual actually on the web misses out one or two things, hasn't been updated as much as it should perhaps. I think I'm looking at the latest one. But anyway, the information's there if you look around a bit. So, once you've decided which of these dialogues you want, then there are a lot of other options, which will modify what you do. So, for example, there's a set of general options, and they all begin with double hyphen. So, hyphen hyphen title sets the window title for the dialogue box. The thing you see on the top bar, depending on which window man you enjoy. There might be an icon involved with it, which you can set with hyphen hyphen, window hyphen icon equals, and then the name. I've got the name of an icon, or a pointer to file containing the icon, I think. I won't read these out step by step, but you can set the width and height of the window. Otherwise, it will default to what's appropriate for your usage. You can also set a timeout, which is interesting. So, you could be asking a question with a timeout, which will eventually trigger if you don't answer, and then your script can take action to say, what do you want to answer? Where have you gone? I'm not going to do this at all, close down. Those types of things. So, I wanted to look at a couple of examples of dialogues that I use. So, my first one is the calendar dialogue, which is actually quite complex, but I thought you might find that more interesting. It takes options, hyphen hyphen text, and that's a string followed by a string, with which you set text in the dialogue box. Then you can set the date on which the calendar is to be set. There's a display with that date highlighted. You can do that with hyphen hyphen day, hyphen hyphen month, hyphen hyphen year. Each of those followed by an integer to set these values. But if you don't include one of those three, then you will default to the current date. The one that I find to be the most useful is hyphen hyphen date hyphen format equals pattern. And what that does is to set the format for the date returned by the calendar dialogue. And you can use in there the codes that you'll find in the man page under STRF time, like percent capital Y for the four digit year, for example. So I've got an example here, which is on the command line, typing Xenities space, hyphen hyphen calendar, hyphen hyphen title equals test, hyphen hyphen date four and date hyphen format equals. Then in quotes, percent capital Y hyphen percent, the lower case M hyphen percent D, close quote. So what that does, and I've put up screen, shotted an example. This was run on the 25th of July. It's showing a calendar thing. We did, I guess, a dialogue showing the entirety of July with the 25th being highlighted. If you then clicked OK on that, you would get back the string 201 hyphen zero seven hyphen two five. So that's all good and fine. And you could, you can hopefully see how you would use that in scripts and so forth. So I use this in my PD menu stuff. So there is a file called dot PD menu RC, which contains all of all of these sorts of things. And in particular, I use it to keep, keep notes about HBR shows. I've got an SQL, do you say this? It's SQLite database, which in which I keep a list of HBR shows and I store notes about them so that when I do the community news recording, I don't have to rely on my memory to remember what I thought about a particular show. And I'm commenting about it. This is useful. I've got a script which will take this database. So I actually put the database info into a media wiki wiki, which runs on one of my Raspberry Pi's. And the reason I've got a database and a wiki is because I started with the wiki and we chose to hand edit. And then I thought, oh, I've put it in a database and then I automate the editing. It's easier to type into a database I thought using an editor. So that's what I do. I'm sure there's easy ways to have done that. I've got a wiki page per month that HBR shows. And so I just put that up on my screen during the community news show and scroll through it. So my menu in PD Menu has got this particular one. Says update the wiki for this month. Is one of the options or update the wiki for any month. So I might have added shows into the wiki into the database ready to go on the wiki for the coming months, for example, because these shows have just been added to the HBR database. And I've updated them, I've added them to my data. So the two menu entries you see shown in the in the PD Menu sub menu in the notes are implemented through two exec lines in menu. And the first one simply calls a script called update underscore wiki where it defaults to the current month and everything's fine. But if I want to do update for a previous month or a future month, then I use Zenity to pop up a calendar dialogue to ask what month I want to do stuff for. And if you look at the example you'll see, I've got a long line which has been split with backslashes at the ends of lines. But the piece of this line is the bit where you put the command that you want to execute when that particular menu entry is clicked on. Is rep equals. So I've got a variable called rep. And this is a piece of shell script. And then dollar. And then in parentheses, Zenity, space, hyphen, hyphen calendar, space, hyphen, hyphen title, equals, quote, update space, wiki, close quotes, space, hyphen, hyphen, text equals select a month, space, and then hyphen, hyphen, date format equals. And in this case, I want a date to come back in the format. The first, I just force it to be the first of the month. So it's 01, hyphen, percent, lowercase B. And that returns a month name in an abbreviated form, hyphen, percent, capital y, close quote. So that's the entire command that's being run in a command substitution. And the answer from that gets put into a variable called rep. So if that succeeds, that is I don't click on the cancel button when the window is being displayed. We'll do the thing after the double ampersand. So this is using standard shell type. This is using standard shell type choices, where you have a thing that returns a true false and a double ampersand. And then in curly braces, you have a bunch of commands that you want to execute. So I've got in curly braces slightly unreadably because it's all being split up with backslashes. I've got echo space then in double quotes dollar rep. That's just me debugging what I'm doing. It's not really necessary. Then I call this update wiki script with in double quotes dollar rep semi-colon. So what that actually does is to invoke the script that I mentioned earlier. But at this time, it's getting a date. Why the date, why is the date in this format? Well, I'm using a, it's a pulse script. I'm using a library which quite likes that format by default. It's a date parsing library which seems to handle that particular format most easily. So I just, that's that format of date. Even though it's not good 8601 format stuff that Ken would prefer. So there we go. So I choose the month I want to update and if it goes and updates it. Now what will happen is the, the Zenity window will be shown and it will default to the current date. So if I want, it's now August. If I wanted to update something for July, then I simply had the month of August displayed in the window. And in the top of the window, either side of the month name, there is less than and a greater than side. So I would just click the less that sign which moves me backwards one month and then click OK. It will then return the month. Doesn't the actual day in 12 doesn't matter because it's ignored and replaced by one. So that's, that's what I do. That's a lot, lot, lot easier than typing a date into the PD menu input thingy. So the other example I was going to talk about is using the Forms dialog. The Forms dialog is a very strange beast. It effectively lets you assemble a number of other dialogues together into one structure, one form. And it takes the following options which I've listed in the notes. It takes a text string with a hyphen hyphen text and it then can take one of the following which do the adding of different types of dialogues into the form. So hyphen hyphen add hyphen entry will create a little text entry due to add which I'm tempted to say widget. I'm not sure if it's widgets or I would but anyway, you can see an example here further down the page. You can add a password entry into the form. You can add a calendar that those seem to be the only ones you can add. You can't add some of the other more esoteric dialogue types that Xenity offers, which is a bit of shame but there you go. This is not meant to be an all-singing or dancing tool. It caters for a lot of capabilities though. I think in the manual they do point out that if you wanted to be more fancy than they have provided, do more fancy things than the things they're catered for, then you'd be better to learn to program dialogue boxes yourself and write your own. Which is my supposed fair enough. Then hyphen hyphen separator is a way in which you can define because you're going to be getting back multiple values when you've run this. And you can specify what the separator between the values should be. Should it be commas or vertical bars or whatever. And if you have something which returns a date, i.e. a calendar, then you can specify the date. So I've got an example here using a form. It's got an entry called first name, and an entry called last name, and a calendar called D.O.B. And a format is the year month day format. So if you were to type Joe in the first name box, blogs in the second, in the last name box, and click on the first of April 2000 for your date of birth, and then click OK, you would get back as shown in the example. Joe vertical bar blogs vertical bar 2000 hyphen 04 hyphen 01. The default separator is the vertical bar I should have said. So that's pretty useful. I find it useful anyway. So I have an example, another piece of PD menu stuff, where I, in this case, I am collecting metadata from the internet archive. And the reason I'm doing this is because I'm keeping an inventory of all of the HBR shows that have been updated and added. They've been uploaded, I should say, to the internet archive archive.org. And I'm also keeping a record of all of the files associated files with it. I do intend to incorporate this into the new HBR database, but for the moment I'm just doing a sort of local version. So there's another SQLite database with this in. And I've got scripts which will do the data collection from archive.org, and they return JSON format data, save it in a file. So the particular menu entry I have is the one, just to talk to you about, is the one that collects the JSON from the internet archive. And in this particular case, the shell script bit that I'm executing is rep equals dollar open parenthesis. Zenity, space, hyphen hyphen forms, space, hyphen hyphen text equals intershow numbers in single quotes, space, hyphen hyphen ad, hyphen entry equals start, space, hyphen hyphen ad, hyphen entry equals end, close parenthesis, double end, then if that Zenity call returns true, then I will execute a script called collect show data. And I will be giving it two arguments. It needs the first show of a series alone and a second show number. So my example shows 2866 and 287O being provided to the particular call. And that's just to grab five HBR shows metadata for them, having previously uploaded them to the internet archive. And you see the sort of fun I have with this. So this just does the thing, the bit after the amp double ampersand uses these two numbers and feeds them into this script. I guess one of the important aspects of this is you got this single string with a vertical bar in it and you've got bits either side of it. There are just two pieces to this. So the expression in double quotes, dollar, open curly bracket, rep, that was the variable that's got the value. Percent vertical bar, asterisk, close curly brackets, close double quote. What that will do is it will remove from that string everything from the vertical bar to the end of the string. So in other words, it returns the first element of the string. Then the second one is again in double quote, dollar, then in curly brackets, rep, rep, hash, mark, asterisk, vertical bar. So what that's doing is removing everything from the start of the string to the vertical bar, inclusive of course. So that returns the second piece of the string. So that's how the two values are fed in. There's no error checking really here. Though I think if you click OK before both fields are filled in, it's entity might fail. I'm not sure, I've never tried that. I don't do a huge lot of error checking and things are right for myself, how to say. So that's what it does and that seems to be that works fine for me and I think it's a very cool way of approaching things. I've certainly come across other people who use this type of approach to running scripts and so forth. And so I don't think I'm on my own in doing things in this way. So in summary, I said, I need menus. Since I can never remember what I'm supposed to do to carry out workflows and other processes. I'm always, the times I used to do this type of thing. Where I would think right now, just go and do that, whatever is necessary. And then I have to do step one, step two, step three, step four. I do step one and then go straight to three. Why is that? And then I'll know I forgot to do step two. So putting it in some sort of script form just stops me being an idiot. It stops me being an idiot, but it stops my idiocy from coming out into the world. So as I say here, my motto is, if in doubt write a script, that's what I do. And I said again that the facilities in PD Mini for prompting and gathering information are a bit too basic for my needs. But Zenity does a pretty good job. So it's not, I'm not doing good old command line and curses type things. I've actually broken out into the world of GUI with this stuff. But I think I think that's fine. It seems like I could come from finally. Just one slight grumble about PD Menu. The shell it uses to run is run these commands. SH and not bash. So some of the bash's, as I like to use, aren't available to me. Which I fell over when I was developing some of these things. Not hampering me too much, I can work around it. And the thing that implements SH on my machine is dash, which is reasonably sophisticated. But not quite as sophisticated as bash. And if it becomes a problem, maybe look at changing the source of PD Menu to do what I want. It would be quite nice if you could tell PD Menu what shell to use underneath. I'm not sure how practical that is. Whether you can specify that to the sort of calls that do these run these things in a shell. But that would be something I would look at if it became a problem. But hopefully you find it useful. If you look into it in a bit more detail. If you feel that you want me to talk about how you would write more sophisticated bash tricks rather than specific PD Menu things, then tell me by all means I'll be happy to do more. But I assume that I would have done enough. Alright, well that's it then. And thanks to Joey Hess for PD Menu. Okay, bye. You've been listening to Hecopublic Radio at HecopublicRadio.org. We are a community podcast network that releases shows every weekday Monday through Friday. Today's show, like all our shows, was contributed by an HPR listener like yourself. If you ever thought of recording a podcast and click on our contributing to find out how easy it really is. Hecopublic Radio was founded by the Digital Dove Pound and the Infonomicon Computer Club. And it's part of the binary revolution at binrev.com. If you have comments on today's show, please email the host directly, leave a comment on the website or record a follow-up episode yourself. Unless otherwise status, today's show is released under Creative Commons, Attribution, ShareLite, 3.0 license.