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”
setthe_folderto “Unlabeled:IMOLYM” asalias
tellapplication “Finder”
–make initial destination folder on Desktop makenewfolderatdesktopwith properties {name:“New_Pics”} setdestination_to “Titanium 867:Users:tugboat:Desktop:New_Pics” asalias
–copy pictures from card to destination duplicatecontentsofthe_foldertodestination_ endtell
–use the shell to rename the folder, put it on the desktop and remove the temporary folder
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.
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
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
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.
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.
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.
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