Getting Filename with AutoHotkey

January 8, 2010 by Jim · Leave a Comment 

So in my previous post (Authotkey and Eclipse Autocomplete) I was trying to replace a snippet with an AutoHotkey script.  One thing I was lacking was automatically inserting the file name which you can easily do in a snippet by inserting $${CURRENTFILE} into your snippet.

Doing this in AutoHotkey was a bit more involved…

There didn’t seem to be a way to directly get the file name. After another post on the forum someone suggested I look at WinGetTitle which will get the title of the active window.  Looking at CFEclipse the title does show the file name:

Screenshot: Eclipse Titlebar with Path

Eclipse Titlebar with Path

The path was there but also some extraneous text I didn’t want.  There is probably an easier way to do this with RegEx but I went for a quick and dirty replace:

WinGetTitle, Title
StringReplace, Title, Title, % " - Eclipse"
StringReplace, Title, Title, % "CFEclipse - "

So now I had a cleaned up path but I really just wanted the file name. Turns out AutoHotkey has a SplitPath command which you can use to return various bits of the path: drive, path, filename.

My initial attempts trying this didn’t work. Then I realized since AutoHotkey was a Windows program it was confused by the path returned by Eclipse.  I did another replace to flip the backslashes:

StringReplace, Title, Title, /, \, All
SplitPath, Title, Title

And this time I got just the file name!

Authotkey and Eclipse Autocomplete

January 8, 2010 by Jim · Leave a Comment 

Yesterday I was trying to write up a quick AutoHotkey script to quickly dump in a Fusedoc snippet into my files.

I was tearing my hair out (I don’t have much left!) because I kept getting extraneous characters printing out after the script ran.  After quite a bit of trial and error it finally dawned on me what was happening.

I was using SendInput in AutoHotkey to print out characters and when running this in Eclipse the autocomplete was kicking in!  So if I entered “<”  it would throw in “>”.   I tested this by running the same script in Notepad and it printed out fine.

I was a bit stumped but someone on the AutoHotkey forum suggested using the clipboard instead!!  This morning I tinkered a bit and rewrote my script to instead append everything to the clipboard and then paste it.

First we need to setup our clipboard:

clipboard =

That was easy :)   Now we can simply append items to it.  The `r`n is a line break.

clipboard = %clipboard% <!--- `r`n
clipboard = %clipboard% <fusedoc fuse="%currentFile%" language="ColdFusion" specification="2.0">`r`n
clipboard = %clipboard%    <responsibilities>%fuseDocResponsibility%</responsibilities>`r`n
...

Then finally we can dump it back to the screen by doing a CTRL+V:

...
clipboard = %clipboard% ---> `r`n
send ^v

By using the clipboard this pastes as one item into Eclipse and bypasses the autocomplete!  The full script is below:


:*:;fd::
FormatTime, TimeString, ShortDate
SetKeyDelay, 20 ;in millisceonds
InputBox, fuseDocResponsibility, Attribute Prompt, Responsibility..., , 300, 150
If ErrorLevel = 1 ; They clicked cancel
return
Sleep, 100

clipboard =
clipboard = %clipboard% <!--- `r`n
clipboard = %clipboard% <fusedoc fuse="" language="ColdFusion" specification="2.0">`r`n
clipboard = %clipboard%    <responsibilities>%fuseDocResponsibility%</responsibilities>`r`n
clipboard = %clipboard% <properties>`r`n
clipboard = %clipboard%    <history type="Create" author="Jim Priest" comments="" date="%TimeString%" role="Developer" email="priestj@my.work.email"></history>`r`n
clipboard = %clipboard% </properties>`r`n
clipboard = %clipboard% <io>`r`n
clipboard = %clipboard%    <in></in>`r`n
clipboard = %clipboard%    <out></out>`r`n
clipboard = %clipboard% </io>`r`n
clipboard = %clipboard% `r`n
clipboard = %clipboard% ---> `r`n
send ^v
return

Autohotkey + Dropbox = The Perfect Combo

