Hello
I’m using a handler from Bratis website to remove some text from a string; It works fine (I can see the result in the applescript editor) however I’m unable to get the result into a new variable to keep working on the string.
I made the variable theText global hoping the value will be changed after being processed by the handler but if I run the script with “return newText” uncommented I don’t get the result of the handler but the previous one.
How can I update the variable newText?
Thanks for your help
--this removes the string "Answer : " from the text
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
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't RemoveFromString: " & eMsg number eNum
end try
set newText to theText's text items
end RemoveFromString
RemoveFromString(newText, "Answer : ")
--return newText
Thank you Stefan
I got a new issue and it’s probably not worth to open a new post:
I’m trying to convert this script which works fine with just one parameter (dontdeletePhrase to “match1”) to work with a list but is returning an empty string. What do I need to do to make it work?
I’m trying to keep these basic questions to a minimum but sometimes I get stuck and no search can help me and the deadline is getting closer
Thanks again
set fileText to read (choose file with prompt "Choose a text file:" of type {"eml"})
set dontdeletePhrase to {"match1","match2","match3"}
deleteLinesFromText(fileText, dontdeletePhrase)
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
You need a repeat loop to check all items of the list, I recommend to create an extra handler which returns a boolean value.
dontdeletePhrase is declared as a property that avoids passing the list as a parameter.
property dontdeletePhrase : {"match1", "match2", "match3"}
set fileText to read (choose file with prompt "Choose a text file:" of type {"eml"})
deleteLinesFromText(fileText)
on deleteLinesFromText(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 deleteLinesFromText
on checkContainingText(theLine)
repeat with aPhrase in dontdeletePhrase
if theLine contains aPhrase then return true
end repeat
return false
end checkContainingText
Just out of curiosity, what’s wrong with passing the list as parameter? I think it’s cleaner especially when the handler is used with different files and different conditions.
set dontDeletePhrase to {"match 1", "match 2", "match 3"}
deleteLinesFromText(fileText1)
set dontDeletePhrase to {"match 4", "match 5", "match 6"}
deleteLinesFromText(fileText2)
set dontDeletePhrase to {"match 7", "match 8", "match 9"}
deleteLinesFromText(fileText3)