Create a launcher for a node.js script - bash

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"

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.)

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"

Related

Run Bash script on selected files in Finder

I have a tiny Bash script that executes ffmpeg and a touch command on an input file. I use this to recompress video files from my camera. I would like to be able to right-click files in Finder and run the script on the select file(s), preferably showing the terminal window while executing and closing when done.
How to do this on macOS?
I think this is what you want. I started Automator by pressing ⌘space and starting to type "Automator", hitting ↩ as soon as it guessed correctly. I then created a "Quick Action" that contains this code:
on run {input, parameters}
repeat with theItem in input
set f to POSIX path of theItem
tell application "Terminal"
activate
tell window 1
do script "echo " & f
end tell
end tell
end repeat
end run
and looks like this:
It basically just echos the filename, but you can put ffmpeg commands in there instead.
Why using finder? Or automator? Or going though loops and hoops just to use the GUI?
You have fully-functional bash shell in MacOS, so save time and hassle with the below one-liner.
Assuming you need to run your script for all *.mpeg files in the folder.
Try this:
ls *mpeg | xargs <your_script_name>
You will see the execution output in the same terminal window.

Opening a file with an app using a command line in apple script or an automator app

I want to be able to double click to open a files type (HTML) in a particular app (SeaMonkey composer).
Double clicking the file opens SeaMonkey Browser, but I want it to open in Seamonkey Composer. The only way to do it is with the following command line
seamonkey -editor "filename.html"
So, how can I use apple script or automator to open my html files in composer ?
Save the following script as Application in Script Editor:
on run filesList
repeat with fileRef in filesList
do shell script "seamonkey -editor " & quoted form of POSIX path of fileRef
end repeat
end run
Select View > Show Bundle Contents and give it a custom bundle ID. You can then change the file association as above.
The above assumes the seamonkey command is itself just a launcher; if it's actually the full application (which may be the case as it's obviously not a native Mac app), the middle line'll need tweaked a bit:
do shell script "nohup seamonkey -editor " & quoted form of POSIX path of fileRef & " >/dev/null 2>&1"
That should allow the shell script to exit as soon as the seamonkey process is launched, leaving Seamonkey running until you quit it from its GUI.
If you want to use AppleScript to do this, this simple script should do the job:
set filePath to ((path to documents folder) as text) & "filename.html"
tell application "Seamonkey Composer" to open filePath
I don't have the Seamonkey Composer app to test, but it works with BBEdit.
Note that the open command must have a full path to the file.

Script that launches commands on two Terminals

I need to do a .command file script/batch. Launching it (double-click) it has to to those things:
Open a terminal window (A)
Launching a command that open the folder where the file is (maybe this cd "dirname "$0"")
Launch a command
Open a terminal window (B)
Launching same command at point 2
Launch a command
Given that you explicitly want to create terminal windows, consider creating an application using AppleScript:
Open Script Editor (up to 10.9, AppleScript Editor)
Paste the code below.
Save as an application (via the pop-up list in the Save As dialog) to the desired folder.
# Determine the folder in which this app is located.
set thisFolder to do shell script "dirname " & quoted form of POSIX path of (path to me)
# Sample commands to execute in the new windows.
set cmds to {"date", "echo $$"}
tell application "Terminal"
# Create 2 new windows, change to the
# this app's folder, and execute the respective command.
repeat with i from 1 to 2
do script "cd " & quoted form of thisFolder & "; " & item i of cmds
end repeat
# Activate Terminal.app
activate
end tell
The reason that I recommend using an application over a *.command file is that the latter would itself open in a Terminal window first, before creating the desired windows, which is visually disruptive (and, depending on your Terminal.app preferences, may leave the extra window open).
Alternatively, you could turn that into a virtue and use the *.command file's own window as your 1st terminal window, and only create one additional one.

Shortcut to running Mac Terminal commands

Ok... I've been searching for an answer to this for a while and can't seem to find any good examples, so I thought I'd break down and ask.
How can I create a shell file (.command) in OSX that I can just double-click on which:
Opens a new Terminal window
Runs a few commands
... and stays active so I can continue to run other things!
My goal is to setup various environments using individual .command files, which will each set variables and run certain command line tools, and then remain open to manually run other commands. I currently have one like this:
#!/bin/sh
export MY_VAR_A="blah A"
export MY_VAR_B="blah B"
cd /Users/
... and this doesn't work. It just opens a Terminal window with this output:
Last login: Sat Aug 17 12:52:15 on ttys000
unknown60c5470527e4:~ me$ /Users/me/Documents/test.command ; exit;
logout
[Process completed]
Is there a better (or just different) way of accomplishing what I want? Or do I just need to adjust something simple in my current .command file?
Use applescript
tell application "Terminal" to activate
tell application "Terminal"
do script ("ls -l") in window 1
do script ("cd /Users/test/Music/iTunes/") in window 1
do script ("ls -l") in window 1
end tell
Save apple script as application bundle.

Close Terminal window from within shell script (Unix)?

Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.
Using exit 0 will cleanly terminate the script.
Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.
Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:
echo -n -e "\033]0;My Window Name\007"
Then at the end of the script, use:
osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &
You can use apple script to quit the terminal app. Add the following to your script -
osascript -e 'tell application "Terminal" to quit'
This will give you a popup confirming to close the app. You can disable this in Terminal preferences.
Alternatively, you can also use killall command to quit the app. The following would work just as well.
killall Terminal
Note:
Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. First being you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohup so that any commands that are currently running won't quit due to quitting of the Terminal app.
This works for me:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.
closeWindow() {
/usr/bin/osascript << _OSACLOSE_
tell application "Terminal"
close (every window whose name contains "YourScriptName")
end tell
delay 0.3
tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}
This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.
I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.
Here is how you do it:
Run Automator (in Applications).
Choose "New Document" and when it
asks "Choose a type for your document" choose "Application"
In the
left panel, select "Utilities" then "Run Shell Script".
Type in your
script commands in the workflow item in the right panel. You can either call another
shell script, or just put your commands in their directly.
Save the
Application, which will be a full-fledged Mac App. You can even
cut-and-paste icons from other apps to give your script some
personality.
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
you can also create a shortcut for your script:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
then type
yourshortcutnamewhateveryouwant
will run whatever is writen into script at any directory.

Resources