sense USB drive and invoke folder action

HI MSASF,

Is there applescript to detect when i insert a named usb memory stick? i need to make it invoke my folder action made in automator to copy the contents of the usb into a folder on the desktop.

seems to be possible in 10.4

but not 10.6

thanks for your help…

Browser: Firefox 3.6
Operating System: Mac OS X (10.6)

Hi,

launchd is the best method to accomplish this.
Take a look at this article

That looks too complicated for me to impliment

This is the Automator script which only works on 10.4

http://automator.us/examples-08.html

It’s not complicated.

¢ Create an compiled script with this code (it is based on Craig’s article)


property flashState : false
property flashName : "myDisk"

property myApp : "Autoflash"
-- property allNotificationsList : {"Disk present", "Disk gone", "launchd"}
-- property enabledNotificationsList : {"Disk present", "Disk gone", "launchd"}

set currentDisks to paragraphs of (do shell script "ls /Volumes")
(*
tell application "GrowlHelperApp"
	register as application myApp all notifications allNotificationsList ¬
		default notifications enabledNotificationsList icon of application "Disk Utility"
	notify with name "launchd" title "Notification" description ¬
		"Folder Volume changed" application name myApp
end tell
*)
if (flashName is in currentDisks) then
	if (flashState is false) then -- the flash drive is present
		-- do something when the disk is mounted
		
		-- Growl_notify("Disk present", ("Disk " & flashName & " is present"), "")
		set flashState to true --After the script has been run, change the flashState property
	end if
else
	if flashState is true then --This is ONLY accessed when the flash drive is NOT present
		-- do something when the disk is unmounted
		
		-- Growl_notify("Disk gone", ("Disk " & flashName & " has gone"), "")
		set flashState to false
	end if
end if
(*
on Growl_notify(name_, title_, description_)
	tell application "GrowlHelperApp"
		notify with name name_ title title_ description description_ application name myApp
	end tell
end Growl_notify
*)

¢ if you use Growl, uncomment all lines which do not start with – do something
¢ Change the value of the property flashName to the name of your disk
¢ Insert your code to move files after – do something when the disk is mounted
¢ Save the script somewhere

¢ Download this Installer: launchd_installer
¢ Run it, press Load and choose the script

that’s it.

To unload the agent run the installer again, press Unload

The agent is located in ~/Library/LauchAgents
The script is located in ~/Library/Scripts/launchd

thank you i can see that works

but now i can’t transfer my automator file from 10.4 to 10.6 so my folder action won’t run.

it just needs to copy a file on the usb into a folder on the desktop

don’t understand why there isn’t basic actions like this available to automator

Automator is not needed to move a file,
use this, insert the whole try block into the appropriate place
and change the “myFile.ext” and “myFolder” to your desired names


.
if (flashName is in currentDisks) then
	if (flashState is false) then -- the flash drive is present
		-- do something when the disk is mounted
		try
			tell application "Finder" to move file "myFile.ext" of disk flashName to folder "myFolder" of desktop
		on error e
			display dialog "error " & e & " occured"
		end try
		-- Growl_notify("Disk present", ("Disk " & flashName & " is present"), "")
		set flashState to true --After the script has been run, change the flashState property
	end if
else
.

thanks Stefan

that works perfectly

one expects Automator to do so much. But actually a better knowledge of Applescript is the key!

regards Pete

Automator is just a GUI for AppleScript with a lot of restrictions, because you are dependent on the
given actions. But for most of the basic tasks it’s very comfortable

Very true. I used Automator and then Applescript. I thought like I was released from a jail cell when I used Applescript. So many people use AppleScript then Automator it seems. :confused:

It’s exactly the same feeling after learning Objective-C. :wink:

how do i add the line …

to delete the file of the same name which already exists in the folder?

or rename the existing file as “version_(n)”

before copying the new file from the USB

regards

Actually I’ve expected this question much earlier :wink:

Deleting the file is quite easy


property myFileName : "myFile.ext"

.
if (flashName is in currentDisks) then
	if (flashState is false) then -- the flash drive is present
		-- do something when the disk is mounted
		try
			tell application "Finder"
				if exists file myFileName of folder "myFolder" of desktop then
					delete file myFileName of folder "myFolder" of desktop
				end if
				move file myFileName of disk flashName to folder "myFolder" of desktop
			end tell
		on error e
			display dialog "error " & e & " occured"
		end try
		-- Growl_notify("Disk present", ("Disk " & flashName & " is present"), "")
		set flashState to true --After the script has been run, change the flashState property
	end if
else
.

To rename the file with a timestamp suffix [yyyymmdd-HHMMSS] use this code


property myFileName : "myFile.ext"

.
if (flashName is in currentDisks) then
	if (flashState is false) then -- the flash drive is present
		-- do something when the disk is mounted
		try
			tell application "Finder"
				if exists file myFileName of folder "myFolder" of desktop then
					set TimeStamp to do shell script "/bin/date +%Y%m%d-%H%M%S"
					set name of file myFileName of folder "myFolder" of desktop to (TimeStamp & "_" & myFileName)
				end if
				move file myFileName of disk flashName to folder "myFolder" of desktop
			end tell
		on error e
			display dialog "error " & e & " occured"
		end try
		-- Growl_notify("Disk present", ("Disk " & flashName & " is present"), "")
		set flashState to true --After the script has been run, change the flashState property
	end if
else
.

thanks Stefan

There is no holiday in applescript! :wink: