AutoHotKey Path Problem; Windows Shortcut Path

Perm url with updates:

AutoHotKey Path Problem; Windows Shortcut Path

Xah Lee, 2010-06-05

This page documents some file path problems of AutoHotKey scripting language, and also some compexlity of computing.


Suppose you have a Windows shortcut file, named “ErgoEmacs”, at “C:\Users\xah\Desktop\”.

Now, create a file with this content:

Run "C:\Users\xah\Desktop\ErgoEmacs"

Name it for example “test.ahk”. Double click to run it. AutoHotKey will complain about file not found.

However, adding the “.lnk” extension works:

Run "C:\Users\xah\Desktop\ErgoEmacs" ; works

But, other files without extension also works, example:

Run "C:\Users\xah\Desktop\emacs 23.2.1 -Q" ; works

This is odd. Here's a summary of what works and not:

; shortcut file name: ErgoEmacs
 Run "C:\Users\xah\Desktop\ErgoEmacs" ; works

; shortcut file name: ErgoEmacs
 Run "C:\Users\xah\Desktop\ErgoEmacs" ; no

; shortcut file name: ErgoEmacs- - Shortcut.lnk
 Run "C:\Users\xah\Desktop\ErgoEmacs- - Shortcut" ; no

; shortcut file name: ErgoEmacs- - Shortcut.lnk
 Run "C:\Users\xah\Desktop\ErgoEmacs- - Shortcut.lnk" ; no

Apparently, the problem isn't just about having or not having the “.lnk” extension there. I think it might also have to do with the shortcut name having a version string with multiple dot chars. It seems to confuse ahk in its attempt to interpret the path.

Use Full Name with Extension For Windows Scripting

A little test with PowerShell:

PS C:\Users\xah\Desktop> explorer.exe '.\ErgoEmacs 1.7.lnk'
PS C:\Users\xah\Desktop> explorer.exe '.\ErgoEmacs'
PS C:\Users\xah\Desktop> explorer.exe '.\emacs 23.2.1 -Q.lnk'
PS C:\Users\xah\Desktop> explorer.exe '.\ErgoEmacs- - Shortcut.lnk'

Shows that it all runs fine. So, the problem must be AutoHotKey.

When writing scripts on Windows, apparently the full file name with the “.lnk” extension is preferred. Also, apparantly when calling executables, full name with “.exe” is preferred. Because: (1) when pressing the Tab key for name completion, full file name is suggested. (2) when using a file without the “.lnk” extension, PowerShell refuses to open it. (it'll open a default directory)

This seems obvious now. When doing scripting in unix, usually the full file name is prefered, even though sometimes extension or program can name be omitted (shebang #!).

AutoHotKey Problem

I think i narrowed down a clean example of this problem. Create a shortcut “ErgoEmacs- - Shortcut” in your personal desktop. Then, the following ahk script will fail:

Run "C:\Users\xah\Desktop\ErgoEmacs- - Shortcut.lnk"

The error is:

ahk shortcut path error

Apparently, ahk tried to parse the path into parameters, probably because it sees the “.exe” part.

“.lnk” Extension Does Not Show

Another complexity that's interesting to note here is that the “.lnk” extension does not show, even if you have folder options to show file extensions. Wikipedia explains:

Microsoft Windows uses .lnk as the filename extension for shortcuts to local files, and .URL for shortcuts to remote files, like web pages. Commonly referred to as “shortcuts” or “link files”, both are displayed with a curled arrow by default, and no filename extension. (The extension remains hidden in Windows Explorer even when “Hide extensions for known file types” is unchecked in File Type options, because it is controlled by the NeverShowExt option in HKEY_CLASSES_ROOT\lnkfile in the Registry. The IsShortcut option causes the arrow to be displayed.)

Desktop File Location Mirror

Another potential complexity is that, on Windows, there are 2 locations for Desktop files, and shortcut files showing in the public one will automatically be mirrored on the user one by some virtual mechanism. (but not the other way, of course)

See: Windows Start Menu, Desktop, Quick Launch, File Locations


Overall, to understand the situation in depth, is to understand:

  • How exactly ahk interpret the path in “Run ‹file path string›”.
  • How exactly PowerShell interpret file and executable path. (i.e. how does it automatically add “.exe” if omitted.) Same question for “cmd.exe”.
  • What is the mechanism that mirror shortcut files from public desktop in user's desktop dir.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs