I would like to be able to write a script so that I can detect when a flash drive is inserted into a USB port. I would then copy a specific file from the users hard drive to this flash drive.
I need to be able to know the name of the flash drive so I can copy to it. I also need to make sure I am not copying to a hard drive.
the following script really only lets me know if there is a second disk.
any help would be appreciated
[on Is_Flash_Drive_Present()
try
display dialog "Please disconect all peripherals then insert a USB memory stick; THEN click the " & quote & "OK" & quote & " button" with icon note
on error number -128
return "user canceled"
--capture if user canceled (error number -128)
end try
--delay so USB drive can mount
delay 5
--check for USB drive
try
set info_disc to (do shell script "diskutil info disk2") as string
return true
on error
return false
end try
end Is_Flash_Drive_Present]
Model: macbook pro
AppleScript: 2.6.1
Browser: Safari 537.77.4
Operating System: Mac OS X (10.8)
Thanks for the info.
I read the thread, but it doesn’t quite work for me. It seems to detect the USB FlashDrive by checking for a file located on the FlashDrive. It uses that to copy that file from the FlashDrive to the users internal Hard Drive. That is not my situation.
Let me explain:
I have a Filemaker Pro database that stores recorded audiofiles. When a client comes in who wants the audiofile, the user clicks a button in the Filemaker pro Database that runs an Applescript. I want that Applescript to copy the audiofile to a blank USB Flashdrive.
With Applescript I need to :
1 - Detect that a Blank USB FlashDrive has been inserted.
2- Get the Name of the Blank USB FlashDrive, so I can get the path
3- Copy the audiofile from the users internal Hard Drive to the Blank USB FlashDrive (I know how to do that)
4 - Eject the USB FlashDrive
5- Let the user know it is safe to remove the USB FlashDrive
I think it comes down to what you mean by “detect.” AppleScript can certainly examine your Volumes, and also determine which volumes can be unmounted (thus most likely a flash drive), but it really cannot detect the presence of a drive all by itself.
Since I wrote that article about detecting flash drives, the capabilities of launchd have improved, and I believe it is now possible to fire an AppleScript whenever a flash drive is inserted. This page lists all the latest keys that a launchd agent can utilize. I recommend experimenting with StartOnMount and WatchPaths.
I would love to experiment some myself, but I am leaving the continent early tomorrow morning, and will not have access to a Mac for about 10 days. If this thread is still alive when I return, I will see what I can do.
I need to have the Flashdrive detected only after the User presses a button in the Filemaker database. I therefore do not think the launchd will work.
I have been testing various ways to detect a flash drive and acquire its name using the application “finder” and shell scripts. This what I have come up with. It is not elegant and I am not sure how it might fail. Any help in rewriting and noting flaws would be appreciated
[
set VolumeName to Is_Flash_Drive_Present()
on Is_Flash_Drive_Present()
try
display dialog "Please disconect all peripherals then insert a USB memory stick; THEN click the " & quote & "OK" & quote & " button" with icon note
on error number -128
return "user canceled"
--capture if user canceled (error number -128)
end try
--delay so USB drive can mount
delay 5
--check for USB drive
try
set info_disc to (do shell script "diskutil info disk2") as string
-- if we don't error then more then one disc is connected to computer
--so now we have to test for flash drive
on error
return false
end try
set currentDisks to paragraphs of (do shell script "ls /Volumes")
set x to 1
set y to number of items of currentDisks
set NumberofDiscs to y
repeat y times
--make sure the VolumeName is the Flash Drive
set VolumeName to item x of currentDisks
set discNumber to NumberofDiscs
repeat NumberofDiscs times
set whichdisc to "diskutil info disk" & discNumber
set info_disc to (do shell script whichdisc) as string
if info_disc contains "Generic" and info_disc contains "USB" and info_disc contains "Flash" and info_disc contains VolumeName then
return VolumeName
end if
set discNumber to discNumber - 1
end repeat
set x to x + 1
end repeat
Just make a list of existing disks before and compare with a list after the dialog. You can still verify if it is a USB disk, in some situations a suprise ejectable disk might come from Time Machine.
tell application "System Events"
set n to name of every disk whose ejectable is true
end tell
display dialog "bof"
tell application "System Events"
set m to name of every disk whose ejectable is true
end tell
set thedisk to missing value
repeat with o in m
if o is not in n then
set r to do shell script "diskutil info " & quoted form of ( "/Volumes/" & o)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set y to ((words of r) as text)
set AppleScript's text item delimiters to saveTID
set y to y contains "Protocol USB"
if y then
set thedisk to o
exit repeat
end if
end if
end repeat
if thedisk is missing value then
display dialog "You had ONE thing to do!"
end if
return thedisk
In this case the text is converted to a list of words and then back to text with a space as separator. Eliminates the spaces and other fluff in the returned text.