Friday, August 9, 2013

Building a portable scripting environment, Part 1

As I've gotten tired of bouncing back and forth between machines, each having its own inconsistent development framework, I thought it a good idea to go ahead and create a single development environment that I could carry with me on a thumb drive.

This will look different for everyone, but here's my checklist:

Gotta-have's

  • Python (both 2.7.x and 3.x)
  • Git
  • XAMPP
  • Notepad++
Nice-to-have's
  • Eclipse w/ PyDev
  • Cygwin (or some other decent shell replacement)
  • Some form of GUI interface for GIT (a'la SourceTree, preferably)
  • PowerGUI or some other handy PowerShell IDE
  • AutoHotKey

Tonight, the Must-Have's...

First, Python. Thankfully, this one is pretty easy, with Portable Python having both version 2 and 3 available, and pre-packaged for portable use. The downloads are pretty large, and the installation takes a while when you install directly to a USB drive, but they come bundled with a lot of handy libraries, so it seems worth it.

As a sidenote, I see that Portable Python also comes packaged with PyScripter. I've never used this, personally, but if it's a decent enough IDE for my purposes, I may forego the Eclipse portion of my list and just stick with that.

Getting Git is also pretty easily handled, since msysgit releases portable versions - typically - alongside the desktop versions. Pick up the latest portable version from the download page, and extract onto the drive.

There's some config yet to do here, though, in order to truly have a portable version. This blog post covers it pretty well, so I'll skip the details here. If you're already familiar with the basic process and just need a refresher, you'll want to set your home directory to a local path within your git directory (I used /home/jeremy), set up your SSH key(s), and update your .profile to load ssh-agent for you, so you don't have to type your SSH password every time you use a git command that touches a server.

For those who aren't familiar with XAMPP, it's a bundle of Apache, MySql, Php, and Perl, for web development. I'll be using the Windows (WAMPP) bundle, available at the Apache Friends website.

Next on the Must-have list is Notepad++, my text editor of choice. It's already present on most of the systems I use, but on the off chance that I wind up working on a new/temporary machine, it'll be nice to have. Download the current version's .7z package from the Notepad++ website, and extract to your drive.

Once installed, create an empty file called doLocalConf.xml, so that N++ loads configuration files and plugins from the local directory instead of looking for them in your Windows user profile. Since I've got a Git Bash shell already open, this is easily accomplished via (my USB drive was drive G when I ran through this) touch /g/Notepad++/doLocalConf.xml.

Now that all of that's set up, I'm going to go poke around in PyScripter for a bit. Next post, I'll see how things went with these apps, and move on to some of the Nice-to-have's.

Use PowerShell to send test email in batches or on schedules

Recently, I had to test a scenario where an ActiveSync device received a relatively large (70-100) batch of emails in a relatively short (60-90s) window.

For smaller tests of this type, I've set Outlook into Offline mode, created 5-10 draft emails, sent them all, then switched back to Online mode to have them send at the same time. In this case, I wasn't interested in copypasta'ing 70 to 100 times per test, so I did some quick research, and found that Exchange 2010 has a Send-MailMessage cmdlet that ... well ... sends a mail message...

Anyhoo, a bit of Google-fu led me to this ASP.Net forum post. I ran the HTML code in the final reply through a parser, and found a script that pointed me to JFRMilner's Tech Blog. I saw an opportunity to expand that script and add some functionality, and some intense PowerShell scripting later, I had a finished product. You can check it out on Gist.

Wednesday, July 17, 2013

PowerGUI Syntax Highlighting

If you do any scripting in PowerShell, allow me to strongly recommend that you check out PowerGUI. It's exceptionally handy, and pretty tweakable.

After reading Jason Slaughter's TechNet Blog post on Exchange ActiveSync logging via PowerShell, I realized that I wasn't satisfied with the default syntax highlighting in PowerShell, and went on a quest to find out how to improve it. As it turns out, I wasn't able to find much on the topic online. The PowerGUI wiki mentions that it's possible to change via an XML file, but doesn't go into much more detail than that. Adam at CSharpening.net has a blog post with the results of his foray into the topic, which also pointed me to the list of named colors that could be used, which came in handy.

Using this as a foundation, I got to work.

First, a couple of quick notes. In order to save your changes to the PowerShellSyntax,xml file, you're going to have to open it with elevated rights. Also, these changes don't take effect immediately, so you're going to have to close and re-open PowerGUI each time you make changes in order to see their effects.

As I worked through the different styles, I realized that I actually wasn't sure when some of them were supposed to be applied, so I wrote a syntax test script with examples of each type, and a block at the bottom that lists the strings that trip each type. You can snag it from GitHub.

One of the first things I noticed when working my way through the XML was that the AutoVars style was not used anywhere. There's a PSAutoVars group defined, but it uses the VariableStyle style. I changed that, since a separate highlighting style makes sense for these variables:

Find:
<ExplicitPatternGroup TokenKey="PSAutoVars" TokenID="23" Style="VariableStyle" LookAhead="{NonWordMacro}|\z" CaseSensitivity="AutoCorrect">

Replace with:
<ExplicitPatternGroup TokenKey="PSAutoVars" TokenID="23" Style="AutoVars" LookAhead="{NonWordMacro}|\z" CaseSensitivity="AutoCorrect">

In the list below that line, I also added $true and $false, since those are also built-in variables that I wanted highlighted using this style.

That done, I messed with styles themselves. Here's what I finally settled on:
<!-- Highlighting Styles -->  <Styles>    <Style Key="ReservedWordStyle" ForeColor="Blue" Italic="True" />    <Style Key="OperatorStyle" ForeColor="Red" />    <Style Key="OperatorWordStyle" ForeColor="Red" />    <Style Key="VariableStyle" ForeColor="DarkSlateGray" Bold="True" />    <Style Key="CmdletStyle" ForeColor="Blue" />    <Style Key="NetClassStaticStyle" ForeColor="teal" />    <Style Key="NetClassStaticMethodStyle" ForeColor="saddlebrown" />    <Style Key="CmdletParamStyle" ForeColor="Navy" />    <Style Key="NumberStyle" ForeColor="Black" />    <Style Key="StringDelimiterStyle" ForeColor="Purple" />    <Style Key="StringDefaultStyle" ForeColor="Purple" Italic="True" />    <Style Key="CommentDelimiterStyle" ForeColor="Green" />    <Style Key="CommentDefaultStyle" ForeColor="Green" />    <Style Key="AutoVars" ForeColor="DarkOrange" Bold="True"/>    <Style Key="FunctionStyle" ForeColor="CadetBlue" />  </Styles>


And a side-by-side for ya:






Thursday, January 17, 2013

Quick Send in Gmail's new Compose window

If you're an Outlook user at work (like myself), this may make you very very happy.

In Outlook, you see, when composing an email, the Ctrl+Enter hotkey can be used to send the message without having to move your hand to your mouse and click Send1. If this is the first you've heard of it, give it a try the next time you compose an email, and you'll probably be addicted.

This is such a natural and simple operation to me that I found myself doing it in Gmail without thinking. Sadly, in the early days of Gmail, no such shortcut existed. I could press Tab, then Enter instead, which was a perfectly workable substitute, with the sole downside of having to remember one shortcut for one application and another for a different one.

Recently, Gmail updated to a new compose interface to allow us to write messages while still viewing our Inbox. Brilliant! I've been using it since it was first offered on an opt-in basis, and I love it.

Today, I loved it even more. I opened a new message, finished writing it, and pressed Ctrl+Enter out of habit... and the message sent!

As a fervent advocate of hotkeys, I've heard numerous people complain that learning this series of hotkeys for this app and that series for that app that does the same thing was just too hard. I have no rebuttal for that - I tend to agree, though I still find the use of hotkeys to be something that saves me hours of time and focus, so I will still slog through attempts to learn them.

I'm glad to see Google adopting hotkeys like this one from software that's already familiar to their users. It will make everyone's lives easier, in the long run.


[1] - As will become rapidly apparent in my postings here, I'm a huge fan of keyboard shortcuts over mouse movements. If this seems like an insignificant thing, think about how often you have to take a hand off of the keyboard, move it to your mouse, select a new field or window, then remove that hand from the mouse and move it back to your keyboard so you can resume typing. If you don't feel that that happens often, rock on!

If - on the other hand - you have just come to the realization that you are frequently wasting time, energy, and focus on a circular motion that, in many cases, can be replaced by pressing a key (or a combination thereof), you are in the same boat as the vast majority of computer users.