Showing posts with label Variables. Show all posts
Showing posts with label Variables. Show all posts

Tuesday, January 1, 2008

Code Grinder 1: File Enumeration

Code Grinders: File Enumeration

So, a buddy of mine was talking to me the other day, and he was needing to get all the names of the files in a certain directory. I told him, easy, just use the 'Code Grinder' File Enumeration that's in the main QM Toolbar under 'Files, Web'. A 'Code Grinder' is a term I came up with to describe dialogs that help you come up with pre-formatted code with just a little bit of info from you. In the same way that you drop some meat, garlic, pepper, salt, sage into the hopper, turn it on and shove it all down the grinder's throat. It's the same here in QM. The grinder is usually a dialog of some kind and the meat and spices are locations or variable names.

Let's take a look at the File Enumeration grinder and see what we can come up with (or is that, ...'see with what we can come up'....no, that's not right either... ummmm....'see what can come up from ourselves'...yeah, that's totally not the right direction....man, I hate that whole, "you can't end a sentence with a preposition" thing...is it any better going on and on like this at the end of a sentence rather than a preposition?). Anyway, so start up that dialog and lets take a look. Click the File Enumeration icon in the 'Files, Web' menu and here's what we've got.

As you can see, it's a pretty straight forward dialog but there are some real useful features. First, you can browse to the folder you are looking for or you can choose 'Special Folders' that will be the same on all computers. It looks like this.

I've circled some of the things that are a real pain when moving from computer to computer, but with 'Special Folders' you can make it quite easy to get to those directories no matter who is logged in (doh, preposition again!). You can set date/time qualifiers as well as 'this folder/subfolders' options as well. Now where this grinder really gets cooking is in the 'Variables' section. If you need just filenames, just put in your variable name in the field. If you need the path, just put in your variable name in that field. If you need the size etc, etc, etc.

Here's the code I get from asking it to list the filenames on the desktop.

Dir d
foreach(d "$Desktop$\*" FE_Dir)
str sPath=d.FileName()
out sPath


Go ahead and dump that into your QM and see what happens. It's that easy. Now 'sPath' has the filename of each file as it loops through the directory. So, now you have a way to get some or all that info automatically (or at least so close to automatically that it makes little difference).


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
mesText=
;Hey guys you gotta check this out.
;
'Ad
url.getsel
mesText.from(mesText url)
mesText.setclip



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
mailto="mailto:Ender@battle-school.mil;Bean@battle-school.mil?subject=subHere&body="
subj.getwintext(win)
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
mesText=
;Hey guys you gotta check this out.%0D%0D
'Ad
url.getsel
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.