Need help to finish this script

Hello I’m having issues combining two different scripts which need to be merged, both scripts work fine by themselves by I can’t find what I’m doing wrong when put them together, I get an error when put "property oneHandler:{} " inside the else block so I have moved them to the top hoping it will work. I’m sure you will also find a lot of staff that is doing nothing :slight_smile:
The if block works fine is the else block which is not working…

Here’s the script:


global newText

set fileText to read (choose file with prompt "Choose a text file:" of type {"eml"})
-- this is the pharse will will check against in the fileText
property CharOrString : {"Department:", "Contact Name:", "Email:", "Budget Code:", "Name:", "Title:", "Tel:", "Fax:", "Web:"}
property dontdeletePhrase : {"Department:", "Contact Name:", "Email:", "Budget Code:", "Name:", "Title:", "Tel:", "Fax:", "Web:"}

---STUDENTS BLOCK---
if fileText contains "Student Introduction Cards" then
	set dontdeletePhrase to "Answer : "
	deleteLinesFromText(fileText, dontdeletePhrase)
	set fileText to newText
	
	set dontdeletePhrase to "<span>"
	deleteSpanFromText(fileText, dontdeletePhrase)
	set fileText to newText
	
	set CharOrString to "Answer : "
	RemoveFromString(fileText, CharOrString)
	set fileText to result
	
	set dontdeletePhrase to "Accepted"
	deleteSpanFromText(fileText, dontdeletePhrase)
	set fileText to newText
	
	set newText to result
	set nameFile to first paragraph of newText
	set currentDate to short date string of (current date) & "-" & (random number from 1 to 999) & tab
	replaceString(newText, return, tab)
	set tabText to result
	replaceString(tabText, return, tab)
	set tabText to result
	set tabText to currentDate & tabText & tab & "ST_BC_" & nameFile & ".pdf"
	set tabText to result
	
else
	linesToBeSaved(fileText)
	RemoveFromStringStaff(fileText, CharOrString)
end if

----**HANDLERS**----

--removes all lines not starting with Match
on deleteLinesFromText(theText, dontdeletePhrase)
	set newText to ""
	try
		
		set textList to paragraphs of theText
		repeat with i from 1 to count of textList
			set thisLine to item i of textList
			if thisLine contains dontdeletePhrase then
				set newText to newText & thisLine & return
			end if
		end repeat
		if newText is not "" then set newText to text 1 thru -2 of newText
	on error
		set newText to theText
	end try
	return newText
end deleteLinesFromText



--removes all lines starting with Match
on deleteSpanFromText(theText, dontdeletePhrase)
	set newText to ""
	try
		
		set textList to paragraphs of theText
		
		-- now repeat over the list and ignore everything that dont have the dontdeletePhrase
		repeat with i from 1 to count of textList
			set thisLine to item i of textList
			if thisLine does not contain dontdeletePhrase then
				set newText to newText & thisLine & return
			end if
		end repeat
		if newText is not "" then set newText to text 1 thru -2 of newText
	on error
		set newText to theText
	end try
	return newText
end deleteSpanFromText

---remove single word match from string
on RemoveFromString(theText, CharOrString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, CharOrString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			if theText does not contain CharOrString then ¬
				return theText
			set AppleScript's text item delimiters to CharOrString
			set lst to theText's text items
		end considering
		set AppleScript's text item delimiters to ASTID
		return lst as text
	end try
	set newText to theText's text items
end RemoveFromString

--replace oldstring with newString
on replaceString(theText, oldString, newString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on linesToBeSaved(theText)
	set newText to ""
	try
		
		set textList to paragraphs of theText
		repeat with i from 1 to count of textList
			set thisLine to item i of textList
			if checkContainingText(thisLine) then
				set newText to newText & thisLine & return
			end if
		end repeat
		if newText is not "" then set newText to text 1 thru -2 of newText
	on error
		set newText to theText
	end try
	return newText
end linesToBeSaved
on checkContainingText(theLine)
	repeat with aPhrase in dontdeletePhrase
		if theLine contains aPhrase then return true
	end repeat
	return false
end checkContainingText

--- remove the xxx: on the string
on RemoveFromStringStaff(theText, CharOrString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, CharOrString, lst
	set ASTID to AppleScript's text item delimiters
	try
		if theText contains CharOrString then ¬
			return theText
		set AppleScript's text item delimiters to CharOrString
		set lst to theText's text items
		set AppleScript's text item delimiters to ASTID
		return lst as text
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't RemoveFromStringStaff: " & eMsg number eNum
		set newText to theText
	end try
	return newText
end RemoveFromStringStaff

on checkContainingText2(theLine)
	repeat with aPhrase in CharOrString
		if theLine contains aPhrase then return true
	end repeat
	return false
end checkContainingText2

Hi,

no offense but you are posting 200 lines of code with the laconic comment “the script doesn’t work”.

Please add at least what the script is supposed to do and what is the expected result of the part which is not working.
The more information you provide the more answers you’ll get.

Here is a commented version.


--global newText # no need for this global variable

set fileText to read (choose file with prompt "Choose a text file:" of type {"eml"})
-- this is the pharse will will check against in the fileText
property CharOrString : {"Department:", "Contact Name:", "Email:", "Budget Code:", "Name:", "Title:", "Tel:", "Fax:", "Web:"}
property dontdeletePhrase : {"Department:", "Contact Name:", "Email:", "Budget Code:", "Name:", "Title:", "Tel:", "Fax:", "Web:"}

---STUDENTS BLOCK---
if fileText contains "Student Introduction Cards" then
	--set dontdeletePhrase to "Answer : "
	--set fileText to deleteLinesFromText(fileText, dontdeletePhrase)
	set fileText to deleteLinesFromText(fileText, "Answer : ")
	--set fileText to newText
	log "Point 1"
	log fileText
	--set dontdeletePhrase to "<span>"
	--set fileText to deleteSpanFromText(fileText, dontdeletePhrase)
	set fileText to deleteSpanFromText(fileText, "<span>")
	--set fileText to newText
	log "Point 2"
	log fileText
	--set CharOrString to "Answer : "
	--set fileText to RemoveFromString(fileText, CharOrString)
	set fileText to RemoveFromString(fileText, "Answer : ")
	--set fileText to result
	log "Point 3"
	log fileText
	--set dontdeletePhrase to "Accepted"
	--set fileText to deleteSpanFromText(fileText, dontdeletePhrase)
	set fileText to deleteSpanFromText(fileText, "Accepted")
	--set fileText to newText
	log "Point 4"
	log fileText
	--set newText to result
	set nameFile to first paragraph of fileText
	set currentDate to short date string of (current date) & "-" & (random number from 1 to 999) & tab
	set tabText to replaceString(fileText, return, tab)
	log "Point 5"
	log tabText
	--set tabText to result
	--replaceString(tabText, return, tab)
	--set tabText to result
	set tabText to currentDate & tabText & tab & "ST_BC_" & nameFile & ".pdf"
	--set tabText to result
	log "Point 6"
	log fileText
	log tabText
	
	# Here you have two chunks of datas:
	# the contents of the variable fileText
	# the contents of the variable tabText
	# I don't know what you want to do with them.
else
	(*
	linesToBeSaved(fileText)
	# Here you had the result of the preceeding instruction in the variable result
	# But this variable was be modified by the next instruction so it's as if you never triggered the preceeding one
	RemoveFromStringStaff(fileText, CharOrString)
	*)
	set data1 to linesToBeSaved(fileText)
	log "Point 7"
	log data1
	set data2 to RemoveFromStringStaff(fileText, CharOrString)
	log "Point 8"
	log data2
	
end if

----**HANDLERS**----

--removes all lines not starting with Match
on deleteLinesFromText(theText, dontdeletePhrase)
	--set newtext to ""
	set aList to {}
	try
		
		set textList to paragraphs of theText
		considering case # I'm not sure that it's what is needed
			--repeat with i from 1 to count of textList
			repeat with thisLine in textList
				--set thisLine to item i of textList
				--	if thisLine contains dontdeletePhrase then
				if thisLine starts with dontdeletePhrase then
					--set newtext to newtext & thisLine & return
					set end of aList to thisLine as text
				end if
			end repeat
		end considering # I'm not sure that it's what is needed
		--if newtext is not "" then set newtext to text 1 thru -2 of newtext
		if aList is not {} then
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to return
			set newtext to aList as text
			set AppleScript's text item delimiters to ASTID
		end if
	on error
		--set newtext to theText
	end try
	return newtext
end deleteLinesFromText



--removes all lines starting with Match
on deleteSpanFromText(theText, dontdeletePhrase)
	--set newtext to ""
	set aList to {}
	try
		
		set textList to paragraphs of theText
		considering case # I'm not sure that it's what is needed
			-- now repeat over the list and ignore everything that dont have the dontdeletePhrase
			--repeat with i from 1 to count of textList
			repeat with thisLine in textList
				--set thisLine to item i of textList
				if thisLine does not contain dontdeletePhrase then
					--set newtext to newtext & thisLine & return
					set end of aList to thisLine as text
				end if
			end repeat
		end considering # I'm not sure that it's what is needed
		--if newtext is not "" then set newtext to text 1 thru -2 of newtext
		if aList is not {} then
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to return
			set newtext to aList as text
			set AppleScript's text item delimiters to ASTID
		end if
	on error
		--set newtext to theText
	end try
	return newtext
end deleteSpanFromText

---remove single word match from string
on RemoveFromString(theText, CharOrString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, CharOrString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			if theText does not contain CharOrString then ¬
				return theText
			set AppleScript's text item delimiters to CharOrString
			set lst to theText's text items
		end considering
		set AppleScript's text item delimiters to ASTID
		return lst as text
	end try
	set newtext to theText's text items
end RemoveFromString

--replace oldstring with newString
on replaceString(theText, oldString, newString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		--considering case
		set AppleScript's text item delimiters to oldString
		considering case
			set lst to every text item of theText
		end considering
		set AppleScript's text item delimiters to newString
		set theText to lst as string
		--end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

on linesToBeSaved(theText)
	--set newtext to ""
	set aList to {}
	try
		
		set textList to paragraphs of theText
		considering case # I'm not sure that it's what is needed
			--repeat with i from 1 to count of textList
			repeat with thisLine in textList
				--set thisLine to item i of textList
				if checkContainingText(thisLine) then
					--set newtext to newtext & thisLine & return
					set end of aList to thisLine as text
				end if
			end repeat
		end considering # I'm not sure that it's what is needed
		--if newtext is not "" then set newtext to text 1 thru -2 of newtext
		if aList is not {} then
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to return
			set newtext to aList as text
			set AppleScript's text item delimiters to ASTID
		end if
	on error
		--set newtext to theText
	end try
	return newtext
end linesToBeSaved

on checkContainingText(theLine)
	repeat with aPhrase in dontdeletePhrase
		if theLine contains aPhrase then return true
	end repeat
	return false
end checkContainingText

--- remove the xxx: on the string
on RemoveFromStringStaff(theText, CharOrString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, CharOrString, lst
	set ASTID to AppleScript's text item delimiters
	try
		--if theText contains CharOrString then return theText
		# You were here if theText didn't contain CharOrString so the instructions below were NEVER executed
		# So I assume that the correct instruction was supposed to be :
		if theText does not contain CharOrString then return theText
		set AppleScript's text item delimiters to CharOrString
		considering case # I'm not sure that it's what is needed
			set lst to theText's text items
		end considering # I'm not sure that it's what is needed
		set AppleScript's text item delimiters to ASTID
		return lst as text
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		set newtext to theText # Here it's executed but is useless because the error doesn't return in the caller code
		error "Can't RemoveFromStringStaff: " & eMsg number eNum
		--set newtext to theText # Here the instruction was never executed
	end try
	--return newtext # never executed too
end RemoveFromStringStaff

on checkContainingText2(theLine)
	repeat with aPhrase in CharOrString
		if theLine contains aPhrase then return true
	end repeat
	return false
end checkContainingText2

I have no text available to test it.
As I inserted 8 log instructions you will be able to see what works and what fails if something continue to fail.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 12 novembre 2016 15:07:33

StefanK
Apologies for the big script, I assume wrongly that those who could program in applescript will find errors by just reading it as someone who can read English could spot a grammatical error even if don’t understand the content.
Yvan
Thanks very much for your comments, I was quite sure they will be a lot of unnecessary lines in the script, your post will help a lot to optimise it…