Help needed building Droplet with multiple sub-routines

Hi;

I’ve got an Applescript working that analyzes a Framemaker MIF file and moves different types of markers to different locations in the MIF. It’s currently composed of three sub-routines (one that moves all of the markers to the front or back of a <ParaLine tag, then a second that moves just the Glossary markers to the first ParaLine tag inside a <Para tag.

At the moment, it asks for the user to choose a MIF file to work with, saves a temp file after the first transition, then asks the user to choose the second file (in this case, the temp file) – this is a result of building the two sub-routines as two programs; the third sub-routine is merely Apple’s example for saving files.

I’ve tried a few times to place the sub-routines inside or at the end of a droplet shell, as well as trying to remove the sub-routines by merging the two main routines into a single program, but everything I try seems to break something else (it’s been a looong day). :frowning:

I’ve built programs within droplet shells before without a hitch, but for some reason these sub-routines keep coming back and biting me.

Can anybody point me to the easiest way to add a droplet feature that would allow me to drop multiple MIFs on it, ask me where to place the cleaned files, and then follow through and place the converted files to the new folder?

The code, long and clunky as it is, works:

--	MIFF Cleaner v0.04 2006/Mar/27 15:00
--	This version selects the file, breaks it into sections with "<ParaLine" as a text item delimiter, re-assembles the parts and writes the new file to "Testxx"
--	Next step: analyzing for tags, then rearranging and writing

set vParaLine to "<ParaLine"
set CurrentDoc to (read (choose file))
moveMarkers of CurrentDoc for vParaLine


set vPara to "<Para "
set CurrentDoc to (read (choose file))
moveGlossaryMarkers of CurrentDoc for vPara

--- SUB-ROUTINES ---

to moveMarkers of currText for delimiterString
	with timeout of 10000 seconds (* time-consuming statements *)
		set tid to text item delimiters
		set text item delimiters to delimiterString
		--	set myFile to (open for access file "Test File" with write permission)
		set tempResult to ""
		considering case
			tell currText to if (count text items) is 1 then
				set searchResult to "ERROR: no match found for \"" & ({""} & "\".")
			else
				set TextCount to get count of text items in currText
				repeat with counter1 from 1 to TextCount
					if counter1 = TextCount then
						set searchResult to (text item counter1 as string)
					else
						set searchResult to (text item counter1 & "<ParaLine " as string)
					end if
					if searchResult contains "<Marker" and counter1 > 1 then
						-- Break down the current text item line by line (eg. Paragraph by Paragraph), checking for
						-- Location (eg. currentParagraph contains "Mtype") and type of Marker; once found, and the end of the ParaLine section has been reached (eg. currentParagraph contains "> # end ParaLine")
						-- we re-order the Markers, moving crossref, Glossary and (?) Type 18 markers to the front of the string, all else to the back, Index marker last
						(* Marker types: 9 - Cross-Ref 6 = Glossary 8 = Hypertext 12 = Type 12 14 = Type 14 18 = Type 18 2 = Index *)
						
						--	set CurrentParagraph to 1
						set TotalParagraphs to (count of paragraphs in searchResult)
						set ParaText to ""
						set FrontMarkers to ""
						set BackMarkers to ""
						set tempString to ""
						set tempMarker to ""
						set MarkerFlag to "off"
						set ParaLineFlag to "on"
						
						repeat with CurrentParagraph from 1 to TotalParagraphs -- while tempString does not contain "> # end of Para "
							set tempString to get paragraph CurrentParagraph of searchResult
							
							-- If we're currently between <ParaLine and > # end of ParaLine...
							if (ParaLineFlag = "on") then
								if (tempString contains "<TextRectID") then
									set tempResult to (tempResult & return & tempString & return as string)
									
								else if (tempString contains "<Marker") then
									
									-- Process the Marker
									set MarkerFlag to "on"
									set tempMarker to (tempMarker & tempString & return as string)
								else if ((tempString contains "<MType") or (tempString contains "<MText") or (tempString contains "<MCurr")) then
									set tempMarker to (tempMarker & tempString & return as string)
								else if ((MarkerFlag is "on") and (tempString contains "Unique")) then
									set tempMarker to (tempMarker & tempString & return as string)
								else if (tempString contains "> # end of Marker") then
									set MarkerFlag to "off"
									set tempMarker to (tempMarker & tempString as string)
									
									
									-- Marker is complete; check type; move appropriately, clear tempMarker 
									if (tempMarker contains "<MType 9>") then
										set tempMarker to (tempMarker & return & FrontMarkers as string)
										set FrontMarkers to (tempMarker)
										set tempMarker to ""
									else if (tempMarker contains "<MType 2>") then
										set BackMarkers to (BackMarkers & tempMarker & return as string)
										set tempMarker to ""
									else
										set FrontMarkers to (FrontMarkers & tempMarker as string)
										set tempMarker to ""
									end if
									-- End of Marker Processing
									
									-- If we're at the end of the ParaLine segment...
								else if (tempString contains "> # end of ParaLine") then
									if (FrontMarkers is not "") then
										set tempResult to (tempResult & return & FrontMarkers & ParaText & BackMarkers & tempString & return as string)
									else
										set tempResult to (tempResult & FrontMarkers & ParaText & BackMarkers & tempString & return as string)
									end if
									set tempString to ""
									set ParaText to ""
									set ParaLineFlag to "off"
								else
									set ParaText to (ParaText & tempString & return as string)
								end if
							else
								set tempResult to (tempResult & tempString & return as string)
							end if
						end repeat
						-- set tempResult to (tempResult & return & FrontMarkers & ParaText & BackMarkers)
						--set FrontMarkers to ""
						--set BackMarkers to ""
					else
						set tempResult to (tempResult & searchResult)
					end if
				end repeat
			end if
		end considering
		set text item delimiters to tid
		--	set newFile to (CurrentDoc & "Updated" as string)
		my write_to_file(tempResult, "MIF_Temp", true)
	end timeout
end moveMarkers

-- set Number_of_Paragraphs to (count of "<Marker" in searchResult)
-- set the_offset to (offset of vmnum in CurrentDoc)
-- Number_of_Paragraphs

to moveGlossaryMarkers of currText for delimiterString
	with timeout of 10000 seconds (* time-consuming statements *)
		set tid to text item delimiters
		set text item delimiters to delimiterString
		--	set myFile to (open for access file "Test File" with write permission)
		set FinalResult to ""
		considering case
			tell currText to if (count text items) is 1 then
				set searchResult to "ERROR: no match found for \"" & ({""} & "\".")
			else
				set TextCount to get count of text items in currText
				repeat with counter1 from 1 to TextCount
					if counter1 = TextCount then
						set searchResult to (text item counter1 as string)
					else
						set searchResult to (text item counter1 & "<Para " & return as string)
					end if
					if searchResult contains "<MType 6>" and counter1 > 1 then
						
						--	set CurrentParagraph to 1
						set TotalParagraphs to (count of paragraphs in searchResult)
						set ParaText to ""
						set GlossaryMarkers to ""
						set BackMarkers to ""
						set tempString to ""
						set tempMarker to ""
						set MarkerFlag to "off"
						set ParaLineFlag to "off"
						
						repeat with CurrentParagraph from 1 to TotalParagraphs
							set tempString to get paragraph CurrentParagraph of searchResult
							
							-- If we're currently between <Para and > # end of Para ...
							if (ParaLineFlag = "off") then
								set FinalResult to (FinalResult & tempString & return as string)
								if (tempString contains "<ParaLine") then
									set ParaLineFlag to "on"
								end if
							else if (tempString contains "<Marker") then
								
								-- Process the Marker
								set MarkerFlag to "on"
								set tempMarker to (tempMarker & tempString & return as string)
							else if ((tempString contains "<MType") or (tempString contains "<MText") or (tempString contains "<MCurr")) then
								set tempMarker to (tempMarker & tempString & return as string)
							else if ((MarkerFlag is "on") and (tempString contains "Unique")) then
								set tempMarker to (tempMarker & tempString & return as string)
							else if (tempString contains "> # end of Marker") then
								set MarkerFlag to "off"
								set tempMarker to (tempMarker & tempString as string)
								
								-- Marker is complete; check type; move appropriately, clear tempMarker 
								if (tempMarker contains "<MTypeName `Glossary'>") then
									set GlossaryMarkers to (GlossaryMarkers & return & tempMarker & return as string)
									set tempMarker to ""
								else
									set ParaText to (ParaText & return & tempMarker as string)
									set tempMarker to ""
								end if
								-- End of Marker Processing
								
								-- If we're at the end of the Para segment...
							else if ((tempString contains "> # end of Para") and (tempString does not contain "> # end of ParaLine")) then
								set FinalResult to (FinalResult & GlossaryMarkers & ParaText & tempString & return & "<Para " & return as string)
								-- set ParaLineFlag to "off"
							else
								set ParaText to (ParaText & tempString & return as string)
								
							end if
							set tempString to ""
							-- set ParaText to ""
						end repeat
						-- set FinalResult to (FinalResult & return & FrontMarkers & ParaText & BackMarkers)
						--set FrontMarkers to ""
						--set BackMarkers to ""
					else
						set FinalResult to (FinalResult & searchResult)
					end if
				end repeat
			end if
		end considering
		set text item delimiters to tid
		--	set newFile to (CurrentDoc & "Updated" as string)
		my write_to_file(FinalResult, "Cleaned_MIF", true)
		tell application "Finder"
			delete ("MIF_Temp")
		end tell
	end timeout
end moveGlossaryMarkers

-- set Number_of_Paragraphs to (count of "<Marker" in searchResult)
-- set the_offset to (offset of vmnum in CurrentDoc)
-- Number_of_Paragraphs

-- write-to-file subroutine, from AppleScript Resources
on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

– Walt Sterdan

Browser: Safari 312.3
Operating System: Mac OS X (10.3.9)