Copying files from Compact flash... what wrong?

I’m trying to write this applescript to copy files from a compact flash card. First if the card is in the script sees all the files but copies nothing. Second, if the card’s not mounted it does not display the “No memory card found” message. I’m sure its something stupid but I can’t figure it out.

tell application "Finder"
	activate
	if exists disk "NIKON D2X" then
		set source_folder to folder "DCIM" of disk "NIKON D2X"
		set destination_folder to "Users/jscherer/Pictures/Download/"
		set folder_list to every folder of source_folder
		repeat with this_folder in folder_list
			copy every item in this_folder to destination_folder
		end repeat
		if not (exists disk "NIKON D2X") then
			display dialog "No memory card found." buttons {"OK"}
		else
			display dialog "Done." buttons {"OK"}
		end if
	end if
	
end tell

This could be wrong, but I believe the “copy” syntax is a command for setting variables. Try substituting copy for duplicate.
It is the finder command for copying files from one location to another. As to the not finding disc, try putting the syntax in a try statement. Something like:

tell application "Finder"
	try
		set source_folder to "NIKON D2X:DCIM"
		set source_folder to source_folder as alias
		make new Finder window to source_folder
	on error
		display dialog "No memory card found." buttons {"OK"}
	end try
end tell

I haven’t tested this but it might work.
Chuck:D

Jacques

For what its worth you might find this script a little more flexable. I am a photographer and my whole workflow is scripted
This script chooses the folder you want to copy from and prompts you to trash the images.


set processimages to "Users/jscherer/Pictures/Download/"
set fromcamera to choose folder with prompt "Choose a folder from which to process images"
tell application "Finder"
	duplicate every item of fromcamera to processimages
	set cameradisk to name of disk of fromcamera
end tell
tell application "Finder"
	activate
	beep 1
	display dialog "Your images are copied.
You can eject disk and leave the images on it or trash the images and eject the disk." buttons {"Eject disk", "Trash Images"} default button 2 giving up after 10000 with icon 2
	if the button returned of the result is "Trash Images" then
		beep 3
		display dialog "This action permantley deletes images from your disk. Are you sure?" giving up after 10000 with icon 2
		delete every item of fromcamera
		try
			empty trash
			delay 1
			set y to "Trash"
			set x to name of every window
			repeat until x does not contain y
				set x to name of every window
				delay 0.2
			end repeat
			eject disk cameradisk
			display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 10000
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1 giving up after 10000 with icon 2
			
		end try
	else
		try
			eject disk cameradisk
		on error the error_message number the error_number
			display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1
		end try
		display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 100000
		
	end if
	
end tell


Model: 11 macs, all my babies
AppleScript: 2.1.1
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

Weedinner:

Thanks for the great script. I used it as a base and combined it with some stuff Bill Briggs wrote at http://maccentral.macworld.com/features/applescriptprimer43/ . Basicly it copies all the files from my card to a unique directory in my Pictures directory with a name based on the current date plus a sequence number.

I’ll work on more additions as I get more time but having it do this much is great! Thanks again everyone.

-John

--
-- Test for card
--
tell application "Finder"
	activate
	if exists disk "NIKON D2X" then
		--
		--Format date for our purpopses
		--
		set todaysDate to (current date)
		set {d, m, y} to {day, month, year} of todaysDate
		set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}
		repeat with i from 1 to 12
			if m = (item i of monthList) then
				set monthString to text -2 thru -1 of ("0" & i)
				exit repeat
			end if
		end repeat
		set dayString to text -2 thru -1 of ("0" & d)
		set todaysDate to monthString & "-" & dayString & "-" & y
		-- Lets start
		tell application "Finder"
			set basefolder to ("Users/jscherer/Pictures/Unprocessed/" as POSIX file) as alias
			set theList to every item of basefolder whose name begins with "JRS"
			-- JRS is my project name, use yours here
			sort theList by creation date
			set theList to the result
			set label index of item 1 of theList to 0
			--
			-- Make the new a new unique folder based on current date + a sequence number
			--
			if (name of the first item of theList) contains todaysDate then
				set firstName to the name of (first item of theList)
				set theCount to (text -2 thru -1 of firstName as number)
			else
				set the theCount to 0
			end if
			set fileNameSuffix to (text -2 thru -1 of ("0" & ((theCount + 1) as text)))
			set theNewDir to "JRS_" & todaysDate & "_" & fileNameSuffix
			with timeout of 20 seconds
				make new folder at basefolder with properties {name:theNewDir}
			end timeout
			set comment of folder theNewDir of basefolder to "This backup was written to disk on " & ((current date) as text)
			--
			-- Copy images from card
			--
			-- set fromcamera to choose folder with prompt "Choose a folder from which to process images"
			set fromcamera to folder "DCIM" of disk "NIKON D2X"
			tell application "Finder"
				repeat with this_folder in (get folders of fromcamera)
					duplicate items of fromcamera to folder theNewDir of basefolder
				end repeat
				set cameradisk to name of disk of fromcamera
			end tell
			tell application "Finder"
				activate
				beep 1
				display dialog "Your images are copied.  Eject disk or trash the images and eject the disk." buttons {"Eject disk", "Trash Images"} default button 2 giving up after 10000 with icon 2
				if the button returned of the result is "Trash Images" then
					beep 3
					display dialog "This action permantley deletes images from your disk. Are you sure?" giving up after 10000 with icon 2
					delete every item of fromcamera
					try
						empty trash
						delay 1
						set y to "Trash"
						set x to name of every window
						repeat until x does not contain y
							set x to name of every window
							delay 0.2
						end repeat
						eject disk cameradisk
						display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 10000
					on error the error_message number the error_number
						display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1 giving up after 10000 with icon 2
						
					end try
				else
					try
						eject disk cameradisk
					on error the error_message number the error_number
						display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1
					end try
					display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 100000
					
				end if
			end tell
			
			say "Your backup is now complete."
		end tell
	else
		display dialog "No memory card found." buttons {"OK"}
	end if
	
