Posts tagged with "scripting"


Using TextExpander with Perl? Hell yes!

After hearing about it on my beloved 5by5 Back to Work, I finally started using TextExpander. Today, I realised it can be used with my lifehacking language of choice.

Way of the future

TextExpander lets you assign blocks of text (or even images) to a shortcut you assign. For example, when I type ;nc it expands out to all my ncurses boilerplate. ;sig expands to my name, address, email, EARLs and phone numbers. You could leave it at that, and be crazy productive.

But you don't have to stop there. If you create a new shortcut and choose choose "Context: Shell Script" from the top of the editing box, you can write shell scripts to process stuff. I ignored it initially, largely for the same reason I eschew (gesundheit) most Western fast food. I can eat it, but I prefer other things.

Earning some Siracusa cred

On a hunch this morning, I decided to test the limits of this "Context: Shell Script" box and enter some Perl (which of course I inserted with its own TextExpander shortcut!):

#!/usr/local/bin/perl [...]
print("Hello, world");

When I typed my ;test shortcut, Hello was inserted in it's place.

Oh. My. SCIENCE.

That's right, TextExpander took the output from this Perl script, and used it in the substitution. Think of the possibilities. No, seriously, think of them.

I've used Perl scripts, a Dock shortcut and the Mac pasteboard to automate and send results to where I want since 2004. Now I'm going to go through them all and see how I can make TextExpander shortcuts for all of them.


Perl 6 Rakudo Star

So it looks as though Perl 6 might be coming soon... again!

#!/usr/bin/env perl -wT
use strict;

&sayWooHoo("again");

sub sayWooHoo {
    print ("Hey, 6 might be coming soon, $_[0]!");
}

My first job out of high school was writing Perl programs. Was the happiest time of my life, because university hadn't crushed my spirit yet. Perl is awesome. CPAN is awesome! And don't believe what they say, it is possible to write Perl programs that you and others can understand later, provided you follow the conventions :).


Eyes, and scripting Tumblr posts to WordPress?

While I revel in this more traditional approach to blogging (when it works, looking at you WordPress), I do admit I'm jealous of how it easy it is for people on Tumblr to re-post entries from other tumblogs (that is the term, right?). Say... this one, for example!

So here's my idea

Usually I would start reading up on a site's API, but with so many things to do right now I decided to take the easy way out given I'd only be using it in a personal context. Here's my idea:

  1. Write a crappy Python script that accepts the URL/URI/address for a Tumblr entry I'd like to re-post in true Tumblr-like fashion.
  2. Use curl to grab the page, or fetch, or... heaven forbid, wget.
  3. Extract the textual information from the post, including the links to the people who re-posted it.
  4. Download local copies of the image(s) in a post and use ImageMagick to scale and convert them.
  5. SFTP to my webserver and upload the image(s)
  6. Connect to my WordPress database, add all the stuff, done.

Of course things aren't that simple!

How so? Ugh I hate rhetorical questions, mainly because I always need to consult a spell checker whenever I spell rhetorical.

For example, the picture of the girl above whom I can't stop looking at for some reason was saved on Tumblr as a gigantic PNG. What?! I know it could be saved as a JPEG and save a ton of space and bandwidth, but ImageMagick can't tell that from a diagram that should be kept as a PNG. In other words, it couldn't be an entirely automated process. Perhaps I could specify along with the URL I'm giving the script whether or not the images should be messed with. Ugh, but then it's not as automatic, which kinda defeats the purpose.

Secondly, Tumblr allows people to share far more than just pictures. If someone embedded a YouTube video or something else, what do I do then? I prefer hosting my own copies of stuff with attributions becuase I don't like relying on third party sites to stay alive. Why am I hearing Bee Gees all of a sudden? Ha, ha, ha, ha, staying alive... staying alive. Ha, ha, ha, ha, staying aliiiiiiive!

Thirdly, I shouldn't do this hack job and instead use the public APIs to get all the information I need and implement it in a WordPress plugin. It'd mean I'd have to actually learn PHP for real instead of just the hackish bits and pieces I've picked up from my Perl days and from maintaining MediaWiki and WordPress installs.

