How Can I Copy a File to the Clipboard?

I have a series of template files which I use regularly. I would like to be able to copy them to the clipboard when needed.

I would enter the file path in first line.

What would I enter in the second line below ?

Thank you

PS I am not trying to read copy the contents of a file. I am trying to copy the file itself, as if the file was selected in Finder and you pressed cmd-C

What you need is the Read a File action. See the wiki here for more information: action:Read a File [Keyboard Maestro Wiki]

1 Like

thank you. There is a misunderstanding. I am not trying to read copy the contents of a file. I am trying to copy the file itself, as if the file was selected in Finder and you pressed cmd-C

Like this?

2 Likes

Thank you @kcwhat . I had seen it but have no clue how to adapt it to a specific file with a known path. Not a file I select.

If not that, the line you are pointing to is the destination. After you copy the file, what are you doing with it? Where do you want it to go?

1 Like

@kcwhat @tiffle

I found the solution but editing the AppleScript in the macro suggested by @kcwhat
It works perfectly
thank you again

tell application "Keyboard Maestro Engine"
set filePathToCopy to "/Users/ronald/card health insurance exp 31 march 2022.pdf"
end tell
set the clipboard to POSIX file filePathToCopy

This post was revised to make the AppleScript easier to read using @ccstone's excellent macro

thank you @ccstone !

3 Likes

Indeed. Glad you found a solution. Please mark the solution so others may find it if they have the same question.

KC

2 Likes

Only this portion is necessary:

set filePathToCopy to "/Users/ronald/Downloads DUMP TEMP/dentist apts.pdf"
set the clipboard to POSIX file filePathToCopy
1 Like

For the record, KM already has an action specifically for this use-case, no AppleScript required: action:Set Clipboard to File Reference [Keyboard Maestro Wiki]

Example Macro.kmmacros (1.2 KB)

4 Likes

Thank you @gglick I tested it and works perfectly.

Interesting tidbit about both methods:

If I use either the AppleScript above or your method using set system clipboard to file and then open my clipboard history to drag and drop the corresponding clip into Finder, Pathfinder or an app (DevonThink, Scrivener)

  • If I use my copy paste app called Copy'em → drag and drop the clip → I end up inserting the actual file with both methods.

  • If I use KM's clipboard history → drag and drop the clip → I end up inserting a text file containing the file PATH (not the file) with both methods.

1 Like

This is dependent upon several factors. In particular:

  • What data-types Keyboard Maestro has placed on the system clipboard.
  • What data-type the receiving app expects in the given context.

For me – dragging and dropping a file from the Keyboard Maestro clipboard history to the Finder creates a text-clipping containing the file path.

BUT – pasting the same history item does in fact paste the file.

2 Likes

Yes you are right. Thank you. My problem was that pasting a file works in Finder Pathfinder and DevonThink, but not in Scrivener. I needed a file drag and drop option.

1 Like