1

I'm trying to create a launcher for node.js scripts (so that I can run the scripts by clicking on their file icons instead of launching them from the terminal.) How is this usually done? I'd prefer if I could simply run a script in the terminal by clicking on its icon.

I tried writing a shell script to launch another script in the same folder, but it doesn't show the node.js script's command line output for some reason:

#!/bin/bash
echo -n "Enter a node.js script to run > "
read text
node "$text"
Anderson Green
  • 25,996
  • 59
  • 164
  • 297
  • Is this Windows or Gnome or KDE or ...??? – glenn jackman Sep 14 '12 at 22:37
  • It's actually (Ubuntu) Linux. – Anderson Green Sep 14 '12 at 23:05
  • This post looks relevant, but it doesn't appear to be an exact duplicate: http://stackoverflow.com/questions/4806571/is-it-possible-to-run-node-js-scripts-without-invoking-node – Anderson Green Sep 15 '12 at 01:28
  • I found something that appears to be a duplicate: http://stackoverflow.com/questions/4806571/is-it-possible-to-run-node-js-scripts-without-invoking-node – Anderson Green Sep 16 '12 at 20:32
  • The linked post describes making your JS files executable _themselves_, by adding a shebang line (`!#…`) and applying `chmod +x`. If your JS files are only invoked from a a shell, that's a convenient option. Opening such files in a GUI file manager, however, will typically not give you what you want; for instance, Finder in OS X runs the script in a visible Terminal window, but exits the shell too once the script has finished running, leaving the window read-only. On Ubuntu, opening an executable script from Files, the file manager, opens it for editing. – mklement0 Jul 27 '15 at 12:30

2 Answers2

4

I now know that you're looking for an Ubuntu solution, but in case someone is interested in an OS X solution, here goes:

  • Open Automator.
  • Create a new application.
  • Add an AppleScript action
  • Paste the following code:

on run {input, parameters}

    tell application "Terminal"
        repeat with f in input
            do script "node " & quoted form of (POSIX path of f)
        end repeat
        activate
    end tell

end run
  • Save the application.
  • In Finder, control-click any *.js file and select Open With > Other ..., pick the new application and check 'Always Open With.'

From then on, whenever you open a *.js file, it will open in a new Terminal window that will stay open after node finishes running; add ; exit to the command string above to close automatically (possibly adding read -sn 1 first to wait for a keystroke first.)

mklement0
  • 245,023
  • 45
  • 419
  • 492
  • Is it possible to do the equivalent using sh? I'm trying to find a solution that will work on most (or perhaps all) Unix-like operating systems. – Anderson Green Sep 15 '12 at 01:25
  • While I don't see any technical problem with the script in your question (it should output whatever `node` outputs), I don't see an advantage over simply calling `node` _directly_ with the *.js file as the argument. As to the larger question of changing what the various GUI file managers do when opening a *.js, I suspect there is no generic answer. There's a generic _Ubuntu_ answer [here](http://askubuntu.com/questions/4361/how-are-file-extensions-mime-types-icons-default-applications-asssociated?rq=1), but you'll have to figure out how to specify a shell command/script as an application. – mklement0 Sep 15 '12 at 02:27
1

i use this to start my node scripts on debian in the terminal

#!/usr/bin/env sh
dir=$(dirname $0)
script="$dir/path_to_your_server.js"
echo "node $script"
supernova
  • 3,606
  • 2
  • 19
  • 34
  • Where should the .sh script and the .js script be located, respectively? – Anderson Green Sep 16 '12 at 02:45
  • "path_to_your_server.js" would be the relative path to the file, where $dir is the directory where the .sh file is located for example my .sh script is located in /bin and my server.js is located in / of my project dir, so it would be script="$dir/../server.js" – supernova Sep 16 '12 at 05:24
  • 1
    Is it possible to change the script so that the terminal stays open even after the script is finished running? – Anderson Green Sep 17 '12 at 16:17