Okay I admit it!

This entire post was an excuse to post that picture. I'm a sucker for eyes, and I'm lonely, shaddup.


Android isn't evil, it's just not as awesome

Andreas Constantinou has written one of the most interesting blog posts about Android that I've read in a long while; he argues its claim to being open source is not only suspect, but that the platform's relative success has very little to do with it. The following are my own thoughts.

It's not as open

Years ago I wanted a Qtopia device, a chocolate-bar form factor phone made by the Qt framework folks. I was on the mailing lists for the OpenMoko phone for years as it missed deadline after deadline. The Neo1973 looked like a boat anchor, complete with an awkward handle for tying rope onto its frame.

For a sizable part of the 2000s I was a Mac user but longed to move to entirely free and open source systems because it was the "right thing to do", but as with many FLOSS efforts I gave up when they failed to deliver on promises.

When Android was announced my hopes were renewed, but as it quickly became clear it's advertised openness wasn't as clear cut as we were led to believe, as Andreas painstakingly points out. This has led to a few discussions personally on Twitter asking whether Apple's unabashed "play by our rules or leave" approach is more honest than Google's "we're all about openness" when there are even parts of their own systems that are off limits.

That said, I have no problem acknowledging they're light years ahead of Microsoft, and as consumers in general we're far better off with Android being the generic OS for phone makers than WiMo with either a creaky old codebase or a UI design that'd look great... in print!

It's not really original

Prior to the iPhone, the Android prototype looked like a Blackberry, right down to the tiny keys and the layout of the user interface. Fast forward a few years and aside from some awkward slide-out keyboards and trackballs that even most Android users claim is utterly unnecessary, Android devices look like iPhones with a few more buttons. That's not necessarily a bad thing, but one can hardly claim Android is original in any really meaningful way.

Daniel Eran Dilger wrote a brilliant piece in Roughly Drafted recently about the upcoming Android 2.2 software and how if Apple released something akin to it people would be up in arms.

While Apple has taken a public beating for being slow to deliver some significant features of the iPhone OS, including last year’s copy and paste or this year’s multitasking, it’s less well known that Android is missing key features that everyone just assumes it must have.

I still want one

While acknowledging the iPhone is a superior device in terms of usability, security and design, the nerdy part of me would still love to have an Android device, and not just so I'd have an icebreaker with Gina Trapani who I have a mad crush on.

One of the more limiting aspects of the iPhone [for me] is not being able to automate tasks that I would otherwise fire up a shell and hack away at on a desktop. Having a Python interpreter (or even just basic shell scripting) on a phone would be an absolute slice of heaven, and I know that Apple will never allow it on my current device. I could imagine Apple doing an Automater.app-like environment for the iPhone OS at some point in the future, but I'd still prefer being able to write some scripts that use APIs for the phone, text messages, email and so on, then upload them to the phone. There's so much stuff I repeat everyday that the phone itself could do for me.

All that said, having done no research into this I have no idea an Android device would allow me to much (or any) of this either, but I suppose if they didn't I could roll my own custom firmware... that is, if Google allows it.

It's 3am and I'm writing about phones. I should be asleep, dreaming about them instead. Hey, wait.


Shell work at 01:15 in the morning != smart


It still doesn't work... it still doesn't work... it still doesn't work...

I've heard it said by many programmers I respect that you spend 10% of your time coding, 90% debugging. I would argue it's closer to 5% coding, 95% debugging, but the point stands. It also stands true that the more tired you get, the less obvious basic mistakes appear to be.

