0

I get into this situation quite often but haven't found a solution. I have a group of CSS files in the clipboard that I need to paste + replace into several different destination folders. My folder structure is similar to

.
├── folderA_11
|   └── CSS
|        └── file1.css
|        └── file2.css
|        └── file3.css
├── folderB_11
|   └── CSS
|        └── file1.css
|        └── file2.css
|        └── file3.css
├── folderC_11
|   └── CSS
|        └── file1.css
|        └── file2.css
|        └── file3.css
├── folderD_22
|   └── CSS
|        └── file1.css
|        └── file2.css
|        └── file3.css
├── folderE_33
|   └── CSS
|        └── file1.css
|        └── file2.css
|        └── file3.css
└── folderF_44
    └── CSS
         └── file1.css
         └── file2.css
         └── file3.css
etc...

There's a pattern: I want to paste to all CSS folders whose container ends in _11 but not any of the others.

The content of the destination folders doesn't matter, I just need to replace them with the new files. Is there a simple way to paste these files into multiple destinations at once? It could also be an Automator flow or Terminal command.

  • You can get the file from clipboard via "Get Content of Clipboard" and then "Get Selected Finder Items". You can then use Copy Finder Items. Not yet sure how to handle the folders, but maybe this helps you for the start. – Kevin Grabher Oct 31 '18 at 16:43
  • @KevinGrabher Copying the files is not the problem, I already have them on the clipboard - the origin for all the files is the same. My problem is specifically the multiple destinations, I'm stuck opening multiple folders individually for now... – Luciano Oct 31 '18 at 16:46
  • I'm looking to paste a group of files into many different folders, but worst case scenario I can copy the files individually (it's just a few files). The main problem is pasting into many different folders spread across my folder structure. – Luciano Nov 1 '18 at 14:11
  • @CJK updated the file structure, I hope it's more clear now. – Luciano Nov 1 '18 at 14:43
  • I also opened another question specifically about the the folders I want to select apple.stackexchange.com/q/341354/307867 – Luciano Nov 1 '18 at 14:47
2

1 - Open Automator and create a new Service:

enter image description here


2 - Set the Service to receive Files and Folder in Finder:

enter image description here


3 - Drag an "Ask for Finder Items" action:

enter image description here


4 - Drag a "Run AppleScript" action:

enter image description here


5 - Paste this code:


on run {input}
    set numItems to count input
    set theFile to item (numItems) of input as text
    set numItems to numItems - 1
    repeat while numItems is not 0
        tell application "Finder"
            duplicate file theFile to folder (item (numItems) of input as text) with replacing
            set numItems to numItems - 1
            delay 0.1
        end tell
    end repeat
    display dialog "File copied to all folders" buttons {"Thanks"} giving up after 5
end run


6 - Save your workflow and name it as you want:

enter image description here


7 - USAGE: Select as many folders as you want, right click, services, find your workflow:

enter image description here

Select the file you want to copy (beware the window can be hidden behind the main one):

enter image description here

Click "Choose" and wait for the copy.

enter image description here

PS: the file will replace existing ones with the same name.

  • 1
    I think the OP has the file(s) already on the clipboard, so the Ask For Finder Items action is probably not needed. Just to nudge your scripting brain, have a second look at your AppleScript and think about how you could improve it. Consider whether all the lines are really necessary, and whether you could re-word some of the code to be more streamlined. Also consider what the reason was behind choosing System Events to display the dialog, and what effect, if any, that has on the dialog box. – CJK Nov 1 '18 at 0:13
  • @CJK Thanks for your feedback. I believe I'm provideing a better solution, because AFAIK you can't input 2 informations at once for Automator. The workflow must know WHICH FILE and WHERE TO. The other option would be start with the file and ask for several folders, a worst approach in my opinion. Anyway other ideas are welcome. I'm removing the "system events" line. – Mateus Ribeiro Nov 1 '18 at 1:01
  • @CJK I've tested the idea of using the clipboard to retrieve the file but apparently it will only return the name (and not the full path), generating an error in the workflow. The Ask for Finder Items action will provide the complete path. – Mateus Ribeiro Nov 1 '18 at 1:27
  • 1
    Sure about that ? Know of any way to retrieve a file off the clipboard as an actual file object ? Also, there are about three more lines I could trim off your AppleScript. By the way, I think your workflow, overall, is good, and a pretty neat idea. I'm just getting you thinking a bit more. 🤔 Still, I'm going to +1 you. – CJK Nov 1 '18 at 1:30
  • Here's a hint: go into Finder and copy a file onto the clipboard by pressing Cmd-C. Then enter this command in Script Editor and see what you get back: get the clipboard as «class furl» – CJK Nov 1 '18 at 1:34
1

As requested, here's a workable solution as we've discussed in the comments thread. Currently, the situation is that you have multiple files on the clipboard that need to be pasted into multiple destination folders. These destination folders are all called "CSS" and reside inside a set of parents folders (names ending "_11") that you are able to select en masse.

After doing that, you can run the following Automator service, either from the right-click context menu and selecting it under Services (or the Mojave equivalent); or by assigning it a keyboard shortcut in System Preferences, and triggering the service with the press of a hotkey.

The Automator service in question looks like this:

Automator service in macOS

The AppleScript code used in the Run AppleScript action is below. The AppleScript reads the list of files contained on the clipboard; isolates the "CSS" subfolders in each of the selected parent folders that were passed into the service (the "CSS" folder is created if it doesn't already exist); and then duplicates the files from the clipboard into the "CSS" directories.

If the clipboard does not contain any files, you'll hear a beep and no further action is taken.

use framework "AppKit"

property this : a reference to current application
property NSPasteboard : a reference to NSPasteboard of this
property NSURL : a reference to NSURL of this


on run {input, parameters}
    set pb to NSPasteboard's generalPasteboard()
    set fs to (pb's readObjectsForClasses:[NSURL] options:[]) as list

    using terms from scripting additions
        -- No files on clipboard, nothing to do
        if fs = {} then return beep
    end using terms from

    tell application "Finder" to repeat with f in the input
        set css to (a reference to the folder named "CSS" in folder f)
        if not (exists css) then make new folder at f with properties ¬
            {name:"CSS"}

        duplicate fs to css with replacing
    end repeat
end run
  • Nice! The AppKit works with vanilla AppleScript? It doesn't require aditional software? – Mateus Ribeiro Nov 2 '18 at 15:33
  • It does indeed, as long as you’re using macOS 10.10 or above, @MateusRibeiro – CJK Nov 3 '18 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .