12 Replies Latest reply: Nov 23, 2012 9:08 AM by Pierre L.
nick_harambee Level 1 (5 points)

Hi,

 

Here is my scenario.  I have two TextEdit documents which each contain a series of email addresses, with one email address per line.  File A has a very long list of email address.  File B has a shorter list.  For each email address in File A I would like the script to check if the same email address exists in File B, and if it does, delete it from File A.

 

Would it be straightforward to do this with applescript, and if so could someone help get me started?

 

Thanks,

 

Nick


Macbook Pro, Mac OS X (10.7), 2.66 GHz Intel Core i7 8GB RAM
  • twtwtw Level 5 (4,925 points)

    set listA to paragraphs of (read "/path/to/fileA")

    set listB to paragraphs of (read "/path/to/fileB")

     

    set editedList to {}

    repeat with thisEmailAddress in listA

              if thisEmailAddress is not in listB then

                        set end of editedList to thisEmailAddress

              end if

    end repeat

    set {oldTID, my text item delimiters} to {my text item delimiters, return}

    set editedText to editedList as text

    set my text item delimiters to oldTID

     

    set fp to open for access "/path/to/outputFile/" with write permission

    write editedText to fp

    close access fp

     

    There are some things you could do to optimize it, but unless your list has thousands of entries this should do well enough.

  • nick_harambee Level 1 (5 points)

    Thanks.

     

    I ran a test of your script as follows:

     

    File A

     

    test1@me.com

    test2@me.com

    test3@me.com

     

    File B

     

    test1@me.com

     

    What I would was hoping for was a new file containing:

     

    test2@me.com

    test3@me.com

     

    What I got was the following error:

     

    error "Can’t make {item 2 of {\"test1@me.com\", \"test2@me.com\", \"test3@me.com\"}, item 3 of {\"test1@me.com\", \"test2@me.com\", \"test3@me.com\"}} into type text." number -1700 from {item 2 of {"test1@me.com", "test2@me.com", "test3@me.com"}, item 3 of {"test1@me.com", "test2@me.com", "test3@me.com"}} to text

  • Pierre L. Level 5 (4,235 points)

    Try this:

     

    set fileA to POSIX file "/Path/to/File A" as alias

    set fileB to POSIX file "/Path/to/File B" as alias

     

    tell application "TextEdit"

        set document1 to open fileA

        set document2 to open fileB

    end tell

     

    set theLongList to paragraphs of document1

    set theShortList to paragraphs of document2

     

    set theNumbers to {}

    repeat with k from 1 to (count items of theLongList)

        set thisParagraph to item k of theLongList

        if (length of thisParagraph > 1) and (thisParagraph is in theShortList) then

            copy k to the end of theNumbers

        end if

    end repeat

     

    repeat with k in reverse of theNumbers

        tell document1 to delete paragraph k

    end repeat


  • nick_harambee Level 1 (5 points)

    Hi Pierre,

     

    I tried your script, but it didn't remove any email addresses.

     

    I now have the following script working:

     

    set longFile to POSIX path of ((path to desktop as text) & "long.txt")

    set newLongFile to POSIX path of ((path to desktop as text) & "newLong.txt")

    set shortFile to POSIX path of ((path to desktop as text) & "short.txt")


    set longList to every paragraph of (do shell script "cat " & quoted form of longFile)

    set shortList to every paragraph of (do shell script "cat " & quoted form of shortFile)


    set newLong to {}

    repeat with anEmail in longList

       set anEmail to contents of anEmail

       if anEmail is not in shortList then set end of newLong to anEmail & return

    end repeat


    do shell script "echo " & quoted form of (newLong as text) & " > " & quoted form of newLongFile

  • Pierre L. Level 5 (4,235 points)

    Have you replaced "/Path/to/File A" and "/Path/to/File B" with real paths, like for example "/Users/pierre/Desktop/File A.rtf" and "/Users/pierre/Desktop/File A.rtf"?

     

    Since the script works flawlessly on my computer, I can't understand why it doesn't work on yours.

  • nick_harambee Level 1 (5 points)

    Yes I did.  I will test it again in a while.  I have to pop out now.

  • Pierre L. Level 5 (4,235 points)

    Something else… You wrote “with one email address per line”. Am I right to assume that you were actually meaning “one email address per paragraph"?

  • nick_harambee Level 1 (5 points)

    What is the difference?  I enter one email and press return then I enter another - is this a new paragraph or a new line?

  • Pierre L. Level 5 (4,235 points)

    Each time you press Return, you create a new paragraph. Some paragraphs have just one line, others have many lines. To create a new line without creating a new paragraph, you just have to press Control+Return. When you triple click on any line of a multi-line paragraph, you select the whole paragraph.

  • Pierre L. Level 5 (4,235 points)

    Last question: was I right to assume that each paragraph of both files A and B did not contain anything else than an email address, like this:

     

    Capture d’écran 2012-11-19 à 7.15.27 PM.png

  • nick_harambee Level 1 (5 points)

    Yes, that is correct.

  • Pierre L. Level 5 (4,235 points)

    I understand that you now have a script working. Nevertheless, would you be kind enough to try the following version of my previous script. If this one works, I think I'll be able to tell you why the previous one didn't work on your computer.

     

    set fileA to POSIX file "/Path/to/File A" as alias

    set fileB to POSIX file "/Path/to/File B" as alias

     

    tell application "TextEdit"

        set document1 to open fileA

        set document2 to open fileB

    end tell

     

    set theLongList to paragraphs of document1 as text

    set theShortList to paragraphs of document2 as text

    set theNumbers to {}

    repeat with k from 1 to (count paragraphs of theLongList)

        set thisParagraph to paragraph k of theLongList

        if (length of thisParagraph > 1) and (thisParagraph is in theShortList) then

            copy k to the end of theNumbers

        end if

    end repeat

     

    repeat with k in reverse of theNumbers

        tell document1 to delete paragraph k

    end repeat

     

     

    Message was edited by: Pierre L.