Perm url with updates: http://xahlee.org/mswin/autohotkey_path_problem.html
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 22.214.171.124”, at “C:\Users\xah\Desktop\”.
Now, create a file with this content:
Run "C:\Users\xah\Desktop\ErgoEmacs 126.96.36.199"
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 188.8.131.52.lnk" ; 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 184.108.40.206.lnk Run "C:\Users\xah\Desktop\ErgoEmacs 220.127.116.11.lnk" ; works ; shortcut file name: ErgoEmacs 18.104.22.168.lnk Run "C:\Users\xah\Desktop\ErgoEmacs 22.214.171.124" ; no ; shortcut file name: ErgoEmacs-126.96.36.199.exe - Shortcut.lnk Run "C:\Users\xah\Desktop\ErgoEmacs-188.8.131.52.exe - Shortcut" ; no ; shortcut file name: ErgoEmacs-184.108.40.206.exe - Shortcut.lnk Run "C:\Users\xah\Desktop\ErgoEmacs-220.127.116.11.exe - 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 18.104.22.168.lnk' PS C:\Users\xah\Desktop> explorer.exe '.\emacs 23.2.1 -Q.lnk' PS C:\Users\xah\Desktop> explorer.exe '.\ErgoEmacs-22.214.171.124.exe - 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 #!).
I think i narrowed down a clean example of this problem. Create a shortcut “ErgoEmacs-126.96.36.199.exe - Shortcut” in your personal desktop. Then, the following ahk script will fail:
Run "C:\Users\xah\Desktop\ErgoEmacs-188.8.131.52.exe - Shortcut.lnk"
The error is:
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)
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.