AutoHotkey Community

It is currently May 27th, 2012, 6:48 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: October 26th, 2009, 9:42 pm 
Offline

Joined: April 9th, 2008, 8:46 am
Posts: 35
Location: Espoo, Finland
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 6:24 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
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.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 2:12 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 27th, 2009, 2:58 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
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.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: 64 Bit Windows 7
PostPosted: October 13th, 2010, 11:54 am 
Offline

Joined: August 16th, 2008, 6:05 pm
Posts: 7
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 ???


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 13th, 2010, 12:15 pm 
Offline

Joined: August 16th, 2008, 6:05 pm
Posts: 7
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 13th, 2010, 12:17 pm 
Offline

Joined: August 25th, 2009, 4:52 pm
Posts: 321
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 13th, 2010, 12:39 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 14th, 2010, 12:49 am 
Offline

Joined: August 25th, 2009, 4:52 pm
Posts: 321
Lexikos wrote:
It works if you call it correctly; i.e. pass it ANSI strings, not Unicode.

Oh, I see. Thanks for correcting me. :)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 4th, 2011, 6:37 pm 
Offline

Joined: November 30th, 2010, 6:33 pm
Posts: 12
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 5th, 2011, 3:36 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: batto, sjc1000 and 63 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group