Applescript: clear line

I have this line that has illegal characters that I want to remove, but I don't know what characters might be present.

I created a list of characters that I want not to filter, and I built this script (from another one I found on the internet).

on clean_string(TheString)
    --Store the current TIDs. To be polite to other scripts.
    set previousDelimiter to AppleScript text item delimiters
    set potentialName to TheString
    set legalName to {}
    set legalCharacters to {"a", "b", "c", "d", "e", "f", 
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
 "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
 "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5",
 "6", "7", "8", "9", "0", "?", "+", "-", "Ç", "ç", "á", "Á", "é",
 "É", "í", "Í", "ó", "Ó", "ú", "Ú", "â", "Â", "ã", "Ã", "ñ", "Ñ",
 "õ", "Õ", "à", "À", "è", "È", "ü", "Ü", "ö", "Ö", "!", "$", "%",
 "/", "(", ")", "&", "€", "#", "@", "=", "*", "+", "-", ",", ".",
 "–", "_", " ", ":", ";", ASCII character 10, ASCII character 13}

    --Whatever you want to eliminate.
    --Now iterate through the characters checking them.
    repeat with thisCharacter in the characters of potentialName
        set thisCharacter to thisCharacter as text
        if thisCharacter is in legalCharacters then
            set the end of legalName to thisCharacter
            log (legalName as string)

        end if
    end repeat
    --Make sure that you set the TIDs before making the
    --list of characters into a string.
    set AppleScript text item delimiters to ""
    --Check the name length.
    if length of legalName is greater than 32 then
        set legalName to items 1 thru 32 of legalName as text
    else
        set legalName to legalName as text
    end if
    --Restore the current TIDs. To be polite to other scripts.
    set AppleScript text item delimiters to previousDelimiter
    return legalName
end clean_string

      

The problem is that this script is slow as hell and gives me a wait time.

What I am doing is character by character validation and comparison with a list of legalCharacters. If the character is there, that's okay. If not, ignore.

Is there a quick way to do this?

sort of

"look at each char TheString and remove the ones that are not on legalCharacters"

?

thanks for any help.

+2


a source to share


4 answers


What non-ascii characters are you using? What is the encoding of your file?

It is much more efficient to use a shell script and tr, sed or perl to process the text. All languages ​​are installed by default on OS X.

You can use a shell script with tr (like the example below) to undo the return, and you can also use sed to separate spaces (not in the example below):

set clean_text to do shell script "echo " & quoted form of the_string & "| tr -d '\\r\\n' "

      



Tech Note TN2065: Make a wrapper script in AppleScript

Or, with perl, this will strip non-printable characters:

set x to quoted form of "Sample text. smdm#$%%&"
set y to do shell script "echo " & x & " | perl -pe 's/[^[:alnum:]|[:space:]]//g'"

      

Search around SO for other examples of using tr, sed and perl to process text with Applescript. Or search for MacScripter / AppleScript | Forums

+3


a source


Iteration in Applescript is always slow and there really isn't a faster way around these problems. Entering loops is an absolutely guaranteed way to slow things down. Use the log command wisely.

In your particular case, however, you have a length constraint, and moving the length checker into a repeat loop will potentially significantly reduce processing time (just under a second to run in Script Debugger regardless of text length):



    on clean_string(TheString)
     set potentialName to TheString
     set legalName to {}
     set legalCharacters to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "?", "+", "-", "Ç", "ç", "á", "Á", "é", "É", "í", "Í", "ó", "Ó", "ú", "Ú", "â", "Â", "ã", "Ã", "ñ", "Ñ", "õ", "Õ", "à", "À", "è", "È", "ü", "Ü", "ö", "Ö", "!", "$", "%", "/", "(", ")", "&", "€", "#", "@", "=", "*", "+", "-", ",", ".", "–", "_", " ", ":", ";", ASCII character 10, ASCII character 13}
 with timeout of 86400 seconds --86400 seconds = 24 hours

     repeat with thisCharacter in the characters of potentialName
      set thisCharacter to thisCharacter as text
      if thisCharacter is in legalCharacters then
       set the end of legalName to thisCharacter
       if length of legalName is greater than 32 then
        return legalName as text
       end if
      end if
     end repeat
 end timeout
     return legalName as text
    end clean_string

      

+2


a source


Another shell script method could be:

set clean_text to do shell script "echo " & quoted form of the_string & "|sed \"s/[^[:alnum:][:space:]]//g\""

      

which uses sed to remove anything that is not an alphanumeric character, or a space. More regex reference here

+2


a source


BBEdit or TextWrangler will be much, much faster. Download TextWrangler (free) then open your file and run Text -> Zap Gremlins ... on it. Does it do what you need it to do? If so, celebrate with a cold drink. If not, try BBEdit (it's not free) and create a new Factory text with as many Replace All conditions as you like, then open the file and run the Factory text on it.

0


a source







All Articles