Using a .desktop file to launch a node.js app

In Ubuntu 16.04, I (eventually) want to drop a text file onto a desktop icon and get a Node.js app to run, treating the dropped file and outputting a new file.

I have discovered .desktop files, and am doing my best to understand how to get the process to work, using a barebones set-up. I currently have three files: two files in a directory at /home/blackslate/Utilities/ and one on my desktop.

Utilities files

A text file named convert.js:

const fs = require('fs');
const crypto = require('crypto');
const random = crypto.randomBytes(4).toString('hex');
const filename = random + '.txt'
fs.appendFile(filename, 'Hello ' + random + '!', function (err) { if (err) throw err; console.log(filename + ' saved');
});

An executable file named convert.sh:

#!/bin/sh
node ./convert.js
gnome-terminal -e "bash -c 'echo hello world; sleep 3'"

Desktop file

An executable file named convert.desktop:

[Desktop Entry]
Exec=gnome-terminal -e "/home/blackslate/Utilities/convert.sh \"%u\""
Icon=utilities-terminal
Type=Application
Name=Convert

Here's what happens:

  1. If I call node ./convert.js from a Terminal window, I see that:

    • A new file is created and its name is shown in the Terminal window
  2. If I call ./convert.sh from a Terminal window, I see that:

    • A new file is created and its name is shown in the same Terminal window
    • A second Terminal window is opened and says hello world before closing 3 seconds later
  3. If I click on the Convert desktop file, I see that:

    • One Terminal window opens, showing a bunch of text, and then it closes immediately
    • A second Terminal window is opened and says hello world before closing 3 seconds later
    • No new text file is created

It is obvious that the second command in the convert.sh file is being executed, but it appears that something is going wrong with the first one. What steps can I take to debug and fix this?

1 Answer

A .desktop can easily run a bash script

You should change things in your convert.sh :

#!/bin/bash
# set prompt to the working dir with cd
cd /home/blackslate/Utilities/
node ./convert.js
# You want some text notification? Use notify-send
notify-send -t 3000 'echo hello world'

Note that you can redirect the output of node ./convert.js to a variable and use it as notification text...
MYVAR=$(node ./convert.js) then notify-send -t 3000 "Convert" "$MYVAR"

And you may make some changes in the .desktop file
find icons there /usr/share/icons

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec='/home/blackslate/Utilities/convert.sh'
Name=Convert
Icon=utilities-terminal
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like