August 28, 2009 by Jim · Leave a Comment 

So you’ve downloaded Autohotkey and are writing all kinds of automation scripts to help you out during the day at work.  But what about home?  You can setup routines to help you out there as well. I have several setup for email signatures, shortcuts for my home PC, etc.

To make juggling your Autohotkey scripts easier take a look at Dropbox.  I’ve been keeping more and more stuff in Dropbox lately. It serves as a simple version control system, and best of all it’s truly cross platform so I can dual-boot my laptop into Windows or Linux and access the same files!

With Autohotkey you can drop your .ahk file in Dropbox and access your shortcuts from any computer (with Autohotkey installed of course).

Autohotkey Week: Day 5 – Conclusion

August 21, 2009 by Jim · Leave a Comment 

So hopefully this week I’ve provided you with a few ideas to get you started with Autohotkey!   Here are a few more links to help you…

Autohotkey

Similar tools:

More resources:

If YOU create a useful Autohotkey script please let me know.  I am planning on a opening a section on my wiki to store and share scripts!

Autohotkey Week: Day 4 – Launching Apps and GUI

August 21, 2009 by Jim · 1 Comment 

So last session we looked at using the clipboard.  Today we’ll explore using Autohotkey to launch applications and build a very simple GUI.

I’m always amazed at the effort people go through to launch applications.  Desktops full of icons, long Start menus, quick launch bars full of icons.  All those take an amazing amount of time to process through, find your app and click.

With Autohotkey we can reduce all that searching down to one simple keystroke.  I start CFBuilder many times throughout the day by simply clicking F6:

;F6------------------------------
SC040::
Run C:\eclipse-bolt\eclipse.exe -vm "C:\Program Files\Java\jre6\bin\javaw" vmargs -Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=256m
return

This is really straightforward – SC040:: translates to the F6 key on my Microsoft Natural keyboard.  I then simply execute a standard Run command and point it to the Eclipse executable with the appropriate startup flags appended.

Now I click F6 and CFBuilder starts. No hunting, searching, digging through menus, etc.  I have a series of 3-4 programs I use very frequently all assigned to hot keys for easy startup.

Autohotkey is far more than text replacement and application shortcuts.  You can also build complete GUIs with Autohotkey.  I’ll direct you to the documentation online if you want to pursue this but I will show you a very simple example.

At work I’ve been trying to keep better track of my time throughout the day and stumbled across this Autohotkey script to ask for user input every XX minutes and write it out to a text file:

SetTimer, #a,900000
#a::
; Show the Input Box to the user.
inputbox, text, Diary,,,300,100

; Format the time-stamp.
FormatTime, CurrentDateTime,, MM/dd/yyyy h:mmtt  ; It will look like 9/1/2005 3:53 PM

