5

So I have this program I use to watch movies on my Apple TV that runs from my Mac in Terminal and goes to the TV. Well whenever you wanna start the server up you have to run a set of commands in Terminal.

$ cd Desktop
$ cd PopcornTV
$ sudo npm install
$ sudo node atv.js

These commands start the server which my Apple TV than connects too. Is there anyway I can make a Automator Application or anything of that sort that will run these commands for me when I click a button.

  • 4
    Surely npm install does not need to be run more than once – mmmmmm Sep 6 '15 at 18:16
4

Running it in an apple script is not that bad. you just have to run the entire script as root because sudo requires user interaction. if you do an apple script like this one:

do shell script "cd ~/Desktop/PopcornTV && /path/to/npm install && /path/to/node atv.js" with administrator privileges 

replace then/path/to/npm and /path/to/node with the output of which npm and which node

it will open up a dialogue and ask you for your password.

| improve this answer | |
  • error "/bin/sh: npm: command not found" number 127 i.imgur.com/YqDlueN.png – Ilya Vorobyev Sep 6 '15 at 22:30
  • then you should call npm with the full path like /path/to/npm (same in node) – Schwertspize Sep 7 '15 at 5:08
  • How do I call a path, like where do I add the path? I'm sorry if I'm asking stupid questions, I'm sort of new to this. Here is the path to it /usr/local/lib/node_modules i.imgur.com/RIFH1bF.png – Ilya Vorobyev Sep 7 '15 at 8:02
  • if you call which npm the output will be something like /usr/local/bin/npm (not true as far as I know, but as example). if you call npm this command gets executed, but in the script you have to call this command with /usr/local/bin/npm instead of just npm (these paths were just example, I can't try out just now) – Schwertspize Sep 7 '15 at 8:07
2

A solution without using Automator:

1. Gathering some details:

  • What is the path to npm? Find out using which npm in Terminal.app
  • What is the path to node? Find out using which node in Terminal.app
  • What is your username? Find using whoami in Terminal.app

All of this is important, make a note of the outputs.

2. Configuring sudo

Let’s now configure sudo to not require you to use a password for those two commands.

In Terminal.app, enter the following:

sudo visudo

You will need to enter your password, though you won’t see any typing as you enter it. Press enter when you’ve completed entering your password.

This will open the /etc/sudoers file for you (probably in vi/vim) press shift+g to go to the bottom of the file, then press o to insert a newline and start inserting text, enter the following:

# Allow me to use npm and node without password
username ALL = NOPASSWD: /path/to/npm /path/to/node

You will need to replace “username”, “/path/to/npm”, and “/path/to/node” with the details you gathered in section 1.

When you have finished entering these details, save and quit by pressing esc then entering :x then pressing enter.

3. Wrapping it up in a file

Let’s create a file to put those commands in. In Terminal.app enter the following:

echo '#!/bin/bash
cd /Users/username/Desktop/PopcornTV
sudo /path/to/npm install
sudo /path/to/node atv.js' > ~/Desktop/startPopcornTV.command

chmod 740 ~/Desktop/startPopcornTV.command

Again, you’ll need to replace “username”, “/path/to/npm”, and “/path/to/node” with the details you gathered in section 1.

4. Running

On your Desktop you will now have a file called startPopcornTV.command (depending on your setting it might just be called startPopcornTV).

All you need to do now is double click that file and it will start PopcornTV for you.

| improve this answer | |
1

I think this should work :

echo "#\n cd Desktop \n cd PopcornTV \n sudo npm install \n sudo node atv.js" >> ~/Desktop/startMyServer.command

then make the command file executable

chmod +x ~/Desktop/startMyServer.command

you will then find the file startMyServer.command on your desktop

| improve this answer | |
1

Open up Applescript-Editor

add this:

do shell script "cd ~/Desktop/PopcornTV && /usr/local/bin/npm install && /usr/local/bin/node atv.js" with administrator privileges

Save as an application. Just open it when you want to run.

| improve this answer | |
  • Sorry it didn't work. i.imgur.com/WcfvwVp.png – Ilya Vorobyev Sep 6 '15 at 18:03
  • You'll need to add "with administrator privileges" at the end of the AppleScript line. – At0mic Sep 6 '15 at 20:43
  • error "/bin/sh: npm: command not found" number 127 screenshot – Ilya Vorobyev Sep 6 '15 at 22:31
  • @IlyaVorobyev Where is "npm"? Could you open terminal and type whereis npm; whereis node? Post the result. – DisplayName Sep 6 '15 at 23:52
  • @DisplayName btw, which not whereis.... – Schwertspize Sep 7 '15 at 5:59
0

Please... use the nice tool Automator!!! For heavens sake, it did not come with macOS for nothing!

So regardless of the Terminal commands that you want to execute, do the following:

  1. Launch Automator (which can be found in your Applications folder)
  2. Choose Application as the type and confirm by clicking
  3. In the Actions, choose Utilities and doubleclick or drag Run Shell Script to the right pane
  4. Copy/paste or enter the Terminal commands within the blank space within the window, you can leave all pulldown values to default
  5. Test if this is what you want with the Run (play symbol) button
  6. Go to File > Save... and pick a dedicated location to save the Automator script, giving it a recognisable name
  7. Create an alias to put on the desktop
  8. Done
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .