11

Lubuntu 16.04 - Openbox session

I can use the terminal to open a time-stamped file with gedit:

gedit ~/$(date +%Y%m%d%H%M%S).txt

The command can also be used via a keyboard shortcut in Openbox.

<keybind key="W-4">        # gedit time-stamped file
  <action name="Execute"><command>sh -c 'gedit ~/$(date +%Y%m%d%H%M%S).txt'</command></action>
</keybind>

But I can't do the same via the Exec= line of a .desktop file.

Exec=sh -c 'gedit ~/$(date +%Y%m%d%H%M%S).txt'

just creates a file called ~/.txt

So how do I get a .desktop file to do what I want?

  • 1
    I don't think the .desktop file can call sub-shell $() commands like that. I think you need it to call another script that contains your sub-shell command for opening your file. – Terrance May 29 '16 at 3:37
  • Sure I will do that. =) – Terrance May 29 '16 at 3:43
  • 1
    HI @DKBose .desktop files can run sub shells, since Exec=/bin/bash -c "echo $(date) > ~/out.txt" runs fine. the +%Y%m%d%H%M%S seems to be the issue. still chewing on it. – Jacob Vlijm May 29 '16 at 8:12
  • I assume the first thing doesn't work because % is already used as command line arguments for exec in .desktop files It might be possible to escape them somehow, but the other solution seems alright. I would have added this as a comment, but my account here is new. – Mikael Kjær May 29 '16 at 8:48
  • 1
    Hi @DKBose see my answer :) – Jacob Vlijm May 29 '16 at 14:19
10

Unfortunately, .desktop files don't always call sub-shell $() commands the way we would like them to. One way to do this that I have found would be to create another script that contains the sub-shell command to open the file like that.

The Exec line would look like:

Exec=/path/to/script

then your script file would contain the command to open your new file:

#!/bin/sh

gedit ~/$(date +%Y%m%d%H%M%S).txt

the script would also have to be executable:

chmod +x /path/to/script

Hope this helps!

  • @JacobVlijm Is the issue similar to what one sees in cron where certain characters (%?) need to be esacped? – DK Bose May 29 '16 at 10:22
  • @DKBose Might very well be, I am still fiddling around with it, and I think it should work. Subshells alone are no issue. – Jacob Vlijm May 29 '16 at 10:29
  • @JacobVlijm You are probably correct. I have found that I like to avoid the sub-shell commands within files like these, much like in the sources.list file of apt you cannot replace the release name with $(lsb_release -sc) as that doesn't work either. If you do figure it out, I would love to see how you did it. :) – Terrance May 29 '16 at 13:12
  • Hi Terrance, it works! See my answer... – Jacob Vlijm May 29 '16 at 14:22
  • @JacobVlijm try using +\%Y\%m\%d\%H\%M\%S then it should work. – Videonauth May 29 '16 at 14:27
8

The issue

A script is not necessary.

The issue is not that a .desktop file cannot run commands with subshells, since

Exec=/bin/bash -c "echo $(date) > ~/out.txt"

works perfectly fine in a .desktop file.

Fiddling around with the command, I came to the conclusion that the % -character is causing the issue. I have no explanation for it though, and so far I could not find information on how to solve or escape that.

I therefore went looking for a command to get the time without using the suspected character.

Alternatively use Ruby for the date & time

This lead me to Ruby, which produces the current date & time from the command:

$ ruby -e 'puts Time.now.inspect'
2016-05-29 16:12:36 +0200

When we edit the output a bit with awk, removing spaces and delimiters, we have just what we want, and a working command in the .desktop file, since we don't use %:

Exec=/bin/bash -c  "gedit ~/$(ruby -e 'puts Time.now.inspect' | awk -F'[: -]' '{print $1$2$3$4$5$6}')"


enter image description here

Note!

No doubt, the command can be "charmed up" a bit, I will probably edit the answer a bit today or tomorrow.

  • Nice! This is definitely another way to do it. I didn't have Ruby installed but once it is installed, this definitely works. I agree that fiddling with this you could get it to look the way you want it to be. :) – Terrance May 29 '16 at 14:33
  • @Terrance it turns out to be an interesting question. Never ran into an issue in .desktop files like this before... – Jacob Vlijm May 29 '16 at 14:35
  • Me neither. It is good to know, and I have found that answering questions like these a lot of times we learn a lot ourselves as well. :) – Terrance May 29 '16 at 14:39
  • 1
    @Terrance 99% of what I know I learned on answering questions :) – Jacob Vlijm May 29 '16 at 14:40
  • 1
    You inspired me to change my wording in my answer as well. I changed mine to be one way instead of the best way. And other such changes so people don't think it is the only way. Thank you! :) – Terrance May 29 '16 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.