Passing received filename to a launcher executable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Passing received filename to a launcher executable

Post by Renfro » 05 Aug 2022, 07:04

I have a few portable applications that use launcher executables to call their respective main program executable. These have to be run from the launcher executable in order to function correctly.

For one of these applications I have added the launcher executable to my 'Send To' menu, so that I can right-click on a file in explorer and send that file to the application in question to be opened.

While this approach works for other applications, in this particular case the launcher executable needs a command line argument appended to the launcher in order to for it to work correctly.

I could just make a shortcut to the launcher with the arguments added to the 'Target' field of the shortcut (which does work) but then I can no longer use the 'Send To' menu to open files directly in the application (because you can't 'send to' a shortcut .lnk file).

I therefore thought I would replace the .lnk shortcut with a simple one-line AHK script that does the same thing as the 'Target' field of the shortcut (i.e. run the launcher executable, but with arguments applied).

Run, LosslessCut.exe --disable-gpu

The idea was that due to this simple AHK script being compiled into an executable, it could still be used with the Windows 'Send To' menu.

However, the problem is that the filename of the currently selected item is not being passed by my AHK executable to the real launcher. The application does open correctly (with correct arguments applied) but no file is opened in the application.

What line do I have to add to my compiled AHK script to get it to forward the file name that it has been sent? (or that it has had dropped on it etc.)

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Passing received filename to a launcher executable

Post by BoBo » 05 Aug 2022, 10:12

JFTR: viewtopic.php?t=18441

I've created this script.ahk...

Code: Select all

MsgBox % A_Args[1]
...and a shortcut/*.lnk-file with this target line...
autohotkey.exe script.ahk - https://www.autohotkey.com/docs/Scripts.htm#cmd

...that is hosted here.
C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\SendTo

So, once I've chosen a file and selected within the SendTo submenu that new shortcut item, it's showing the filename within a MsgBox.
Correct, instead of showing the MsgBox you could also execute that file using a launcher.

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Passing received filename to a launcher executable

Post by Renfro » 05 Aug 2022, 14:50

Thank you very much for your reply @BoBo

When I looked through your reply I realized that I screwed up big-time when writing my original post. I meant to say "Open WIth" context menu, not "Send To".

I don't even know why I typed Send To (I must've been on auto-pilot and thinking of the wrong context menu). The Send To menu is in fact nothing but shortcut links, so putting a shortcut there would not be an issue. But I'm trying to use the Open With menu (which only works with executables) hence why I made a compiled AHK script .exe.

Therefore the solution that you suggested will not work. My apologies for the incorrect information.

Corrected question:
How can I pass the filename(s) sent from the 'Open With' menu via my compiled ahk script to the real launcher executable (i.e. LosslessCut.exe).

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Passing received filename to a launcher executable

Post by lexikos » 05 Aug 2022, 23:15

Just use A_Args as BoBo demonstrated (well, not exactly as demonstrated; you'll probably want to specify some other program, and enclose the arg in literal quote marks in case it contains spaces). Command line usage depends on the application, not on whether it is being invoked by Open With or Send To. If the application isn't registered, the shell defaults to simply passing the filename as a command-line parameter.

The AutoHotkey v2 installer registers an uncompiled script as an Open With entry, via OpenWithProgIds.
Last edited by lexikos on 05 Aug 2022, 23:33, edited 1 time in total.
Reason: well...

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Passing received filename to a launcher executable

Post by BoBo » 06 Aug 2022, 03:44

@Renfro - a few days ago I've scripted this: viewtopic.php?f=10&t=104233 ...that acts as a registry unrelated 'OpenWith' workaround.

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Passing received filename to a launcher executable

Post by Renfro » 06 Aug 2022, 12:15

Thank you Lexicos & BoBo.

I'm getting closer now, but I can't figure out the correct syntax for A_Args usage.

I first had to find out what, if any, arguments the program needed in order to have filenames passed to it (it turns out that you just use the argument: open). So if I compile the following ahk test script, the application launches (with --disable-gpu switch applied) and then it opens the specified media file "Test File.flac".

Code: Select all

Run, LosslessCut.exe --disable-gpu open "D:\Media\Test File.flac"
So far, so good. So now that I know that it can successfully open files, all I have to do is to replace the specific file name with A_Args so that it can open any file.

However, no matter what I try it doesn't work.

In BoBo's script he used: MsgBox % A_Args[1] (which worked fine when I compiled it and dropped files onto it). But it won't compile if I use:

Code: Select all

Run, LosslessCut.exe --disable-gpu open % A_Args[1]
The compiler complains about the percent sign. If I remove the percent sign then it compiles, but the program just opens empty (no file is loaded when dropped on the compiled .exe). I'm presuming this is because without the percent sign the literal text "A_Args[1]" is being sent instead of the path that it represents.

I've tried all sorts of variations with quote marks in different places, but nothing works.

What is the correct syntax for the code line shown above?

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Passing received filename to a launcher executable

Post by lexikos » 07 Aug 2022, 22:46

Each command parameter can take either an expression or text with %variables%, not both. Using a percent sign to mark an expression is only possible at the start of the parameter (immediately after Run in this case).

See Expressions vs Legacy Syntax.

For Run in particular, because you often have to use literal quote marks, it may be easier to assign the value to a variable, and then use %variable%. In this case, you can use the legacy command-line variable %1% directly with Run (not in an expression) instead of A_Args[1].

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Passing received filename to a launcher executable  Topic is solved

Post by BoBo » 07 Aug 2022, 22:57

Run % "LosslessCut.exe --disable-gpu open " A_Args[1] ;expression
Run, LosslessCut.exe --disable-gpu open %1% ;legacy

Renfro
Posts: 64
Joined: 15 Oct 2020, 10:19

Re: Passing received filename to a launcher executable

Post by Renfro » 08 Aug 2022, 13:13

Thank you both very much for the further clarification and specific examples.

I have now finally got it working!

Post Reply

Return to “Ask for Help (v1)”