Simulate mouse drop file

Hey,

I am trying to create a macro that can drop a file on a website file input field, based on a predetermined file path.

The use case is inserting PDF payment receipts in my accounting web platform from a local folder using keyboard shortcuts.

The solution I am trying to avoid: Opening Finder, start dragging the file, making the browser window active, moving the mouse to the input field, and dropping the file.

I am thinking it should be possible to set the file path of a file drag & drop event programmatically similar to how you can "Set Clipboard to File Reference" (it is not a standard file input field, which would allow pasting, it is a custom HTML element that allows files to be dropped on to it). That would be faster and less sensitive to UI delays.

Hope this makes and that one of you experts can help me out :blush:

If you don't want the Finder to show the file, then no, you can't do that. You can't drag and drop something with a mouse if the mouse can't access it (you can't see it).

When you click the browser field, does it present you with a file selection dialog?

If not, here's an idea, assuming you can use Find Image to get the browser field location. (If you can't we could adjust this, for example by using the current mouse location instead.)

Drag and Drop from Finder.kmmacros (41 KB)

Macro screenshot

Setup:

Orange action 1: Adjust this if you're not using Chrome.
Orange action 2: Ditto.

Green action 1: Put an image of the browser field here.

Purple action: Put the predetermined file path here.

Green action 2: This needs an image of the edge of a selected file line in a Finder window. I use column view in dark mode, and our screen resolutions may differ, so you might need to replace this.

Pasted_Image_06_05_2022__15_51

Orange action 3: Adjust this if you're not using Chrome.
Orange action 4: Ditto.

Blue group: This is optional, as you may prefer to leave the Finder window open in the background.

In use:

When triggered, this macro will show the file in a Finder window, then drag it to the field in your browser and (optionally) close the Finder window.

1 Like

Wow, thank you so much for the very detailed suggestion :pray:

I the mean time I have been working on an approach where I trigger the file open/save dialog in the browser using js:
document.activeElement.parentNode.parentNode.parentNode.getElementsByClassName("ledger-table-item-line--cell__file--label")[0].click()

And then I use the / keyboard command to insert the file path, and I use CMD + F to search for files that could match the accounting entry.

Your idea with using "Reveal File in Finder" is brilliant :ok_hand:

1 Like

This would have been my first suggestion. If that works, I'd go with it!

1 Like

I would prefer to avoid it since I have to add so many UI-dependent conditionals, such as "wait for this UI element to appear," which makes the automation sensitive to UI changes or delays.

I am used to coding where it is less fragile :innocent:

I assumed you'd run it when you already had the page open in front of you, but if not, could you use one of these?