Case in point, this evening I decided to update my MacPorts collection on my iBook and install Gnumeric (I've so far only switched to pkgsrc on my MacBook Pro). For the life of me I couldn't figure out why this single line wasn't working:

% sudo port -v selfupdate && port -v install gnumeric

If you've worked in UNIX for more than 5 seconds you'd be able to tell me in an instant what I did wrong, but for the last 10 minutes I just couldn't figure it out. It was driving me crazy! When I did I promptly hit my forehead on the table causing a glass of cold water I had placed on it to jiggle its way off the table and with an almighty crash land in a million pieces on the floor.

ASIDE: That glass thing didn't really happen, but doesn't it sound like something that could?

Lesson learned, don't do any programming, scripting or shell work when you're half asleep or not feeling well. You just don't get much done!

As for the mistake, I only used one sudo command. Using && doesn't carry over the higher user privileges! Simple, obvious, glaring mistake that was staring me in the face the entire time and I couldn't see it.


The best tool for the job is the one you can use

Food poising is not fun in the slightest, but fortunately by using a combination of soothing music and Tiger Balm my headache as of a few hours ago is completely gone. Mary Wallace and my GP suggested I eat bread, rice, apples and toast of which I'm eating right now. Provided I don't get out of this computer chair and don't eat too quickly I think I'll be fine.

With that in mind, I thought I'd share another quote, this time from the FreeBSD forums:

the daemon you know is better than the penguin you don't.
~ danger@

He's referring of course to BSD (the daemon) and Linux (the pengiun).

Reminds me of another similar quote by someone who I can't recall right now who said [paraphrasing] the best programming language for a job is the programming language you're best at and enjoy.

I've been learning a new programming language and a new OS to keep my mind sharp while I'm on holidays, you'll see the reviews of these in upcoming days. Ironically I started learning these because I thought I needed to broaden my horizons and get out of my FreeBSD, Mac, Ruby and Perl comfort zone. Those two quotes above pretty much shoot that down in flames don't they? I don't mind though, I still find them fascinating.

Without sounding too cheesy, I love learning new things, or using old things in new ways. It's one of the greatest pleasures in life.


I'd love to meet Python inventor Guido van Rossum

Guido van Rossum at OSCON 2006, by Doc SearlsWhile I admire Larry Wall and Yatsuhiro Matzumoto and use their respective Perl and Ruby programming languages, I can really relate on an entirely different level to Python inventor Guido van Rossum.

I only just discovered his new blog today, but I've read a lot of what he's written so far and have have agreed with a ridiculously high 99.995% of it. It's as if he's my older, wiser, wittier Dutch cousin living in California!

For example, it pains me to admit this, but I didn't know that Python was named as such because Guido was obsessed with Monty Python's Flying Circus and other absurd, hilarious British comedy:

By the way, the language is named after the BBC show "Monty Python’s Flying Circus" and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!

Frankly, if people have never watched any Monty Python, I have to question their sanity. I've also questioned the sanity of those chaps behind Monty Python too, but that's for another post! And now for something, completely different!

What I've always thought was curious was how both Larry Wall and Yatsuhiro Matzumoto could be designers of programming languages which require such logical thought, and yet be advocates of their associated churches. I didn't intend for that comparison to imply you can't be a logical person of faith, but it does strike me as a non-believer (to use the HIGHLY controversial term Barack Obama used!) to be a surprising and perplexing combination.

I'm not sure if Guido van Rossum is a non-believer too, but we both do seem to share similar philosophical viewpoints. For example, when he discussed the One Laptop Per Child Programme in his post Bibles or computers: Its the same thing:

I'm not surprised that the pope is pleased by the OLPC program. The mentality from which it springs is the same mentality which in past centuries created the missionary programs. The idea is that we, the west, know what's good for the rest of the world, and that we therefore must push our ideas onto the "third world" by means of the most advanced technology available. In past centuries, that was arguably the printing press, so we sent missionaries armed with stacks of bibles.

These days, we have computers, so we send modern missionaries (of our western lifestyle, including consumerism, global warming, and credit default swaps) armed with computers

I certainly hadn't thought of it that way before, and my secular mind did appreciate the comparison!

And now for something, completely different!

But back to computer science: I've never really looked into Python as a programming language, though I've heard good things about it from other "scripting language" users... C/C++ users don't count in this case because any language that's interpreted instead of compiled/tested/compiled sends them into fits of agony!

I was going to look into experimenting with Haskell before I head back to Adelaide in March, but perhaps I'll check out Python instead. As a Perl and Ruby guy I suspect the jump would be much easier than Haskell anyway, which is both good and bad of course. Python definitely looks like it has a very clean and minimalistic syntax which appeals to me visually as well as mentally, if that makes sense.

#!/usr/bin/env python
print 'You rock Guido van Rossum! \n'

Guido van Rossum also has quite a good Wikipedia page. I'm not Bill Kurtis.


Using env in shebang scripting language lines

Directly referencing your interpreter? Crazy...
Directly referencing your interpreter? Crazy...

I haven't written any geeky programming technical posts for a long time. Christmas Eve seems like just as good a time as any. Funny how Perl programming specifically always reminds me of Christmas because I got my first proper job after high school in 2004 writing Perl/MySQL code around Christmas time so I could buy people presents. Happiest time of my life then because I had purpose, direction and optimism.

This morning I'm talking about using env in the shebang line of scripting language source files (FreeBSD man reference). Quite frankly I'm surprised that as of 2008 the majoraty of files I download and look through still reference hard links to the interpreters such as the examples below:

#!/usr/bin/perl -w
#!/usr/local/bin/ruby

The problem with hard/specific referencing is that you can't assume your system will have a filesystem configured in the same way as another. While us users of Unix-like systems are fortunate that our said systems have so much in common, there are still enough subtle differences between Unix-like OSs and even distributions of the same OS to cause problems.

For example, most GNU/Linux systems place all non system critical files in the /usr/bin directory. As a FreeBSD user I shivver in terror at the mere thought of this; on the BSDs we're even more specific and seperate non system critical files that come as part of the core system in /usr/bin, and files we later install ourselves in /usr/local/bin or /usr/local/pkg depending on our package manager. Much stricter, cleaner and easier to maintain... but that's for another post.

The solution to this is to call our scripting language using env which checks your system for the desired interpreter and executes. This means Linux users can share with BSD users can share with Mac OS X users etc without worrying about how said systems are configured. You can even pass regular options, very nice.

#!/usr/bin/env ruby -w

Using env to reference basic sh shell scripts would be overkill, but for languages such as bash, Ruby, Python, Perl etc it just makes sense. Sure you could just assume all your clients and servers will be GNU/Linux folk, and perhaps you'd be right... for now. I'm all for future proofing.


Rubenerd Blog October 2008 rambling summary

A Swindon based 63 service
"A Swindon based 63 service" by The Oxford Bus Page

It's official, October 2008 has been the best month for the Rubenerd Blog since August 2006. In total 63 posts have been submitted compared to 23 posts last month and 37 the month before last. Realistically many of these have been long and involved but many more have been silly posts with less than three lines. Still, that's 63 random thoughts and ideas which isn't as high as the ridiculous 110 I made back in August 2006 but it's close.

In a kind of ironic twist, it seems that the more work and studying I need to do, the more material appears here. I think connecting my mind to a keyboard and letting all the ideas flow is a fantastic coping mechanism. It also allows me to keep my mind alert when I take breaks instead of being a passive consumer like I would be if I just watched TV.

ASIDE: For some reason I think this post will be getting the "pointless milestone" tag. The question is, how can I pull off putting a picture from an anime series on here too so I can use the "pointlessly fun anime reference" tag as well. These are the thoughts that keep me up at night. That and coffee. That made no sense.

Given I'm posting so many entries from my iPhone now as well, I think the next logical step in the evolution of this long winded and overly verbose trail of consciousness is to include some form of location metadata into each post. In this way I could see just how many words I have typed while I was sitting at the Boatdeck Cafe in Mawson Lakes or the huge Starbucks with lots of comfortable lounge chairs in Millennia Walk in Singapore. I have a sneaking suspicion they might be where most of this stuff gets done!

ASIDE: Millennia Walk was just across from where the Singapore Formula 1 night race was held. It's a really bizzare building with gigantic pyramid like skylights that stretch above the main shopping area by several whole floors. I did work for people around Suntec City and Millennia Walk after I finished high school in 2004.

Millennia Walk, Singapore
Millennia Walk, Singapore by williamcho

The place of the Rubenerd Show I think is also starting to evolve again. Previously I would speak everything I was thinking into that show, now it's much more of a spur of the moment type of project which I hope will improve the quality. I wish I had the self confidence that Frank Nora has by being able to speak into a voice recorder as I walk down the street with people staring at me the whole time... perhaps at some point I'll get to that stage.

At some point I would also like... ney LOVE, to be able to finally move all this material onto the content management system I wrote myself in Ruby (without Rails... it bothers me how people assume it must use it if it's a Ruby web app) earlier this year too. Perhaps after the exams and this latest project I'm doing for a client I'll take a solid week off to do that.

Yukikiro Matsumoto I'm fed up with all these constant WordPress updates; it is a great blogging system if you want millions of bells and whistles but don't want to do any programming yourself, but it's incredibly top heavy for what I want to do here. Not only that, but it's written in PHP. As a guy who also loves Perl, I have nothing against PHP, it's just Ruby is so much more Smalltalk like and is so elegant. Ruby code can be syntax-highlighted and printed onto posters it looks so nice. Yukihiro Matsumoto knew what he was doing!

ASIDE: My beautiful late mum thought that Yukihiro Matsumoto was cute.

I'd love to learn though how to better integrate MediaWiki with Ruby applications though, I love MediaWiki and I'd love to start using it again for my own projects. For a while I was a maintainer of an intranet wiki system and I wrote a lot of Ruby scripts that PHP would call using system calls, but it wasn't very elegant. If I could figure out how to better integrate them, I could write so many scripts for Whole Wheat Radio to do trippy things Jim would have to start blocking my IP address because I'd be using all WWR's idle CPU power. Wouldn't that be fantastic?

BRAINWAVE: Wouldn't it be great to be able to use the same user database for my Ruby CMS and MediaWiki? Then I could leave my CMS for the blogging portion, and use MediaWiki for the Wiki side, but the users would be consistant. I'm so smart it's scary sometimes. Okay, I'm more scary than smart.

Well that post started constructively and quickly deteriorated into rambling quickly... sorry about that. 63 posts though. Like the way I got back on topic again by referencing what I was supposed to be discussing? That's called "skill" and it's not something you can learn at any fancy tertiary educational institution.


Nitpicking open source and free... again

Despite really like Ruby and Perl, due to time constraints and other obligations I'm still reluctantly using WordPress and PHP on most of my blog powered sites including this one. Until I make the desperatly wanted switch, WP news still affects me and I take a somewhat interested view of what's going on. Not exactly a glowing endorsement, but then again it is the middle of the day here in Mawson Lakes so if I started glowing it would be a bit of a waste of energy.

It seems the widely used Revolution Theme for WordPress has gone open source. From the Weblog Tools Collection article:

Brian Gardner’s Revolution Theme for WordPress is going 100% Open Source. All the themes that are currently on Brian’s Revolution site will no longer be available as or October 31st and will be replaced with a set of new themes that will be developed and released under the GPL. The original Revolution themes will continue to be supported for those who have purchased them in the past.

Now I hate to be a nitpicker and certainly I consider myself more practical than ideological when it comes to the great software debate, but isn't this an example of something becoming Free Software and not just Open Source? Aren't most applications written in interpreted languages Open Source by their very nature because you can read the files? If Brian Gardner is releasing his themes under the GPL, then wouldn't that make them Free (as in speech as well as beer) instead of just Open Source?

I guess it boils down to disclosure; if you purchase a theme from someone instead of downloading it gratis, there's probably a clause limiting your right to redistribute or share the code. Still, isn't that more of an issue of the software not being Free, rather than it not being Open Source?

In any event I congratulate Brian for going down this path. I suspect he will be getting far more users and interest after doing this, and he deserves all of it.

And now I'm off for a Caeser salad. I've been having cravings for Caeser salad. Is that healthy?