AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Run command fails launching .lnk with parameters

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
orbik



Joined: 09 Apr 2008
Posts: 35
Location: Espoo, Finland

PostPosted: Mon Oct 26, 2009 8:42 pm    Post subject: Run command fails launching .lnk with parameters Reply with quote

I have a shortcut "c:\bin\HDGraph.lnk". c:\bin is in %path%, and .LNK in %pathext%.
So I should be able to run the program anywhere with "hdgraph", and indeed it works. But...

Adding a parameter breaks ahk. E.g. the command "hdgraph d:\" works fine from the run dialog and command prompt, but ahk says
"Error: Failed attempt to launch program or document:
Action: <hdgraph d:\>" :(

Here's the code I wanted to run:
Code:
; HDGraph.. Disk usage visualization
#t::
   folder := ShellFolder(WinActive("A"))
   if folder
   {
      Run, hdgraph %folder%
   }
return


Same also happens whether or not the extension and path are explicit, or if the parameter is a variable. I.e. the following also fails:
Code:
Run, c:\bin\hdgraph.lnk d:\


Interestingly, other extensions such as .bat and .cmd work properly, so I could work around by running hdgraph.cmd instead with parameters explicitly typed "hdgraph.lnk %1 %2 ..." but that would be cumbersome.
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Tue Oct 27, 2009 5:24 am    Post subject: Reply with quote

A search of the forum seems to indicate that the Run command from AHK may not handle some .lnk files well because it is not able to return the target .exe, whereas the common Windows run dialog can(see here). You could try using FileGetShortcut to return the target, but it might be better to Run the target .exe directly and pass it the arguments while opening your file.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Tue Oct 27, 2009 1:12 pm    Post subject: Reply with quote

sinkfaze wrote:
A search of the forum seems to indicate that the Run command from AHK may not handle some .lnk files well because it is not able to return the target .exe,
I'm fairly certain this problem is caused by limitations of the way AutoHotkey parses command lines, and not to do with retrieving the target of a link.

In most cases, Run passes the command line directly to the Win32 function CreateProcess. If the command line specifies an executable file with or without arguments (such as "notepad foo"), it usually succeeds, otherwise it usually fails. If CreateProcess fails, Run will split up the command line and pass it to the Win32 function ShellExecuteEx. ShellExecuteEx is a higher-level function which handles file associations, shortcuts, etc. but requires that the filename and arguments be specified separately.

Unfortunately, Run is currently designed to support arguments only with exe, bat, com, cmd and hta files; for instance, I suppose it interprets "c:\bin\hdgraph.lnk d:\" as a filename. You can confirm by looking at the error message: if it says "Action: <c:\bin\hdgraph.lnk d:\> Params: <>", it must be trying to run a file named "hdgraph.lnk d:\" with no arguments.

As a workaround, you may explicitly call ShellExecute (or ShellExecuteEx) with the correct filename and arguments:
Code:
DllCall("shell32\ShellExecuteA", "uint", 0, "uint", 0
        , "str", A_MyDocuments "\np"  ; path of file
        , "str", "foo"  ; arguments
        , "uint", 0, "int", 1)
To test, I created a shortcut to Notepad, named it np and placed it in my Documents folder. When I run the script, Notepad opens up and tells me "Cannot find the foo.txt file." (This indicates the test passed.) Note that it works even without specifying the ".lnk" file extension.
Back to top
View user's profile Send private message Visit poster's website
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Tue Oct 27, 2009 1:58 pm    Post subject: Reply with quote

Well that makes more sense than my explanation! I saw the example on the page I linked to but the conversation was just esoteric enough to keep me from fully comprehending it.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
HoffiMuc



Joined: 16 Aug 2008
Posts: 7

PostPosted: Wed Oct 13, 2010 10:54 am    Post subject: 64 Bit Windows 7 Reply with quote

Quote:
DllCall("shell32\ShellExecuteA", "uint", 0, "uint", 0
, "str", A_MyDocuments "\np" ; path of file
, "str", "foo" ; arguments
, "uint", 0, "int", 1)


is "shell32\ShellExecuteA" also valid (and/or good/better) for 64 Bit Windows 7 ???
Back to top
View user's profile Send private message
HoffiMuc



