Search code examples
applescriptautomator

Searching all external drives in Automator


I am trying to make an automator action where all external drives are searched for .txt files, which are then copied to a folder on the internal Macintosh HD drive.

The problem I'm having is that I can't find a way to do this without having to specify each external drive's name each time, but that's a new one every time and not predictable. Is there any way for "indirectly" specifying the path to the external drives (I'm imagining something like a "pointer" to the USB-Ports)?

I know about the hidden /Volumes folder, but then this would search all of Macintosh HD for .txt files as well, which is something I don't want...

Thanks in advance!


Solution

  • I know you mentioned AppleScript, however, this can be done so much easier and straight forward in Terminal.

    You do not need to worry about the Macintosh HD in /Volumes as it's a symbolic link and find without the appropriate option is not going to traverse the symbolic link.

    To use the following example shell script code in an Automator workflow, simple add a Run Shell Script action, adding the following line of code while changing '/path/to/destination' to the directory the files are to be copied to.

    find /Volumes -type f -iname '*.txt' -print0 | xargs -I '{}' -0 cp -a '{}' '/path/to/destination'
    

    This will find all the .txt files on external disks and copy them to the destination while overwriting existing files in the destination.

    That's it, all done just a single action in the workflow is all that's needed.

    Also, if you run this with no external disks attached, not to worry as it will essentially do nothing.


    Notes:

    If for some reason you need to use a Run AppleScript action, you can still use the example shell script code using the do shell script command of AppleScript, e.g.:

    do shell script "find /Volumes -type f -iname '*.txt' -print0 | xargs -I '{}' -0 cp -a '{}' '/path/to/destination'"