Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Duplicate USB drive applescript/bash/automator

Basically i'm looking for a script that can be run whenever a usb drive is inserted. The script is to copy the contents of the usb (including the hidden files like .Trashes etc) and paste them into a folder on the desktop. It should also be able to accept any name of usb drive, basically i don't want to have to change the script each time a differently named usb is inserted. I've been looking up different scripts trying to understand it but just cannot figure out how to put different programs together to make the kind of script i need. Thanks

MacBook Pro, OS X Yosemite (10.10.1)

Posted on Feb 9, 2015 2:50 PM

Reply
3 replies

Feb 11, 2015 2:30 AM in response to groundog

You can create a folder action for this with Automator. Press the Command ( ⌘)-Spacebar keys to open the Spotlight Search field, type the first couple letters of the app name (Aut), and press the Return key.

User uploaded file

Press the Command ( ⌘)-N keys to open a new document and choose "Folder Action".

User uploaded file


With the "Library" and "Actions" buttons selected, start typing "Copy Finder Items" in the search box until it is isolated in the Action list. Drag it into the Workflow pane.

User uploaded file

Click "Choose folder" in the pop-up menu and click "Other...". Press the Shift-Command ( ⌘)-G keys. Type "/Volumes" and click the "Go" button. Click the "Choose" button. Choose where you want the Finder items to be saved to and whether they will be replacing existing files.

User uploaded file

Press the Command ( ⌘)-S keys to reveal the Save dialog, enter a name for the folder action in the text input field, and click the Save button. The folder action becomes active upon save. Your folder action is saved to /Users/<username>/Library/Workflows/Applications/Folder Actions/.


The folder action you created will copy any volume mounted, not just USB volumes. As a result of this, and the fact that you may not want to copy all USB volumes, you need enable and disable control over the folder action. Press the Option-Command (⌘)-Spacebar keys to open a Finder window. Press the Shift-Command ( ⌘)-G keys. Type "/System/Library/CoreServices/Folder Actions Setup" and click the "Go" button.

User uploaded file

Drag the "Folder Actions Setup" app icon either to the Dock or the Desktop to create an alias for easy access. If you want to place an alias in the Finder sidebar, hold down the Command (⌘) key while dragging it there.


Click the alias at any time to launch the app and open a window. To enable the folder action, select (checked) "Enable Folder Actions", "Volumes", and the workflow script you created. To disable, you can do so globally by deselecting (unchecked) "Enable Folder Actions", or individually by deselecting (unchecked) the workflow script. If you can't see your workflow, highlight "Volumes" first by clicking on it.

User uploaded file

You may also want to create a keyboard shortcut to launch the Folder Actions Setup app. If you navigate to  > System Preferences > Keyboard > Shortcuts > Services, under "Files and Folders" in the selection field to the right, and scroll down, you'll see there is already a service for the app. Unfortunately, if you add a shortcut for the service it will only launch when in Finder. A better solution is to create a service that can launch the app from anywhere.


In Automator, open a new document and choose "Service".

User uploaded file

At the top of the Workflow pane, click the pop-up menu to the right of "Service receives", scroll down to the bottom and click "no input". Ensure that "any application" is selected in the pop-up menu to the right. Drag the "Launch Application" action into the Workflow pane. Click on the pop-up menu within that action, scroll all the way to the bottom, and click "Other". Press the Shift-Command ( ⌘)-G keys. Type "/System/Library/CoreServices/Folder Actions Setup" and click the "Go" button. Click the "Choose" button.

User uploaded file


Press the Command ( ⌘)-S keys to reveal the Save dialog, enter a name for the service in the text input field, and click the Save button. Your service is saved to /Users/<username>/Library/Services/.


Navigate to  > System Preferences > Keyboard > Shortcuts > Services, and under "General" at the bottom of the selection field to the right, click on "add shortcut" to enter a unique key combination. Use your shortcut any time you want to select or desert the folder action you created.

User uploaded file

If you want to access your Automator service from the Menu bar, you can create an alias and place it in your Scripts Folder located at /Users/<username>/Library/Scripts/. Navigate to Script Editor > Preferences > General and select (checked) "Show Script menu in menu bar" if you don't see the Script menu in the Menu bar. You may want to deselect "Show Computer scripts".


The workflow will run any time you choose, copy the contents of any USB, including hidden files, and paste them into a same named folder on the Desktop. Note that some USB drives, with OS install files for instance, won't copy. Be aware that the workflow will copy every volume mounted, so remember to disable the folder action when you don't want to copy.

User uploaded file

Feb 17, 2015 5:08 PM in response to groundog

Also this doesn't seem to copy the .Trashes file?


