Thursday, May 3, 2007

Lesson 4: String Theory

Sorry, this post won't have anything to do with gravity wells, point particles, or CERN so you can keep your boson particle "foam fingers" in your truck (however, I do like me some tailgate grilling action so feel free to fire up that Grill Master 3000 and make my buffalo steak well-done ... WOOT!). Though I can't tell you the foundation of reality or, more importantly, what's underneath reality that is so sturdy as to support reality's foundation, I can say this, "Variables are the foundation of coding" and especially so in macros. So crack open that 12-pack of Diet Coke and toss 'em in the cooler and let's get this party started.

To first use a variable, you'll need to declare it by telling QM what kind it is and what you want to call it. Here's what it looks like in the wild.
str a
That was easy enough but what can you DO with it? Well, let's start by giving it some values; which can be done several different ways.

str a="anything you want"
Note: the use of the double-quotes.

str a.getclip
This gets the contents of the clipboard.

str a.getsel
This gets the currently selected text.
str a.getwintext(win)
This gets the tittle-bar text of the currently active window.

Now let's put 'em into play. Let's say you want to create a macro that will grab the url of the current web page that you are on and send an email to your buddies (cuz they don't get enough of that stuff from their moms). In FireFox and I.E. 7, if you hit Alt-d the address bar is given the focus and the entire contents are selected. Let's put that into a string variable and the combine it with another string variable and then copy all that to the clipboard ready to paste into an email. Here's how we do it.

str url mesText
;Hey guys you gotta check this out.
mesText.from(mesText url)

Now there are some strange things going on in there so let's take a look.

First, line one is a shorthand way of creating more than one variable on one line. Second, the variable mesText is assigned a value rather oddly. If you set it up like this, every line that follows the 'mesText=' line that has a semi-colon or a space in the front (in other words, that are marked as 'comments' in the code) are assigned as the variable value. This allows you to set big blocks of text without it running all the way across the page and manually entering in the line-breaks (BTW: use [] as the escape-characters for a line-break in QM). Moving on, we have the 'get selection' function (this gets the url). And then, we have the 'from' function. Now, this is a handy one. 'From' allows you to combine (concatenate) two or more string variables and or text 'chunks' that are surrounded by double-quotes. To say it in a human language you would say, "Tack on 'url' to the end of 'mesText'." And then it takes the new value of mesText and loads it to the clipboard. All you have to do is: open a new email; paste it into the body; add a subject; and address it.

Ummmm....yeah....that's too much freakin' work! So, let's turn up the juice and see what shakes loose (MAN, I love quoting old 80's flicks!).

str url mesText mailto subj ;;I'm creating a bunch of variables at one time
subj.findreplace(" - Mozilla Firefox" "");;I'm just pulling out the 'Mozilla Firefox' part cuz I don't need it
mailto.findreplace("subHere" subj);;I'm injecting the url into the subject parameter of the mailto command
;Hey guys you gotta check this out.%0D%0D
mailto.from(mailto mesText url);;I'm 'stringing' them all together here
run mailto

Oh yeah, now that's lazy with a capital "L" baby efficient and helpful; besides, you've got better things to do than menial cut/paste you've got to defend the Frontier against Xur and the Ko-Dan Armada!

So have at it and I'll see you next time cause the hook always brings you back.

No comments: