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 0
	--	set readPosition to 3200000
	--set readPosition to 248157000
	--	set openString to "ˇÿˇ" as string
	set openString to (ASCII character 255) & (ASCII character 216) & (ASCII character 255) as text
	--	set openString to ASCII character 13
	--	set closeString to "ˇŸ" as string
	set closeString to (ASCII character 13)
	--	set myFileReference to open for access file "Drive:MICRODRIVE.img"
	set myFileReference to open for access diskImageRecov
	--	set myFileReference to open for access file "Drive:testfile"
	try
		--	set bigTexteof to get eof of myFileReference
		--		set bigTexteofNum to bigTexteof - readLength
		--	set bigTextRandomNum to (random number from 0 to bigTexteofNum)
		--	read myFileReference as text before openString
		--		read myFileReference from 24999 for 1
		
		repeat howManyLoop times
			try
				set myFileWrite to WhereToWrite & readPosition & "-" & myFileNameNum & ".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
				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
					
					(*		if firstRead contains openString then
		end if 
		*)
					
					--		display dialog (the number of items of firstRead) as text
					--	set bigText to read myFileReference as text until closeString
					write firstRead to myFileReferenceWrite
				end repeat
				
				(*
		set bigText to read myFileReference as text for 27901
		display dialog "second"
		write bigText to myFileReferenceWrite
*)
				
				--		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
			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 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