Feature request copy file, and paste path

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

Feature request copy file, and paste path

Post by Fabimawn »

Hello Love2D forum.

With Windows I've ran in a lot of problems when trying to compile my application for distribution, but most of them were fixable. This problem might not be.

I've got a drag and drop system in my application where customers can drop a .bin file to be uploaded to a microcontroller so they can update the device. The application also contains a driver installer to communicate with the microcontroller. The output of the CMD gets read and human readable messages are saved in a log derived from the CMD. The problem is, to install the drivers the application needs admin privileges which I fixed in another post with the help of some amazing people. The thing is, when running apps in admin mode on Windows, drag 'n drop is blocked from explorer.exe to your app.exe since explorer.exe isn't admin by default and isn't allowed to drop the file in your application so love2d's callback for filedropped won't notice.

I've tried a lot of things including only running the driver installer commands as admin which doesn't work since for that I would need to start a new powershell window inside the os.execute with admin privileges meaning I can't read from that window with handle = io.popen.

So the request is to please implement a function to read the path of an actual file you have copied. I know there's the option to read the contents of the clipboard with text, but not from a file. And for users to right click on the binary file, then copy the path, and name, merge it together in notepad and then copy the complete string is a no go. Also the same reason I don't want to add a file input text field.

By the way, this is some test code I got from ChatGPT using ffi, since this stuff is so far above my head, I can't program it myself. It could actually detect a file but it was garbled data:

Code: Select all

local ffi = require("ffi")

                ffi.cdef[[
                    typedef const char* LPCSTR;
                    typedef int BOOL;
                    typedef void* HANDLE;
                    
                    BOOL OpenClipboard(void* hWnd);
                    BOOL CloseClipboard();
                    BOOL IsClipboardFormatAvailable(int format);
                    HANDLE GetClipboardData(int format);
                    void* GlobalLock(HANDLE hMem);
                    void GlobalUnlock(HANDLE hMem);
                    HANDLE GlobalAlloc(int uFlags, size_t dwBytes);
                    size_t GlobalSize(HANDLE hMem);
                    
                    int GetLastError();
                ]]
                
                -- Clipboard format for file data
                local CF_HDROP = 15
                
                local user32 = ffi.load("user32")
                local kernel32 = ffi.load("kernel32")
                
                function getClipboardFilePath()
                    if user32.OpenClipboard(nil) ~= 0 then
                        -- Check if the clipboard contains file paths
                        if user32.IsClipboardFormatAvailable(CF_HDROP) ~= 0 then
                            local hMem = user32.GetClipboardData(CF_HDROP)
                            local pMem = kernel32.GlobalLock(hMem)
                            
                            -- Extract the file path (the first file in the list)
                            local filePath = ffi.string(pMem)
                            
                            kernel32.GlobalUnlock(hMem)
                            user32.CloseClipboard()
                            return filePath
                        end
                        user32.CloseClipboard()
                    end
                    return nil
                end
                
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

Re: Feature request copy file, and paste path

Post by Fabimawn »

I just got the idea to try using a powershell command to get the data which luckilly works for now AND in Admin mode since UAC isn't acting like an idiot.

Code: Select all

function sys.GetClipboardFilePath() -- Function to get the path to a file that's currently in the clipboard
        -- Run PowerShell command to get clipboard file paths
            local command = 'powershell -Command "if (Get-Clipboard -Format FileDropList) { (Get-Clipboard -Format FileDropList)[0].ToString() }"'
            local handle = io.popen(command)
            local output = handle:read("*a")  -- Read the full output from PowerShell
            handle:close()
    
        -- If there's any output (file paths), return the first file path
            if output and #output > 0 then
            -- Trim any extra whitespace
                output = output:match("^%s*(.-)%s*$")
                return output
            end
            
        return nil
    end
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

Re: Feature request copy file, and paste path

Post by Fabimawn »

I just got the idea to try using a powershell command to get the data which luckilly works for now AND in Admin mode since UAC isn't acting like an idiot for copy paste operations.

Code: Select all

function sys.GetClipboardFilePath() -- Function to get the path to a file that's currently in the clipboard
        -- Run PowerShell command to get clipboard file paths
            local command = 'powershell -Command "if (Get-Clipboard -Format FileDropList) { (Get-Clipboard -Format FileDropList)[0].ToString() }"'
            local handle = io.popen(command)
            local output = handle:read("*a")  -- Read the full output from PowerShell
            handle:close()
    
        -- If there's any output (file paths), return the first file path
            if output and #output > 0 then
            -- Trim any extra whitespace
                output = output:match("^%s*(.-)%s*$")
                return output
            end
            
        return nil
    end
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Feature request copy file, and paste path

Post by RNavega »

Hi, see if this is of any use:
https://love2d.org/wiki/love.system.getClipboardText

Edit:
I know there's the option to read the contents of the clipboard with text, but not from a file.
If you've already tried that command then nevermind. But I'm curious if the text has the file paths you're after.

Edit 2: I also wonder if starting the program through drag and drop (dropping into the shortcut/ executable) wouldn't let you get file paths through the app arguments in love.load(arg, unfilteredArg): https://love2d.org/wiki/love.load
That is, if dropping the files onto the executable would run it with a command line with the file paths.
User avatar
Fabimawn
Prole
Posts: 23
Joined: Wed Jan 04, 2023 11:28 am
Location: Lua

Re: Feature request copy file, and paste path

Post by Fabimawn »

Yea the shell command solved the issue. I am not sure if opening the application like that with admin would also work since it seems it happens on all elevated Windows processes. Even saw some guys on Reddit using photoshop elevated, also no drag 'n drop. It's UAC blocking it, but apparently copying and pasting is allowed since that would mean the application handles the paste action itself which is not very logical since you would also need to program in a on file dropped callback at some point.

Also if I understand your second edit correctly, it's an application created for regular computer users so I think them not needing to drag 'n drop into the application and just clicking an icon is more understandable for them.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests