The Accidental Rubyist

invalid byte sequence in UTF-8

testing ecto scripts – ruby, perl and bash

leave a comment »

Today, i decided to be a little geeky. decided to play a little with ecto’s scripting abilities . Or rather its ability to plug in existing scripts. I do have scripts that textile-ize a file or STDIN and return the output to STDOUT. The first thing is that sadly one cannot run bash or perl or ruby scripts as-is. Surely, the folks at ecto had some strong reasons for that.
So some modifications are required. Essentially, when you invoke a script, the selected text, or the complete file (if not selection) is put into a temporary file. The name of the temporary file is sent as an argument to your script. Thus, you need to read that file for its contents, and write back to that file. Ecto does the rest.
Currently, most unix and perl/ruby scripts would be reading from STDIN and writing to STDOUT. However, ecto prints STDOUT in a dialog box. Great for messages or errors. If your perl script reads in the STDIN fine in “<>” then thats okay, but you will have to open the file and write back. Here is the sample:

    #!/usr/bin/perl
    # take textile text as stdin and output as html
    use Text::Textile qw(textile);
    my $text = "";
    my $entryfile = $ARGV[0];
    while (<>) {    $text .= $_;    }
    my $html = textile($text);
    #print $html;  <---- this line would print in a dialog box so commented off and next few lines added
    open ( FILE, ">$entryfile" ) or die;
    print FILE $html;
    close FILE;
   

In the ruby example, STDIN did not catch any output, so i had to open the file and read from it.

    #!/usr/bin/env ruby
    # Textile'ise things.
    # Take Textile markup on STDIN, and output HTML to STDOUT
    require 'rubygems'
    require 'redcloth'
    file = ARGV[0];
    text = File.open(file,"r").readlines.join;
    html = (RedCloth.new text).to_html
    File.open(file, "w").puts html;
   

Finally, in the spirit of playing around, here is a minimal bash file to append output to your post.
#!/bin/bash
ls -ltrh >> $1
The above perl and bash sample, were created after looking at the provided samples. I hope they make it easier for the first timer. I haven’t done anything with Apple Script, sorry i am too “last-century” (as Steve Jobs would say) to learn apple-scripting 🙂

Advertisements

Written by totalrecall

February 28, 2007 at 6:54 pm

Posted in ecto, ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: