views:

675

answers:

3

Hi, I am trying to create a simple Automator droplet that will take the style.less file dropped into it and run the following LESS command on it:

$ LESSC {DROPPED_FILE} --watch

which will monitor the file I have dropped in for any changes and will then automatically update the outputted style.css

FYI: I am using LESS to have dynamically written CSS files. More info is here.

The basic droplet works great.

  1. The dropped file is passed into a variable; For this purpose: {MY_VAR}.
  2. I run a shell script in the /usr/bin/ruby shell as follows system("lessc {MY_VAR} --watch &")

this works great however I want the --watch to be stopped upon quitting the automator app.

The LESS documentation says to press the CTRL-C shortcut while in the command line shell to abort the script.

But since I am not inside the terminal window (command is being passed in the background) I don't know how to abort the script.

Even after the automator.app has been closed the style.less file is still being watched for changes and the correspondingly generated style.css is still being rewritten.

So basically I need to pass the abort command on exit of the .app.

I have generated a simple pop up that on click will close the app after passing another command to the terminal shell.

This is the part where all my tries have been unsuccessful to stop the script.

Is there a command line function that acts the same as pressing the CTRL-C command? How would I pass this the best to the shell?

Thanks for reading,

Jannis

A: 

less also terminates when it receives the "q" character, I think.

gregsabo
hmm.. I tried doing `$ lessc q` and it didn't work unfortunately. Thanks for the idea though, much appreciated.
Jannis
A: 

Hint: What does ^C Control-C really do in a shell?

(It doesn't send a character).

Clarification on hint: This PDF talks about signals. Note where it also talks about ^C. Your can manually send a signal as required.

When you press ^C in a terminal it normally sends the SIGINT signal to the program that is running. You can send these signals in Ruby as well. More information about working with processes (and sending signals) in Ruby: http://whynotwiki.com/Ruby_/_Process_management.

Happy coding.

pst
Does that mean there is no way to stop the script without using the terminal and actually pressing the CTRL-C key combo?
Jannis
Not at all. An interactive terminal just intercepts ^C and makes it do something special. The same as generally true ^D and ^Z as well, for instance. You can do what the terminal does directly.
pst
I read through the PDF but it's quite hard for me to understand since I am far from being a programmer. I just wanted to create this little droplet to make my workflow fast in the long run.It seems that that is not an option for me.Thanks for your hint none the less, maybe if I had more knowledge in this area I would have understood it better.
Jannis
+4  A: 

If you press CTRL-C in your console, this is not send via STDIN to your programm. Bash(or whatever you use) treats CTRL-C special. The bash sends a signal to the process. In case of CTRL-C this is SIGINT. To send a signal to a program you need to no it's pid. Then you can send the signal to the pid. To be able to get the pid you can start the process with ruby.

p= IO.popen("lessc #{file} --watch")
pid= p.pid
Process.kill("INT", pid)

In ruby there are at least 3 different ways, of executing something.

`lessc #{file} --watch` # returns stdout after exit
system("lessc #{file} --watch") # returns the integer return value after exit
Io.popen("lessc #{file} --watch") # returns imidietly an io object, where STDIN and STDOUT can be written and read

You used system("lessc #{file} --watch&") which also returns imidietly but always returns 0.

johannes
Thanks for taking the time to white this up. One quick question though, I am indeed very new to the command line ruby stuff, and so far have only been lucky copy pasting and following instructions I can find online, in this case: I am not quite sure how to actually run the command you gave me.. how do I enter my terminal instance into 'ruby' mode? Also: Is each line a separate command (as in I enter the first line and press 'return' then the 2nd line and so on?
Jannis
When I saw the ruby tag, I assumed you wanted to write a ruby script.
johannes