Triggering script on mounting SmartMedia card

I have a digital camera that saves its pictures to a SmartMedia card. When I want to copy the pictures to my laptop, I plug in the card reader, insert the card, and a Volume is mounted on my desktop with the pictures. I wrote the script below to get the pictures and put them in a folder with the date in its name. As you can see the pictures are located in a directory called “IMOLYM” inside the Volume “Unlabeled”

set the_folder to “Unlabeled:IMOLYM” as alias

tell application “Finder”
make initial destination folder on Desktop
make new folder at desktop with properties {name:“New_Pics”}
set destination_ to “Titanium 867:Users:tugboat:Desktop:New_Pics” as alias
copy pictures from card to destination
duplicate contents of the_folder to destination_
end tell

use the shell to rename the folder, put it on the desktop and remove the temporary folder

set date_str to do shell script “date “+%m_%d_%Y””
set fldr_name to “Pics_” & date_str
set sscript1 to “mv /Users/tugboat/Desktop/New_Pics/IMOLYM /Users/tugboat/Desktop/” & fldr_name
do shell script sscript1
set sscript2 to “rm -R /Users/tugboat/Desktop/New_Pics”
do shell script sscript2

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

Anyway, this works fine. I would like to know if the script could be triggered by simply mounting the card in the reader. I was thinking about an “if exists” but I could not figure out the syntax. Also, note that I resorted to the shell to rename, move and delete a folder as well as get a date string. I am certain that AS could do this more elegantly, but I know more shell scripting than AppleScripting when it comes to manipulating files and directories.

Thanks for any advice you might have.

Andy

I came up with this so far:

on idle
set sscript1 to “ls /Volumes”
set volumes_ to do shell script sscript1
display dialog volumes_
if volumes_ contains “Unlabeled” then
display dialog “SmartMedia Card insertion detected”
quit
end if
return 3
end idle

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

Hi tugboat,

I must have missed this when you first posted. Does this do what you want to do?

on idle
	tell application "Finder" to set exists_ to exists disk "Unlabeled"
	if exists_ is true then
		set the_folder to "Unlabeled:IMOLYM" as alias
		set date_str to do shell script "date "+%m_%d_%Y""
		set fldr_name to "Pics_" & date_str
		
		tell application "Finder"
			set destination_ to (make new folder at desktop with properties {name:fldr_name})
			duplicate items of the_folder to destination_
		end tell
	end if
	return 3 -- seconds
end idle

– Rob

Rob,

That does it exactly. Thanks so much.

Andy

Glad to help. Here’s the same script with an error handler that throws a dialog if the new folder name is already in use on the desktop.

on idle
	tell application "Finder" to set exists_ to exists disk "Unlabeled"
	if exists_ is true then
		set the_folder to "Unlabeled:IMOLYM" as alias
		set date_str to do shell script "date "+%m_%d_%Y""
		set fldr_name to "Pics_" & date_str
		
		try
			tell application "Finder"
				set destination_ to (make new folder at desktop with properties {name:fldr_name})
				duplicate items of the_folder to destination_
			end tell
		on error e
			display dialog e
		end try
	end if
	return 3 -- seconds 
end idle

– Rob

Uh guys…

Here’s a detailed example of how to attach a Folder Action to the Volumes folder so that when a specific volume is mounted a script runs automatically.

http://www.apple.com/applescript/folderactions/05.html

Enjoy!

Thanks, Sal.

I’m running OS X 10.2.8 and System Events 1.2 (beta). In the past, I’ve encountered problems when attaching a Folder Action to /Volumes/ so I didn’t want to go there. I just tried it again with the same result - System Events crashes. Here’s the test code:

on adding folder items to this_folder after receiving these_volumes
	set name_ to name of (info for (item 1 of these_volumes))
	display dialog name_ & " is mounted."
end adding folder items to

The results vary. Sometimes the dialog appears and sometimes not. The constant is that it always causes the crash. When I first encountered this, I simply assumed that I shouldn’t attach a Folder Action to an invisible folder since the same script works on visible folders.

I’ll be happy to learn that this isn’t a problem in Panther. :slight_smile:

– Rob

Thanks for the continued discussion. Would the problem be resolved if I made the /Volumes folder visible? I am not certain how to do this, but I am pretty sure I have seen details of how to tell the Finder to make something visible that is normally invisible.

Andy

I don’t know what the ramifications of making it visible might be. My cautious nature tells me to leave it alone. :wink:

– Rob

I agree, and I will use your prior suggestion. Thanks a million!

Andy

OK-
This is what I’ve come up with. As I said, I know nothing about applescript, I’m just trying to do a one time deal so that my card will automatically copy to a folder on the desktop while I’m shooting my other card during the birth of my son. This is what I’ve figured out so far by just monkeying around and guessing:
on idle
tell application “Finder” to set exists_ to exists disk “NIKON D70”
if exists_ is true then
set the_folder to “NIKON D70:DCIM:100NCD70” as alias
tell application “Finder”
set destination_ to “HD:Users:christopherplatt:Desktop:Baby”
duplicate items of the_folder to destination_
end tell
end if
return 3 – seconds
end idle

My question is: How do I tell it to eject the disk when it’s done? Can I eliminate the as alias part?
Any help would be appreciated. . .
Chip

All right, here I am replying to my own post. . .After a little more searching through Apple’s applescript guide, I came up with this:
on idle
tell application “Finder” to set exists_ to exists disk “NIKON D70”
if exists_ is true then
set the_disk to “NIKON D70”
set the_folder to “NIKON D70:DCIM:100NCD70”
tell application “Finder”
set destination_ to “HD:Users:christopherplatt:Desktop:Baby”
duplicate items of the_folder to destination_
eject “NIKON D70”
end tell
end if
return 3 – seconds
end idle
But when I run the script (I save it to leave open, so that it’s constantly running) and insert my card, it says “Can’t set “HD:USERS:christopherplatt:Desktop:Baby” to every item of “NIKON D70:DCIM:100NCD70”.” What does that mean? Help me please, the baby was due yesterday. . .
Thanks,
Chip

Hi Chip,

You’re using strings and not references. Why did you leave out the ‘as alias’? You coerce the strings to alias reference with the ‘as alias’.

gl,