global firstPass
global lastPass

tell application "Finder"
	activate
	set diskImageRecov to (choose file with prompt "Disk Image to try to recover JPEGs from:")
	item (diskImageRecov as string) of application "Finder"
	--	display dialog "testing: " & diskImageRecov
	set WhereToWrite to (choose folder with prompt "Choose folder to write recovered JPEGs:")
	item (WhereToWrite as string) of application "Finder"
	--	display dialog "testing: " & WhereToWrite
	set readPosition to text returned of (display dialog "Set Read Position to (0 to start at beginning):" default answer "0")
	set howManyLoop to text returned of (display dialog "Try for how many JPEGs:" default answer "300")
	--set firstRead to "test?"
	set readLength to 3000
	set myFileNameNum to text returned of (display dialog "Start file numbering with:" default answer "0")
	set openString to (ASCII character 69) & (ASCII character 120) & (ASCII character 105) & (ASCII character 102) as text

	set myFileReference to open for access file diskImageRecov

	try
		set firstLoop to true
		repeat howManyLoop times
			try
				set myFileNameNumUse to myFileNameNum as string
				
				if myFileNameNum < 100 then
					set myFileNameNumUse to "0" & myFileNameNumUse as string
					if myFileNameNum < 10 then
						set myFileNameNumUse to "0" & myFileNameNumUse as string
					end if
				end if
				
				set myFileWrite to WhereToWrite & myFileNameNumUse & "-" & readPosition & ".jpg" as string
				--set myFileWrite to file myFileWriteName of folder WhereToWrite
				set myFileReferenceWrite to (open for access file myFileWrite with write permission)
				
				set eof of myFileReferenceWrite to 0
				
				set firstPass to true
				if firstLoop then
					set firstPass to false
				end if
				set lastPass to false
				read myFileReference from readPosition for 0
				
				repeat while not lastPass
					
					set firstRead to read myFileReference for readLength
					set readPosition to readPosition + readLength
					set firstRead to replace_chars(firstRead, openString) of me
					
					write firstRead to myFileReferenceWrite
				end repeat
				
				
				--		display dialog ("wrote " & myFileNameNum as string) giving up after 5
				say ("wrote " & myFileNameNum as string)
				
				
				close access myFileReferenceWrite
				set readPosition to readPosition - readLength
				set myFileNameNum to myFileNameNum + 1
				set firstLoop to false
			on error errText number errNum
				close access myFileReferenceWrite
				exit repeat
			end try
		end repeat
		
		close access myFileReference
		beep
		say "done daddyo"
	on error errText number errNum
		
		close access myFileReference
		close access myFileReferenceWrite
		display dialog errText default button "Cancel"
	end try
	
	
end tell


on replace_chars(this_text, search_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	
	if the number of items of item_list > 1 then
		if firstPass then
			set the item_list to (characters -6 through -1 of item 1 of item_list) & the search_string & item 2 of item_list
			set firstPass to false
		else
			set lastPass to true
			set the item_list to item 1 of item_list
		end if
	end if
	set AppleScript's text item delimiters to ""
	set this_text to the item_list as string
	return this_text
end replace_chars


-- applescript image recover page