How to add my Compose Multiplatform application to SendTo menu in Windows?

270 Views Asked by At

I have a cross-platform application created using Compose Multiplatform. I need to add it to SendTo menu (this menu is available in Windows Explorer when user right-clicked a file).

I've tried to find any options to implement it using Gradle. But there is no special options in nativeDistributions/windows.

I've also tried to implement it on application launch in main() function with the code below:

val appPath = Paths.get("C:/Program Files/MyApp/MyApp.exe")
val sendToDir = Paths.get(System.getenv("APPDATA"), "Microsoft", "Windows", "SendTo")
val shortcutPath = sendToDir.resolve("MyApp.lnk")
Files.createLink(shortcutPath, appPath)

It doesn't work and throws an exception:

java.nio.file.AccessDeniedException: C:\Users\Me\AppData\Roaming\Microsoft\Windows\SendTo\MyApp.lnk -> C:\Program Files\MyApp\MyApp.exe
    at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
    at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
    at java.base/sun.nio.fs.WindowsFileSystemProvider.createLink(WindowsFileSystemProvider.java:622)
    at java.base/java.nio.file.Files.createLink(Files.java:1112)

How to create shortcut in Windows SendTo directory using Compose Multiplatform?

1

There are 1 best solutions below

6
On

The Windows folder with sendTo links is not administrator-permission protected:

%APPDATA%\Microsoft\Windows\SendTo

Just create a link there to the application you want to run.

EDIT 1

Try creating a .lnk file. It has a binary format, so a java lib like mslinks should help you accomplishing this task programatically:

https://github.com/DmitriiShamrikov/mslinks

The documentation says Files.createLink creates hard links, which can be a problem since Program Files is a folder protected with admin privileges.

EDIT2

Write a .vbs file with these lines and call the shell to run if from your app. It will create the .lnk file using native Windows components (VBScript):

Set WshShell = CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut(WshShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Windows\SendTo\MyApp.lnk")
oShellLink.TargetPath = "C:\Program Files\MyApp\MyApp.exe"
oShellLink.Save

You can delete the .vbs file after the script is finished.