Joined: 16 Aug 2008
Posts: 7

PostPosted: Wed Oct 13, 2010 11:15 am    Post subject: Reply with quote

Code:
DllCall("shell32\ShellExecuteA"
        , "UInt", 0 ; hwnd
        , "UInt", 0 ; lpOperation
        , "Str", scriptDir . "Test.ahk" ; lpFile
        , "Str", "eins zwei drei" ; lpParameters
        , "Str", A_WorkingDir ; lpDirectory
        , "uint", 0)


how do I pass more than one parameter with some of them with spaces

e.g.
Code:
Run, %scriptDir%Test.ahk "eins" "zwei drei"
with the above DllCall ?

Test.ahk:
Code:
MsgBox, %0%

Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    MsgBox, 4,, Parameter number %A_Index% is %param%.  Continue?
    IfMsgBox, No
        break
}
return
Back to top
View user's profile Send private message
sbc



Joined: 25 Aug 2009
Posts: 321

PostPosted: Wed Oct 13, 2010 11:17 am    Post subject: Reply with quote

HoffiMuc wrote:
is "shell32\ShellExecuteA" also valid (and/or good/better) for 64 Bit Windows 7 ???
I don't know if it is good or better but, it only works with ANSI builds of AHK. If you use the AHK_L Unicode build, ShellExecuteA may not work (in my enviroment 64bit Win7, it doesn't). You may need to use ShellExecute or ShellExecuteW.
Code:
path = yourlink.lnk
arg = arguments
DllCall("shell32\ShellExecute", "uint", 0, "uint", 0
      , "str", path   ; path of file
      , "str", arg  ; arguments
      , "uint", 0, "int", 1)
Back to top
View user's profile Send private message Visit poster's website
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Wed Oct 13, 2010 11:39 am    Post subject: Reply with quote

Of course it is valid.

However, if you have a recent revision of AutoHotkey_L, you can use Run like so:
Code:
Run %A_ScriptDir%\Test.ahk "eins" "zwei drei"

AutoHotkey Basic requires a bit extra:
Code:
Run "%A_AhkPath%" "%A_ScriptDir%\Test.ahk" "eins" "zwei drei"

sbc wrote:
If you use the AHK_L Unicode build, ShellExecuteA may not work (in my enviroment 64bit Win7, it doesn't).
It works if you call it correctly; i.e. pass it ANSI strings, not Unicode.
Back to top
View user's profile Send private message Visit poster's website
sbc



Joined: 25 Aug 2009
Posts: 321

PostPosted: Wed Oct 13, 2010 11:49 pm    Post subject: Reply with quote

Lexikos wrote:
It works if you call it correctly; i.e. pass it ANSI strings, not Unicode.

Oh, I see. Thanks for correcting me. Smile
Back to top
View user's profile Send private message Visit poster's website
Dave-



Joined: 30 Nov 2010
Posts: 7

PostPosted: Sun Sep 04, 2011 5:37 pm    Post subject: Simple solution to this problem Reply with quote

I found the simplest possible answer to the problem of autohotkey having problems running .lnk type files through the run command. Just add an extra set of double quotes before and after it without escaping:

""c:\dir path\filename.lnk"" ;-works
"c:\dir path\filename.lnk" ;-doesn't work
c:\dir path\filename.lnk ;-doesn't work

tested: Windows 7

When I did some experiments with similar stuff, I found this will work with multiple parameters surrounded by double quotes as well, just use an extra pair of double quotes before and after the entire command and do not escape any of the quotes at all.

""c:\directory\program.exe" /switch1 "parameter1" "parameter2""

This is not an autohotkey problem, this is part of the standard cmd.exe shell syntax, autohotkey is doing what it should be doing, just passing it through to the shell to parse and execute. To understand this, read down the following page until you get to the part that says 'The following logic is used to process the quote (") characters'.

http://ss64.com/nt/cmd.html
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Mon Sep 05, 2011 2:36 am    Post subject: Reply with quote

It sounds like you're running the shortcut via cmd.exe rather than directly with Run. Also, the OP's problem only applies to AutoHotkey Basic, not recent versions of AutoHotkey_L.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group