developer | thinker | tinkerer

Getting Filename with AutoHotkey

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.

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 Breaking Bad Habits - Stop Using The Mouse!

More like this...