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
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