; Write this data to the diary.txt file.
fileappend %CurrentDateTime% | %text%`n, c:\diary.txt
return

Here we’re assigning our script to WIN+A, and using the SetTimer function to click that every 900000 milliseconds.  When you click WIN+A (or the timer clicks it) an input dialog will be displayed where you can enter text.  I’m also writing a time/date string to include as well.  This data is then appended to a defined text file – in this case diary.txt.

So I end up with a .txt file containing a list of things I do throughout day:

08/20/2009 2:05PM | Complete weekly report
08/20/2009 2:21PM | Surf Reddit
08/20/2009 2:35PM | Get Diet Coke
08/20/2009 2:50PM | Work on Bug-123

The Autohotkey site has some much more complex GUI examples I’d encourage you to check out if this interests you.

Next post we’ll wrap this up, and I will provide some links to similar tools and other resources that will help you get started with Autohotkey.

Autohotkey Week: Day 3 – Clipboard

August 19, 2009 by Jim · Leave a Comment 

So now we have Autohotkey installed and have learned to do some basic text manipulation.  Next we will explore how we can use Autohotkey to manipulate existing text by using the clipboard.

Remember in our previous example how we used Autohotkey to go UP and LEFT to position our cursor?  Since Autohotkey can press keys for us, there is no reason why it can’t press CTRL+C for us!  Here is one I use all the time – the classic CFDUMP:

F1::
clipboard =
send ^c
clipwait, 1
If ErrorLevel = 0
{
clipboard = <!--- TODO - for debugging --->`n<cfdump var="#%clipboard%#" top=5 label="My Dump">`n<cfabort>
send ^v
}
return

We are getting a bit more complicated but I think you should be able to follow along.  I’ve got this setup to fire when I hit F1 (you of course can select any key). BUT I first have to select something or there will be nothing to copy!  So select something, hit F1 and it does a send ^c which is the Autohotkey syntax for ‘press CTRL+C‘.  It waits a sec, checks for errors and then writes out a new string to the clipboard and pops in the text we first selected  %clipboard% and outputs a nice string with our variable wrapped up:

<!— TODO – for debugging —>
<cfdump var=”#myVariable#” top=5 label=”My Dump”>
<cfabort>

Here is another great use. I’m working on an old project with no queryparams.  Ugh!  So I wrote this shortcut to grab a variable from a query, strip out the single quotes (if they exist) and wrap it with a variable.  I’ve got a few of these defined so I can output a varchar or numeric depending on my shortcut.

!q::
clipboard =
send ^c
clipwait, 1
If ErrorLevel = 0
StringReplace, clipboard, clipboard, ', , All
{
clipboard = <cfqueryparam value="%clipboard%" cfsqltype="cf_sql_varchar">
send ^v
}
return

I have several more of these setup to do UCase(), Upper(), Trim(), etc. All by simply modifying the clipboard.  Hopefully by now you are seeing the power of Autohotkey!  Tomorrow we’ll explore how you can use it to fire off programs.

Autohotkey Week: Day 2 – Text Manipulation

August 18, 2009 by Jim · 3 Comments 

One of Autohotkeys most basic uses is text replacement or manipulation.  Here is one I use daily to insert date/comment in my code:

:*:;cmt::
FormatTime, TimeString, ShortDate
SendInput,  %TimeString% - Jim Priest - priestj@niehs.nih.gov
return

So now when I’m in my text editor I simply type ;cmt and it inserts the date time and my information:

10:27 AM Tuesday, August 18, 2009 – Jim Priest – priest@myworkdomain.com

This uses some simple date formatting options and then does a SendInput to print out the output.

Now you may be asking why not use an CFEclipe/CFBuilder snippet?  Well, a lot of times I’m editing files outside of Eclipse.  Maybe in Notepad, or an email, or on our company wiki.  Autohotkey shortcuts work everywhere!

Here’s another usage example.  I’m on the CFEclipse mailing list and often answer bug questions with a standard response.  I got tired of typing that over and over so I created a shortcut:

:*:;bug::
SendInput, Can you provide us with more detail on your setup?{Enter}
SendInput, {Enter}
SendInput, http://trac.cfeclipse.org/cfeclipse/wiki/ReportingBugs{Enter}
SendInput, ------------{Enter}
SendInput, When submitting a bug or if you email the mailing list with an issue, please provide as much information as possible:{Enter}
SendInput, {Enter}
SendInput,   * Eclipse version: Help > About Eclipse SDK{Enter}
SendInput,   * CFEclipse version: Help > About Eclipse SDK > Plug-in Details{Enter}
SendInput,   * Java version: From a command line type "java -version"{Enter}
SendInput,   * OS - Windows? OSX? Linux?{Enter}
SendInput,   * Error log information {Enter}
SendInput, ------------{Enter}
return

This example is very similar to the comment shortcut above. We simply use SendInput to dump some text out.  Note the use of {Enter} to force line breaks.  So now if I’m on Gmail or Google Groups I can simply type ;bug and this will get dumped in eliminating a lot of typing and also reducing the chance I might forget something.

Can you provide us with more detail on your setup?

http://trac.cfeclipse.org/cfeclipse/wiki/ReportingBugs

————
When submitting a bug or if you email the mailing list with an issue, please provide as much information as possible:

* Eclipse version: Help > About Eclipse SDK
* CFEclipse version: Help > About Eclipse SDK > Plug-in Details
* Java version: From a command line type “java -version”
* OS – Windows? OSX? Linux?
* Error log information
————

My final example will demonstrate how to prompt for user input.

:*:;cfif::
Send,  <cfif>{Enter}{Enter}<cfelse>{Enter}{Enter}</cfelse></cfif>
Send {UP 3}
Send {LEFT 2}

SetKeyDelay, 20 ;in millisceonds
InputBox, attName, Attribute Prompt, Attribute Name..., , 300, 150
If ErrorLevel = 1 ; They clicked cancel
return
InputBox, attValue, Attribute Prompt, Attribute Value..., , 300, 150
If ErrorLevel = 1 ; They clicked cancel
return
Sleep, 100
SendRaw, %attName% = %attValue%
returnc

In this example first we type out our standard cfif/cfelse blog (using ;cfif as the shortcut) with the Send command, then we use some built in Autohotkey commands UP and LEFT to navigate back to where we want our cursor.  We then open an input box and ask the user to enter the variable and value.  Since our cursor is already in the correct place (right after the first cfif) we simple send the information captured back to the screen and we end up with:

<cfif myVariable = myValue>

<cfelse>

</cfelse></cfif>

Hopefully these basic examples will get your wheels turning.  Tomorrow we’ll explore using Autohotkey and the clipboard together.

One of Autohotkeys most basic uses is text replacement or manipulation.  Here is one I use daily to insert date/comment in my code:

:*:;cmt::
FormatTime, TimeString, ShortDate
SendInput,  %TimeString% - Jim Priest - priestj@niehs.nih.gov
return

So now when I’m in my text editor I simply type ‘;cmt’  and it inserts the date time and my information:

10:27 AM Tuesday, August 18, 2009 – Jim Priest – priest@myworkdomain.com

This uses some simple date formatting options and then does a SendInput to print out the output.

Now you may be asking why not use an CFEclipe/CFBuilder snippet.  Well, a lot of times I’m editing files outside of Eclipse.  Maybe in Notepad, or an email, or on our company wiki.  Autohotkey shortcuts work everywhere!

Here’s another usage example.  I’m on the CFEclipse mailing list and often answer bug questions with a standard response.  I got tired of typing that over and over so I created a shortcut:

:*:;bug::
SendInput, Can you provide us with more detail on your setup?{Enter}
SendInput, {Enter}
SendInput, http://trac.cfeclipse.org/cfeclipse/wiki/ReportingBugs{Enter}
SendInput, ------------{Enter}
SendInput, When submitting a bug or if you email the mailing list with an issue, please provide as much information as possible:{Enter}
SendInput, {Enter}
SendInput,   * Eclipse version: Help > About Eclipse SDK{Enter}
SendInput,   * CFEclipse version: Help > About Eclipse SDK > Plug-in Details{Enter}
SendInput,   * Java version: From a command line type "java -version"{Enter}
SendInput,   * OS - Windows? OSX? Linux?{Enter}
SendInput,   * Error log information {Enter}
SendInput, ------------{Enter}
return

This example is very similar to the comment shortcut above. We simply use SendInput to dump some text out.  Note the use of {Enter} to force line breaks.  So now if I’m on Gmail or Google Groups I can simply type ‘;bug’ and this will get dumped in.

My final examply will demonstate how to prompt for user input.

:*:;cfif::
Send,  <cfif>{Enter}{Enter}<cfelse>{Enter}{Enter}</cfelse></cfif>
Send {UP 3}
Send {LEFT 2}

SetKeyDelay, 20 ;in millisceonds
InputBox, attName, Attribute Prompt, Attribute Name..., , 300, 150
If ErrorLevel = 1 ; They clicked cancel
return
InputBox, attValue, Attribute Prompt, Attribute Value..., , 300, 150
If ErrorLevel = 1 ; They clicked cancel
return
Sleep, 100
SendRaw, %attName% = %attValue%
returnc

In this example first we type out our standard cfif/cfelse blog with the Send command, then we use some built in Autohotkey commands UP and LEFT to navigate back to where we want our cursor.  We then open an input box and ask the user to enter the variable and value.  Since our cursor is already in the correct place (right after the first cfif) we simple send the information captured back to the screen and we end up with:

<cfif myVariable = myValue>

<cfelse>

</cfelse></cfif>

Hopefully these basic examples will get your wheels turning.  Tomorrow we’ll explore using Autohotkey and the clipboard together.

Autohotkey Week: Day 1 – Installation

August 17, 2009 by Jim · Leave a Comment 

So during Marc Esher’s awesome Automation preso at CFUnited I was surprised he didn’t talk about Autohotkey.  What’s Autohotkey you ask?  It’s a free, open-source Windows application that can add quite a few tools to your automation tool belt.  There are some similar tools out there for Mac and Linux which I’ll cover in another post.  So what can we do with Autohotkey?

  • Automate almost anything by sending keystrokes and mouse clicks.
  • Create hotkeys for keyboard, joystick, and mouse.
  • Expand abbreviations as you type them. For example, typing “btw” can automatically produce “by the way”.
  • Create custom data-entry forms, user interfaces, and menu bars.
  • Remap keys and buttons on your keyboard, joystick, and mouse.
  • Convert any script into an EXE file that can be run on computers that don’t have AutoHotkey installed.

This week I’ll try to cover at a basic level everything you can do with Autohotkey at a basic level.  So what are you waiting for! Go download it! http://www.autohotkey.com/

Installation is straightforward. Run the executable, keep the defaults and follow the prompts. The first time you start Autohotkey, it will prompt and ask if you’d like to create a sample script  ( autohotkey.ahk ) in your My Documents folder.

Once Autohotkey is started you will notice a new icon in your taskbar (green box with big H). You can interact with Autohotkey via a right-click on the icon.  From here you can edit, reload, pause and exit from Autohotkey.

So now let’s experiment and see what Autohotkey does. If everything is setup correctly you should be able to hit WIN+Z (windows key) and your browser should fire up and display the Autohotkey site.  Now open up Notepad.  Before you head for the Start menu button, try this instead:  CTRL+ALT+N.  Notepad should fire up.  Now we can navigate over and open up the autohotkey.ahk file (remember it was created in My Documents).

This file is heavily commented but the main section we are interested in are:

#z::Run www.autohotkey.com

This simple line breaks down as follows:

#z – simple means when you hit WIN+Z – let’s do something.
RUN - means we’re going to well, run something – in this case it’s a URL
www.autohotkey.com And since we’re passing in a URL – Windows launches our default browser and takes us to the Autohotkey site.

Simple! Lets look at the next script:


^!n::
IfWinExist Untitled - Notepad
WinActivate
else
Run Notepad
return

The next example is a bit more complicated but I think any good ColdFusion programmer should be able to read through this and understand what’s going on…

^!n = CTRL+ALT+N
IfWinExist – simply examines the running applications for one with a title of “Untitled – Notepad”.  If that exists then it will grab the focus and display it, if not it will start Notepad.

I have several keys setup to launch my frequently used programs.  No start menu, no keyboard shortcuts to a launcher where I have to type in “CF Builder”.  I simply hit F6 to fire up CFBuilder. F7 fires off Oracle SQL Developer:


;F6------------------------------
SC040::
Run C:\eclipse-bolt\eclipse.exe -vm "C:\Program Files\Java\jre6\bin\javaw" vmargs -Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=256m
return

;F7------------------------------
SC041::
Run C:\Program Files\Oracle\sqldeveloper\sqldeveloper.exe
return

In this case the SC041::  are simply shortcuts for special keys on my Microsoft keyboard correlating to F6/F7.

Tomorrow we’ll look at using Autohotkey to replace text!

Next Page »