.Trash folders aren't generally copied as the files in them are considered to be there by intent and not to be retained. The obvious solution is to remove files from the Trash before copying them. If for some reason you don't want to do that, you can use the "Create Archive" action in Automator instead of the "Copy Finder Items" action; .Trash will transfer. Another solution is to write a script.


Be aware that Apple proprietary data stores .Spotlight-V100 and .fseventsd are regenerated when absent and copying them will likely lead to their corruption.


Is there any way that specific files could be searched for to be copied over?


You can use the "Ask for Finder Items" actions for this, although hidden files won't be displayed for selection:

User uploaded file

If you want to select hidden files, you can use a script. In the following script you'll need to set Finder to show hidden files first. Set up a Folder Action as before and use the "Run AppleScript" action as the only action:



(* Read carefully: Finder must be set to show hidden files before this script is run to be able to choose them. .DS_Store cannot be copied, the script will terminate with a message. Hidden files can only be copied to a destination folder if there are no hidden files with the same name. ∴ It is preferable to start with a new folder. Copied source files will overwrite files in the destination folder with the same name. *) on run display dialog ¬ "Click \"Start\" to select and copy files from a USB drive." with icon note ¬ with title "Copy USB Drive" buttons {"Cancel", "Start"} default button "Start" tell application "System Events" to set USBdisks to name of disks whose ejectable is true set the dialogResult to choose from list USBdisks with prompt ¬ "Select a USB drive to copy:" with title "Select USB Drive" if the dialogResult is false then error number -128 else set the sourceUSB to item 1 of the dialogResult as alias end if set the destinationFolder to choose folder with prompt ¬ "Choose a Folder to copy to, or make a New Folder:" default location (path to desktop folder) tell application "Finder" set fileList to items of sourceUSB set fileNames to {} repeat with oneFile in fileList set end of fileNames to name of oneFile end repeat end tell set theSelections to choose from list fileNames with prompt ¬ "Select one or more files to copy. Copied source files will overwrite files in the destination folder with the same name:" with title ¬ "Select Files to Copy" OK button name "OK" cancel button name "Cancel" with multiple selections allowed if the theSelections is false then error number -128 else if (count of theSelections) is greater than or equal to 1 then repeat with oneSelection in theSelections set theFile to (sourceUSB & oneSelection) as text try tell application "Finder" to duplicate theFile to destinationFolder with replacing on error beep display dialog ¬ "Hidden files exist and can't be overwritten. The script will terminate and the USB drive ejected." with icon stop ¬ with title "WARNING!" buttons {"OK"} default button "OK" tell application "Finder" eject sourceUSB display dialog ¬ "The USB drive ejected and is safe to remove." with icon note ¬ with title "USB Drive Ejected" buttons {"OK"} default button "OK" end tell return end try end repeat end if tell application "Finder" display dialog ¬ "The files have been successfully copied!" with icon note ¬ with title "Files Copied" buttons {"OK"} default button "OK" eject sourceUSB display dialog ¬ "The USB drive ejected and is safe to remove." with icon note ¬ with title "USB Drive Ejected" buttons {"OK"} default button "OK" end tell end if end run


If you use the script, when asked to select files from a list, you can select consecutive items by holding down the Shift key and using the arrow keys. You can select non-consecutive items by holding down the Command key and clicking on them.


You can set Finder to show/hide hidden files. Make sure you aren't running any Finder operations, such as copying and moving files that may lead to data loss when Finder restarts.


To show all files, type this command in a Terminal.app shell window and press the Return key:

defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder


To revert back, type this command in a Terminal.app shell window and press the Return key:

defaults write com.apple.finder AppleShowAllFiles FALSE; killall Finder


If you want to use Applescript you can use a script. Every time it is run it will alternate states:

set state to (do shell script "defaults read com.apple.finder AppleShowAllFiles") if state is not equal to "TRUE" then do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder" else do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE; killall Finder" end if


Or use Automator with a "Run Shell Script" action with /bin/bash set for the shell. It will alternate states when run:

STATUS=`defaults read com.apple.finder AppleShowAllFiles` if [ $STATUS == TRUE ]; then defaults write com.apple.finder AppleShowAllFiles FALSE else defaults write com.apple.finder AppleShowAllFiles TRUE fi killall Finder


If you save it as an Automator application, you can run it from anywhere you would run any application: from the Dock, with Spotlight, use a keyboard shortcut, Command (⌘)-drag its icon to the Toolbar of a Finder window, etc.. If using Applescript, you can save your script to your Scripts Folder located at /Users/<username>/Library/Scripts/ and run the script from the Menu bar. You'll need to have "Show Script menu in menu bar" selected in the General pane of Script Editor's Preferences to access the Scripts menu.


Finally, as a reminder, always insure your data by backing up your boot drive before running any new scripts or Terminal commands.

Duplicate USB drive applescript/bash/automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.