How Do I Copy a File Added to a Folder?

This post is so old - but I can't get this to work now? Same kind of idea except I want a new file added to a folder to be added to the clipboard, so I can paste it into Mail or Skype or Slack or wherever else. Any tips would be great - sorry for what must be a very simple question - I have lots of macros for things but just can't figure this one out!

This is really a new question, so I moved your post to a new Topic, where it will get better attention.

But your request is not clear.

Which do you want?

  1. When a file is added to a folder by another action, copy it to the clipboard.
    OR
  2. Create and Add a file to a folder, then copy it

OR something else.

Please clarify.

Apologies - I’m after a folder action that watches a folder for a new file, in my case it would be a mp3 file. I want this file copied to the clipboard so I can paste it into various apps. I have a hazel workflow that scans certain folders and then moves mp3 files to a ‘current mp3’ folder. I am trying to get this file that hazel copies onto the clipboard. Does that help at all?

You can use the Hazel option after moving the file to Show In Finder. Next, have Hazel run an AppleScript to copy the file to the clipboard (note, someone else probably has a better way to use AppleScript than this, but this code will work).

tell application "System Events"
	tell process "Finder"
		keystroke "c" using command down
	end tell
end tell

You don't need Hazel.
The %TriggerValue% token of your macro will contain the path of the file just added.
So, then use File Actions action to Copy the File.

Hi - Thanks for this - however I want to copy the file to the clipboard - I think this only lets you copy the file to a location?

No. %TriggerValue% provides you with the PATH of the file just added to the folder.
You can use that token with the "Copy File" Action to then copy wherever you like.

I understand that - but my workflow I'm looking for is to have : When Hazel scans my folders and add's a MP3 file into my 'Current MP3' folder, that file is then added the the system clipboard, so I can start an email and then paste it in, or paste it into a skype chat, or paste it into slack. I think your method doesn't copy it to the system clipboard unless I'm missing how to do that in the 'Copy File' option in KM?
Or am I thinking about this the wrong way - should I use what you're saying to add that file to a Named Clipboard and then find a way of pasting it into an email?

EDIT : I'm just looking to hit Cntrl - V in whatever app I'm using to copy this 'newest' file that has gone into my directory, if that helps explain it. I have it going to Yoink, which is great, but just trying to avoid mousing around even further. Thanks so much!

Hint: Try adding

https://wiki.keyboardmaestro.com/action/Set_Clipboard_to_File_Reference

as you want the system clipboard to contain your file.

1 Like

The action @jonathonl suggested, in conjunction with the Folder trigger and the %TriggerValue% token, makes it very easy to set the clipboard, system or named, to whatever file is newly added to the specified directory:

Set Clipboard to Newly Added File.kmmacros (1.5 KB)

And if I understand your addendum correctly, if you want to be able to paste the most recently added file to a specified folder arbitrarily, you could do something like this, which will set the file to the clipboard and paste it in one swoop:

Paste Most Recently Added File.kmmacros (2.3 KB)

Obviously, you'll need to change the folder paths to match yours, but aside from that, I think these should do what you want.

1 Like

Wonderful! This has done it thanks so much!

Pete.

1 Like

I apologize if this question sounds ignorant, but I am a new user and don't yet fully understand the programming language of KM.

My goal is to create a MACRO that looks at SOURCE FOLDER for any new files added to that folder (including any sub-folders .i.e SOURCE FOLDER/SUBFOLDER/newfile) and upon the placement of that file a copy gets place in another folder /DESTINATIONFOLDER.

I am confused about the use of %TRIGGERVALUE% in the example above. Does this MACRO 'Copy to clipboard the file? Why is a COPY function not used? Then what is the purpose of the BREAK LOOP function in the example above? I don't want to trigger on a key but AUTOMATICALLY when I place a file in the SOURCE FOLDER. Then the PASTE function doesn't seem to designate a path or is that the PATH indicated in the SET SYSTEM CLIPBOARD to PATH. Also, is the FOR EACH ITEM IN THE COLLECTION EXECUTE needed for what I am trying to accomplish. If someone has a working model and can paste a screenshot. Also, not sure how or where to download macros but would be delighted to try that if you upload one.

Many thanks!!!

Raymond

I don't have time now to answer all your questions about what each action and token does, but I can show you a simple example of a macro that automatically copies any file added to the Downloads folder to the Desktop folder:

Example Macro.kmmacros (1.7 KB)

All you should need to do is change the source folder path in the trigger settings and the destination folder path in the Copy action as appropriate. It won't work on files added to subfolders, but you could easily make more macros like this for each subfolder you want files to be automatically copied from.

THANK YOU VERY MUCH. I will try this and confirm my results.

Hey Raymond,

Because the original poster (OP) wanted this:

(Read Post #1)

To add only the last item added to the directory – note the sort-by-date-added.

You can't – at least not directly. Keyboard Maestro does not see into the subdirectory structure.

You'd have to keep an index of the structure and do a diff on item added. That gets complicated but is doable. You might be able to get away with keeping track of the date-added and scan for newer items.

Your task is doable but not easy.

-Chris