Friday, October 31, 2014

#1 2010-04-01 08:36:57 am

skipzone
Member
Registered: 2010-01-17
Posts: 17

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)

Offline

 

#2 2010-04-01 09:08:47 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

Hi,

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


regards

Stefan

Offline

 

#3 2010-04-01 09:56:26 am

skipzone
Member
Registered: 2010-01-17
Posts: 17

Re: sense USB drive and invoke folder action

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

Offline

 

#4 2010-04-01 11:07:08 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

It's not complicated.

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

Applescript:


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

Last edited by StefanK (2010-04-01 11:18:13 am)


regards

Stefan

Offline

 

#5 2010-04-01 12:57:05 pm

skipzone
Member
Registered: 2010-01-17
Posts: 17

Re: sense USB drive and invoke folder action

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

Offline

 

#6 2010-04-01 01:04:28 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

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

Applescript:



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

Last edited by StefanK (2010-04-01 01:07:25 pm)


regards

Stefan

Offline

 

#7 2010-04-01 02:20:09 pm

skipzone
Member
Registered: 2010-01-17
Posts: 17

Re: sense USB drive and invoke folder action

thanks Stefan

that works perfectly

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

regards Pete

Offline

 

#8 2010-04-01 02:26:05 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

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


regards

Stefan

Offline

 

#9 2010-04-01 04:08:31 pm

Dylan Weber
Member
From: IL, USA
Registered: 2009-08-07
Posts: 539

Re: sense USB drive and invoke folder action

StefanK wrote:

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. hmm


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#10 2010-04-01 04:13:50 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

Dylan Weber wrote:

I thought like I was released from a jail cell…

It's exactly the same feeling after learning Objective-C… wink


regards

Stefan

Offline

 

#11 2010-04-05 03:45:31 pm

skipzone
Member
Registered: 2010-01-17
Posts: 17

Re: sense USB drive and invoke folder action

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

Offline

 

#12 2010-04-05 04:01:54 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10624
Website

Re: sense USB drive and invoke folder action

skipzone wrote:

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

or rename the existing file as "version_(n)"

Actually I've expected this question much earlier wink

Deleting the file is quite easy

Applescript:


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

Applescript:


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

Last edited by StefanK (2010-04-05 04:03:13 pm)


regards

Stefan

Offline

 

#13 2010-04-05 04:17:29 pm

skipzone
Member
Registered: 2010-01-17
Posts: 17

Re: sense USB drive and invoke folder action

thanks Stefan

There is no holiday in applescript! wink

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.102 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)