Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I write an applescript to match strings in two textedit documents?

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

Posted on Nov 19, 2012 1:24 PM

Reply
12 replies

Nov 19, 2012 1:59 PM in response to nick_harambee

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

writeeditedTexttofp

close accessfp


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

Nov 19, 2012 2:10 PM in response to twtwtw

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

Nov 19, 2012 3:04 PM in response to nick_harambee

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 openfileA

set document2 to openfileB

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

Nov 19, 2012 3:10 PM in response to Pierre L.

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 toevery paragraph of (do shell script"cat " & quoted form of longFile)

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

set newLong to {}

repeatwith anEmail in longList

set anEmail to contents of anEmail

if anEmail isnotin shortList thensetendof newLong to anEmail & return

endrepeat

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

Nov 23, 2012 9:08 AM in response to nick_harambee

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 openfileA

set document2 to openfileB

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

end repeat



Message was edited by: Pierre L.

How do I write an applescript to match strings in two textedit documents?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.