quick calc

November 2, 2006 under Firefox, Google, Hacks, Python, Ruby

Often, I need to do some quick arithmetic involving multiple operations and don’t have a good calculator handy. There’s a calculator application on my cell phone, but I’m an atrociously slow cell phone typer. Most simple calculator apps, like calc.exe in Windows, are fine for single operations like 2+2 or 67 * 1.14, but for multiple-operation expressions, those simple apps won’t cut it without needing to resort to some copy ‘n’ pasting or [gasp] manual jotting with a writing utensil.

When I’m in front of a computer and I need to quickly calculate an arithmetic expression to figure out sales tax, find averages, multiply numbers too large for my brain to deal with, etc, writing a script or a total application would be overkill. Here are the cross-platform solutions that I find myself resorting to:

Python (python.org)
Python's IDLE
Python’s interpreter is convenient. It allows you to enter any mathematical express and it will evaluate it as if it were a line in a Python script. You could even assign values to variables or other data structures, use loops, conditions, and create functions like you would in any ol’ script.

Ruby (ruby-lang.org)
Ruby's IRB
Ruby’s interpreter has nearly identical features to Python’s interpreter, except that the language is Ruby instead of Python…duh 😉

Mozilla Firefox JavaScript Error Console (mozilla.org/firefox/)
In Firefox, selecting “Error Console” from the Tools menu item will launch the JavaScript Error Console window. The Error Console is useful for debugging JavaScript in Web applications. However, much like Python’s and Ruby’s interpreter, it too can quickly evaluate arithmetic expressions. Its main shortcoming, when compared to the aforementioned Python and Ruby interpreters, is that can only evaluate one line at a time. So you cannot store data in variables, since once you click the evaluate button, your variables are destroyed.
Firefox's JavaScript Error Console

Google (google.com)
Google’s powerful searching abilities are one thing, but did you know that it can also perform calculations (Ex: (99*66)2), unit conversions (Ex: 120 kilometres in miles) and even unit+currency conversion (Ex: help the in laws figure out Canadian gas prices ).

Instacalc (instacalc.com)
I’ve just recently come across this website and haven’t explored all of the features yet. However, it looks very promising. Using AJAX to display answers as you type in your expression is a nice touch.

For Windows users, there’s also the Power Calculator that’s part of the suite of Windows XP PowerToys. I keep it bound to the Calculator key on my keyboard.

better calculators + me = happy

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 1 »

SendKeys()? meh. how about SendFood() or SendMoney()?

April 1, 2006 under Computers, Programming, Ruby, SendKeys, VBScript

At work last week, it so happened that I had a very tedious manual task to perform. Due to an NDA, I can’t say which server application this task involved. Let’s just say that there isn’t an easy way to import lots values for a picklist field. I had to add 245 values from an Excel worksheet to 3 picklist fields – that would require me to switch to Excel, select the value, copy, switch to the server application, click “Add”, paste, click “OK” 735 times! There is apparently a way to do this by writing a .NET assembly, but I was assured that finding the neccessary info to accomplish this would take longer that actually doing the manual task. And that’s when the thought of my wrists in firey pain from repetive stress foced me to recall and old friend of mine from shell32.dll – the SendKeys() method.

SendKeys() does what it sounds like…it sends keystrokes. Plain and simple. For that task, I knew what keystrokes I needed to do. I could’ve written something in C# or VB.NET, since there’s a SendKeys() method in the System.Windows.Forms namespace, but time was not on my side. There’s nothing like the cling and static-free scripting languages to save the day 🙂 So using the Windows Scripting Host and 15 minutes of my time, I wrote some JScript code (as an aside, it appears that Microsoft is downplaying VBScript more and more all the time) that read all of the values from the Excel worksheet, stored them, and then switched back to the server application’s window to add values in the manual process. I could then take my hands off of the keyboard and watch my “ghost writer” do the work, saving me plenty of time and preventing a repetitive stress injury 🙂

SendKeys() seems old-school to me, but comes in handy when an application you’re working with doesn’t have any of its functionality exposed via COM or .NET or whatever. Even if it does, sometimes it’s quicker to send keystrokes than read API documentation. As a cheesy example, let’s fire up MSCONFIG and automatically switch to the “Startup” tab to see what’s launching when Windows boots:

// Let's get to the good stuff in shell32.dll.
var objWSHShell = WScript.CreateObject("WScript.Shell");
 
// Launch MSCONFIG.
objWSHShell.Run("msconfig");
 
// Wait a couple of seconds for MSCONFIG to launch.
WScript.Sleep(2000);
 
// Switch the focus to MSCONFIG.
objWSHShell.AppActivate("System Configuration Utility");
 
// Keystroke time!
// SHIFT+TAB sets the keyboard focus to the row of tabs.
objWSHShell.SendKeys("+{TAB}");
 
// Hit the right cursor key 5 times to get to the Startup tab.
for (var i = 0; i < 5; i++)
    objWSHShell.SendKeys("{RIGHT}");

Pretty simple, and you don’t need the Windows Scripting Host to use SendKeys() from a scripting language. You can use non-Microsoft languages too. For example, the scripting language that has my attention lately is Ruby – it’s like Python on steroids. This script will do the same thing as the one above, but this time, I’ll write it in Ruby (without comments):

require 'win32ole'
 
objWSHShell = WIN32OLE.new("WScript.Shell")
objWSHShell.Run("msconfig")
sleep(2)
objWSHShell.AppActivate("System Configuration Utility")
objWSHShell.SendKeys("+{TAB}")
 
5.times do
   objWSHShell.SendKeys("{RIGHT}")
end

Think of the fun you can have automating all sorts of tasks in Windows with the SendKeys() method 😉

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »

ridin' the rails with ubuntu

November 25, 2005 under Computers, Linux, Programming, Ruby, Ruby on Rails, Software, Ubuntu

I had enough of hearing about how wonderful Ruby On Rails is, so I wanted to see it for myself. Being the type of person who’s interested in many things all at the same time, I had a reason to try out another much-talked about thing. In order to properly try out RoR, I wanted a decent web server, so I installed Ubuntu Linux. Ubuntu is by far the easiest Linux distro I’ve used thus far. As such, its installation is quite basic – aiming at the desktop user demographic. But that didn’t stop me, and Ubuntu makes it simple to add what you want. Sometimes I used apt-get and other times I used the GUI app that allows you to search for and install new and updated packages.

First, I needed web server software, so installing and configuring Apache 2.0 was my top priority. The next thing that I installed was PHP 5.0; I’m familiar with 3.x and 4.x but I thought I should begin to explore 5. MySQL 5 wasn’t available in the repository at the time, so I installed the latest 4 release. To make life easier, I installed PHPMyAdmin; normally I prefer a command line but sometimes I just need to stave the RSI symptoms. Then came time to install the Ruby interpreter. Finally, using Gems, I installed Rails.

After browsing some tutorials, I saw how easy it was to generate the skeleton of a data-driven web application with Ruby On Rails. As such, it got me thinking of what’s possible now that a lot of overhead is taken care of by RoR. I just need a way to narrow down my list of possible project ideas.

Take a look at my screenshots below of my Ubuntu web server.

php_info() | RoR WEBrick | PHPMyAdmin

Total time to get everything installed and configured = 1.5 hours 🙂

EDIT (09.05.2006): If you’re interested in learning how to install PHPMyAdmin on Ubuntu Linux, I’ve posted something explaining how to accomplish that here.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
comments: 0 »