Audo detect SD Card or USB and take an action
[ 5th October 2013, 15:28:26 ]This is for a friend who asked if it was possible to auto detect a usb jumb drive / flash drive or SD Card, and the answer is yes! This is a really rough solution, but can be the basis of what you want. Note this is for Apple OS X, and is written in AppleScript.
You need to create a directory to store the files/code, and you will have files;
- auto-sd-card.plist
- AutoFlash2.scpt
- MacScript.scpt
- AutoFlashLog.txt
You can download the who package to auto load from an sd card here.
Here is the auto-sd-card.plist
This is the configuration file for launchd, which will get the AutoFlash.scpt script to be automatically called when the Volume changes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <true/> <key>Label</key> <string>WatchingVolumesPath</string> <key>LowPriorityIO</key> <true/> <key>Program</key> <string>/usr/bin/osascript</string> <key>ProgramArguments</key> <array> <string>osascript</string> <string>/Users/***insert-your-file-path***/KG108 - IT projects/31 sd card automation for friend/AutoFlash2.scpt</string> </array> <key>ServiceDescription</key> <string>Runs Applescript directly when Volumes Path changes</string> <key>StandardOutPath</key> <string>/Users/***insert-your-file-path***/KG108 - IT projects/31 sd card automation for friend/AutoFlashLog.txt</string> <key>WatchPaths</key> <array> <string>/Volumes</string> </array> </dict> </plist> |
Lines 16 and 21 need to be changed to the location of where you have put the script.
Here is the AutoFlash2.scpt
This is used to read that a USB/SD card has been loaded, makes sure it's the right one with the right file, and then calls another script of your choice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | property flashState : 2 set triggerFile to "SDCARD001:test.txt" set script2Run to "Macintosh HD:Users:***insert-file-path***:KG108 - IT projects:31 sd card automation for friend:" & "MacScript.scpt" tell application "Finder" if file triggerFile exists then --The file is found, therefore, the flash drive is present if flashState = 2 then --This indicates that MacScript.scpt needs to be called run script file script2Run set flashState to 0 --After the script has been run, change the flashState property end if else --This is ONLY accessed when the file no longer exists, therefore, the flash drive is NOT present if flashState = 0 then --Since the drive is no longer present, the value of flashState must be re-set to 2 in order to prepare for the next hookup set flashState to 2 end if end if end tell |
Line 2 should be set to be your USB / SD Card name instead of "SDCARD001" and the file you wan to detect e.g. "test.txt" - so if you want this code to work without changing it, change your SD Card to be called "SDCARD001" and add a file to the root of the disk called "test.txt". You also want to change line 3 and set the location of the script, note that you should use colons instead of backspaces.
And then here is the MacScript.scpt
The script performs the custom actions on the USB / SD Card / Flash drive that you have inserted
1 2 3 4 5 | tell application "Finder" display dialog "Hello World" end tell |
For now it just outputs hello world, but you should configure this to do what you want it to do - i.e. copy all the files you want to copy off the usb drive / flash drive / sd card
The AutoFlashLog.txt file
You will also need to create the AutoFlashLog.txt file for this to run - this file should be blank/empty initially
Running the script
To run the script, and get it to detect the SD Card load and then run the script, open terminal and type the following;
1 | launchctl load -w '/Users/***path-to-your-code***/KG108 - IT projects/31 sd card automation for friend/auto-sd-card.plist'
|
Note that when you insert the SD Card it will now fire the script, which will simply give you a 'hello world' message
Note that this runs in the background and watches the insert of the SD Card, it will continue to do that until you log out and log back in. There is a way to get this to load whenever you boot your machine, and the article that this project was based on gives details of this.
Debugging if it doesn't work
Note it it doesn't work you can debug by viewing the output under console. Also you should run the scripts in AppleScript editor to debug them until they work successfully
Hope that helps, I'll post follow up posts if I develop this any further
