How to pass multiple associated files to a program? - windows

I have written a Windows software and I have associated a file extension with this program. When I double click a data file then my program starts up and opens the file. So far it works. But when I select multiple files and then click "Open" in the context menu then multiple instances of my program are started, one instance for one file. I want Windows to open ALL files with a single instance of my program. Is this possible without implementing a one-instance-handler in my program?
Currently the MyFileType/shell/open/command in the registry looks like this:
"C:\Program Files\MyApp\MyApp.exe" "%1"
Maybe there is some special Token/Variable I have to use instead of the "%1" to get it working? On Linux I have to use %f for a single file and %F for a list of files. Is there something similar on Windows?

The simplest way is to associate your app with the default shell verb (e.g. “open,” or “play,”) for the file type, and implements a drop target that uses SHCreateShellItemArrayFromDataObject to get the selected files. Suggested reading: How the Shell Invokes Verbs
If you don't like to take over the default verb, you can add your verb to the file association's open with list.
Another method is to make your app a singleton and send the document's path to the first instance via inter-process communication methods such as DDE or RPC. This requires you to keep your main window responsive (for example, won't work if you are showing a dialog)

%* might be what you're looking for.

Related

How to pass several files at once to my registered application?

In a Delphi 10.4.2 win-32 VCL Application associated with graphic file formats in Windows 10, I get parameter files selected in Windows File Explorer. Here I watch the files passed to my app right after Application.Initialize;:
CodeSite.Send('AppInstances: ParamCount', ParamCount);
When I select ONE file in Explorer and then press the ENTER key, this file gets loaded in my application. So far so good.
But when I select THREE files in Explorer and then press the ENTER key, my app gets started THREE times each time with ONE of the 3 file parameters:
Is it possible that Explorer would start my app only ONCE and pass the 3 selected files at once to my app when I press the Enter key?
On Windows 7 and later 1, you can register a MultiSelectModel value (either Document or Player) with your file association verb(s) in the Registry. This is the easiest way to allow Explorer to send multiple files at a time to a single instance of your app, such as in separate command-line parameters.
1: I don't know exactly when this feature was first introduced.
Prior to MultiSelectModel, other ways to handle this included:
implementing the IDropTarget interface in your app, and then registering the DropTarget with your file association verb(s). The Shell can then construct an IDataObject containing information about the files(s), and pass it to your IDropTarget implementation (also see this article). This is the preferred method, as it does not suffer from limitations that other methods have (including MultiSelectModel!), and it allows for more flexibility as the same IDropTarget implementation can accept multiple files executed in Explorer, files dropped onto your app's window, even dropped onto the app's .EXE file itself. It is just a matter of registering the same IDropTarget with the appropriate APIs.
Implementing a DDE server in your app, and then registering the server with your file association verb(s). The Shell can then start a DDE conversation with your app and send the file paths over to it using your specified command(s).
just accepting the Shell starting a separate process for each file. Before your app creates its UI, have its startup code check for a file path on its command-line, and if found then look for another instance of your app already running, and if found then use an Inter-Process Communication mechanism of your choosing (ie, WM_COPYDATA, named pipe, socket, mailslot, etc) to send the file path to that existing instance, and then exit.

Can a Windows batch file determine its own window name?

When a Windows batch file is launched via a shortcut (from the Start Menu, for example), its window will typically receive the title of the shortcut.
Is it possible for the batch file to determine the name of its own window?
This question is related to Can a Windows batch file determine its own file name?, but that question asks if the batch file can determine its own filename. I am asking if a batch file can determine its own window name.
StackOverflow user QcFe lent a kind hand by finding this QA on Superuser: https://superuser.com/questions/1155853/how-to-get-the-window-title-text-from-batch-file
The short answer is that Windows provides no simple and direct way for batch files to accomplish this task.
Windows provides the internal title command to set the window title, but no way to retrieve it. (Which is disappointing, because it would be such a simple addition to the title command.)
The functionality can be attained using PowerShell or possibly tasklist, but for the latter, there are issues that need to be addressed if there are more than one command shell window open.
I elected to force the window to have a unique name using the Title command. That way I could reference it correctly for tools that need to know the window title, such as NirSoft's excellent nircmd utility.

Programmatically building applications for "Open with..." on Mac