end tell

Model: Powerbook G4 1.5Ghz 1.25 GB Ram
Browser: Firefox 1.5.0.4
Operating System: Mac OS X (10.4)

Hi,

Don’t forget the date formats that are available to you with

do shell script "date ``+%Y%m%d.%H%M%S``"

or somesuch. Details via man date
or in the PHP manual.

Or this minor modification of a one-liner by Kai that’s much, much faster.

set myDelim to "."
tell (current date) as string to tell my date it to tell (year * 10000 + (its month) * 100 + day) as string to text 1 thru 4 & myDelim & text 5 thru 6 & myDelim & text 7 thru 8
--> 2006.06.21

And for manipulating the parts, this construct is useful in Tiger:

set {year:yy, month:mm, day:dd, time:tt} to (current date)

and to get mm as a number, set mm to mm as integer. The time is in seconds after midnight.

Hi Adam, thanks for the date modification snip. That works like a charm and make the scrip slightly smaller.

One thing I might is to pull the date from one of the images files and base the directory name on this date. That way I have directory based on when the images were taken. I’ll have to think about that for a while. I would have to think about how to handle when I have images from multiple dates in the directory, which happens a lot. Maybe I could look at all the date and use the oldest or newest file date… Anyone have any thoughts?

Heres the current code:

--
-- This script will download all images from
-- an inserted compact flash card to your computer 
-- John Scherer - (jrsphoto AT gmail.com)
--
tell application "Finder"
	activate
	--
	-- Test for Compactflash
	--
	if exists disk "NIKON D2X" then
		display dialog "Download Images?" buttons {"Yes", "No"} default button 1 giving up after 1000 with icon 2
		--
		--Format date for our purpopses
		--
		set myDelim to "."
		tell (current date) as string to tell my date it to tell (year * 10000 + (its month) * 100 + day) as string to text 1 thru 4 & myDelim & text 5 thru 6 & myDelim & text 7 thru 8
		tell application "Finder"
			set {year:yy, month:mm, day:dd, time:tt} to (current date)
			set mm to mm as integer
			set todaysDate to mm & myDelim & dd & myDelim & yy as string
		end tell
		--
		--  Locate your base folder, build a list of items
		-- in the folder and sort by creation date
		--		
		tell application "Finder"
			set basefolder to ("Users/jscherer/Pictures/Unprocessed/" as POSIX file) as alias
			set theList to every item of basefolder whose name begins with "JRS"
			-- JRS is my project name, use yours here
			sort theList by creation date
			set theList to the result
			set label index of item 1 of theList to 0
			--
			-- Make the new a new unique folder within
			-- the base folder with a name of todaysDate + a sequence number
			--
			if (name of the first item of theList) contains todaysDate then
				set firstName to the name of (first item of theList)
				set theCount to (text -2 thru -1 of firstName as number)
			else
				set the theCount to 0
			end if
			set seqenceNum to (text -2 thru -1 of ("0" & ((theCount + 1) as text)))
			set theNewDir to "JRS_" & todaysDate & "_" & seqenceNum
			with timeout of 20 seconds
				make new folder at basefolder with properties {name:theNewDir}
			end timeout
			set comment of folder theNewDir of basefolder to "This backup was written to disk on " & ((current date) as text)
		end tell
		--
		-- Copy images from card
		--
		-- set fromcamera to choose folder with prompt "Choose a folder from which to process images"
		set fromcamera to folder "DCIM" of disk "NIKON D2X"
		tell application "Finder"
			repeat with this_folder in (get folders of fromcamera)
				duplicate items of fromcamera to folder theNewDir of basefolder
			end repeat
			set cameradisk to name of disk of fromcamera
		end tell
		tell application "Finder"
			activate
			beep 1
			display dialog "Your images have been copied.  Eject disk or trash the images and eject the disk." buttons {"Eject disk", "Trash Images"} default button 2 giving up after 10000 with icon 2
			if the button returned of the result is "Trash Images" then
				beep 3
				display dialog "This action permantley deletes images from your disk. Are you sure?" giving up after 10000 with icon 2
				delete every item of fromcamera
				try
					empty trash
					delay 1
					set y to "Trash"
					set x to name of every window
					repeat until x does not contain y
						set x to name of every window
						delay 0.2
					end repeat
					eject disk cameradisk
					display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 10000
				on error the error_message number the error_number
					display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1 giving up after 10000 with icon 2
				end try
			else
				try
					eject disk cameradisk
				on error the error_message number the error_number
					display dialog "Error: " & the error_number & ". " & the error_message & "Please manually eject your disk" buttons {"Cancel"} default button 1
				end try
				display dialog ("Your been processed, the images deleted and the disk has been ejected" as text) with icon 1 giving up after 100000
			end if
		end tell
		--	
	else
		display dialog "No memory card found." buttons {"OK"}
	end if
end tell

Model: Powerbook G4 1.5Ghz 1.25 GB Ram
Browser: Firefox 1.5.0.4
Operating System: Mac OS X (10.4)

I was installing this script for a friend of mine and I discovered a problem. If the destination directory (theNewDir) is empty, the script fails with the error message: “Can’t get item 1 of {}” (on or around the line “set label index of item 1 of theList to 0”). Any ideas?