I'd like to create a program which uses certain information given by my program's users and bakes this info into executables which can be used with Mac's "Open with...".
Since bash scripts cannot be used directly for Mac's "Open with..." functionality1, however, I'd like to find a way to wrap such a script (and as painlessly as possible).
I am aware Automator lets one build applications which include bash scripts and which work with "Open with...", but I want to be able to programmatically build both the script and the containing application (and I haven't found that Automator can accept command line arguments to compose applications).
What are some simple, lowest common denominator/open format approaches (without using Python, etc.) whereby I can do this (ideally in a way that could work on Linux as well)?
I found I was able to build an AppleScript application programmatically using osacompile (or apparently also possibly using Apple's JavaScript) using on open listener to listen for "Open with...", default file association, or dragged files (including when placed on the dock) and an on run listener to work with command line invocation. I could then use do shell script internally to invoke my desired file (in my case, calling the Node.js binary with a Node script and some arguments).
I was also able to do try/on error check for an input and if none were present do a choose file with prompt (specifically choose file with prompt "Prompt message" of type {"js"} for JS files) so that the application could be double-clicked without a file argument but yet trigger a file dialog (e.g., for use on the dock).

FTYPE/ASSOC priority and adding to OpenWithList from the command line

(Not sure if this belongs on superusers, but it seems there is a cmd.exe tag here, so here goes...)
As background, I'm working on a Firefox add-on (This question does not require knowledge of Firefox, btw, as Firefox add-ons can call the command line.) The add-on aims to build different kinds of shortcuts to cmd.exe (especially for the sake of my project https://github.com/brettz9/webappfind which allows files to be opened directly from the desktop into web apps).
Anyways, I'd like to give users the option to associate these shortcuts:
As the default handler for specific file extensions or file types.
To show up within the Open With list of applications (even if the user opts not to make the apps as default handlers)
As far as the default handling, I have found the ftype and assoc (and associate) commands, but I have read that user selections will override their behavior. Is there some way to ensure that I can get priority from the command line in associating file extensions to types and specific executables (until the user changes it again), or if it is not possible, then at least through C++ or the like?
As far as the Open With list:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\<file extension>\OpenWithList
...in my testing (with an exe), this command:
reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.svg\OpenWithList /v d /d D:\wamp\www\webappfind\cplusplus\WebAppFinder-view-mode-Firefox.exe
...did cause the exe file to show up in:
reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.svg\OpenWithList
...but it did not show up when I subsequently right-clicked a file with the ".svg" extension.
I would really appreciate any help with these two points.
REGEDIT4
[HKEY_CURRENT_USER\Software\Classes\Applications\MYFOO.exe\shell\open\command]
#="\"C:\\MYFOO.exe\" \"%1\""
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.myfoo]
"Application"="MYFOO.EXE"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.myfoo\OpenWithList]
"a"="MYFOO.EXE"
"MRUList"="a"
So I got to an investigation what makes those file associations. It appears that you have to create a mapping from the bare EXE name to the full path as shown in the first two long-ish lines. Then you must only use the EXE name in the .extension branch. Setting the .extension's Application value will give you your default app instantly. Remember, only use APP.EXE, its full path must be defined as above. This was your main error. The "%1" part allows you to customize the parameters of your program so that it doesn't have to be just the opened document in quotes, as shown here. The backslashes are just escape characters for Regedit, you may discard them as you see fit.
The OpenWithList is tricky in the sense that there are letters for entries and just a blind write may overwrite some of the user's favorite apps. One approach would be to call your item "z" to lower the probability of overwriting. The right way would be enumerating the key and giving your app the first free letter. The MRUList is not essential, although it should have each used letter once and yours bumped to the start.
Note about user friendliness: Explorer will cache these values until next reboot. Make sure you update the registry and place exe first and create your file later. Although the caching only fully influences the display of the file and when it is run, the registry is read again and it will execute as you want.
TIP: If you decide to use Regedit instead of reg, the /s parameter skips the confirmation message and applies the values right away. Make sure you use double backslashes in the full path as shown. When preparing your temporary .reg file, make sure you append two CRLF's to the end or a glitch may cause your last line of code to be ignored. This sample starts with REGEDIT4 which signifies an ANSI file. If you need support for Unicode in your app path, you'll have to start the file with Windows Registry Editor Version 5.00 and store it in UTF16. This is already a superior solution to calling reg because there's no way you could get CMD.EXE to process special UTF stuff through the command line without mangling.

Need to write a shell extension

I need to write a shell extension for a small context menu.
unfortunately i reused the code available in open source which uses .net 4.0
now the problem is the requirement is I shud not use .NET 4.0.
Instead is there a way where in I can make an entry in the registry so that the shell extension pops out
My requirement is ....
1. The shell extension context menu should display only on .txt, .csv, .xls files.
2. Upon click of shell extension I need to start a different process, by passing the full name of the file on which we click as parameter to the process.
If you just want to add items to the context menu for certain file types, and in response launch an external application, you don't need to write a shell extension. Registry entries are enough to express this.
As the http://www.jfitz.com/tips/rclick_custom.html article you linked to says, for each file type you want to act on, you need to add a new registry key under the Shell subkey, then create a subkey called command, whose value is the name of the application to launch.
To pass the name of the file you clicked on to this external application, add "%1" to the command. Include the quotes, so that if the file's name contains spaces, it will still be treated as one token by the receiving program.

Resources