Searching a text field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rumplestiltskin
Posts: 222
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

Searching a text field

Post by rumplestiltskin » Sat Aug 01, 2020 11:43 pm

I have a stack with a field into which I drop a file from the Desktop (Windows Desktop in this case). The file is an eMail I've dragged to the Desktop from Thunderbird. No problem getting the contents of the file to appear in the field. I then need to search for the "Subject:" line of that eMail which may appear on any line (line 24, line 85, etc.). Once found (and I assume it would be selected by the find command), my intent is to replace the selectedText of that Subject line with something else of the user's choosing.

I know the following consistencies:
- The Subject line of the eMail always has the text "Subject: " at the start of the line (no quotes).
- The length of the Subject line is variable but always ends with a Return (at least that's what I see when I paste the eMail's text into LibreOffice so I may see formatting).
- The word "Subject:" (both in upper and lower case, no quotes) may appear multiple times in the eMail's header info but these are to be ignored if they do not appear at the start of a line. Likewise, any appearance after the first time it's at the -start- of a line is irrelevant and is ignored.

Once the user makes a change, the contents of the field are written back out to the file. I know how to do that.

So this seems to require some sort of find command that is part regular expression(?). Advice welcomed.

Thanks,
Barry

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Searching a text field

Post by jmburnod » Sun Aug 02, 2020 12:25 am

Hi Barry,
If i understand you correctly you may use offset to check each line of the text.
Best regards
Jean-Marc
https://alternatic.ch

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Searching a text field

Post by Thierry » Sun Aug 02, 2020 8:30 am

Hi Barry,

Yo could try this if you wish,
and then adapt this script to your needs:

Code: Select all

   put "(?m)^Subject:(.*?)$" into theRegex
   
   -- get the position of the subject's text in between p0 and p1:
   if matchChunk(emailText, theRegex, p0, p1) then
      -- replace your subject with another string:
      put " *** THE NEW SUBJECT ***" into char p0 to p1 of emailText
      put emailText
   end if
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Searching a text field

Post by AxWald » Sun Aug 02, 2020 10:54 am

Hi,

alternative way:

Code: Select all

   put fld "email_fld" into myData           --  get the email
   put "Subject: " into mySStrg              --  define what we search for
   put "I'm the new subject" into myNewSub   --  define the replacement
   put lineOffset(mySStrg,MyData) into myFoundLine  --  find the line!
   if myFoundLine <> 0 then                  --  line found, replace
      put myNewSub into char len(mySStrg)+1 to -1 of line myFoundLine of myData
      put myData into fld "email_fld"        --  write back
   else                                      --  Arrgghh, no subject!
      answer error "No subject in this mail!"
   end if
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Searching a text field

Post by FourthWorld » Sun Aug 02, 2020 3:27 pm

Are the email files reliably in mbox format, or something else?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

rumplestiltskin
Posts: 222
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

Re: Searching a text field

Post by rumplestiltskin » Sun Aug 02, 2020 11:12 pm

Big Thank you! to all who responded. I tried Thierry's approach as it seemed to take into account the fact that the word "Subject:" for which I was searching was only a relevant find if it appeared at the start of the line. I modified the provided script (ever so slightly to personalize it for the name of the target field) and it works perfectly. I'll now add the bits of code needed to permit the user to specify the new Subject text and then write the field back out to a file and see what it looks like when it's dragged back into the eMail app.

Some history: There used to be a Thunderbird extension that permitted one to alter the Subject line of an eMail (which one might do for organizational purposes). However, that extension is no longer compatible with Tbird 73 and, in any case, the change was only written to the local Tbird database. View that eMail on another device and the original Subject appeared. So what I'm doing here is actually changing the Subject line of the eMail and then dragging that eMail back to the relevant folder where the new Subject line appears (and appears regardless of the device you view it on because the new Subject is part of the eMail). I'd then simply delete the original eMail (which is now just a copy with an old Subject line and no longer needed).

I'll note that, in Apple Mail, one may drag an incoming IMAP eMail to the Drafts folder, edit it, and then drag it back to the original Inbox (or any other desired folder). This actually did what I want to do here (but I believe doing this with the "Apple Mail Drafts" method changes the eMail date, IIRC).

Richard has asked whether the eMail is in mbox format. In my Tbird profile folder I see a folder with a ".sbd" extension and, inside of there, are ".sbd" files (very small) and large files with the same names but without the ".sbd" that appear to be where the eMails are contained. So what I'm doing is working only with one eMail at a time (one that I drag to the Desktop that appears as a ".eml"). I'm not actually changing the original eMail (as I've read that mucking about with an eMail header while it resides in an IMAP mailbox is asking for a poke in the eye with a sharp stick).

Thanks again,
Barry

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Searching a text field

Post by FourthWorld » Mon Aug 03, 2020 1:44 am

Thanks, Barry. SBD files use the mbox format so the following may be useful:

In mbox, each message begins with "From ". Note the trailing space. When you see a new line that begins with those five characters you know you have the start of an email.

This is useful because AFAIK it's the only string enforced to be uniquely dedicated to that role as a message delimiter.

If you write a line in the body of an email beginning with "From ", you'll find that it gets pretended in storage with a space, so it can't be confused with a message delimiter.

So when parsing mbox, one found it's useful to do it two steps at least with any file that may conceivably contain more than one message:

First, do an offset (cr&"From ", cr&SomeEmailData)

That will get you to the beginning of the next email in the data.

Then do an offset to whatever else you're looking for to distinguish the email (in your case a particular subject line).

If you need to loop through multiple emails you can loop through them, deleting what you've found so your offset will get you to the next one next time though the loop.

You can also use the optional third param to offset to set a start position, but in practice with larger mbox archives I found it slightly more efficient to delete as I go.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

rumplestiltskin
Posts: 222
Joined: Wed Jun 21, 2006 7:33 pm
Location: West of the Pecos
Contact:

Re: Searching a text field

Post by rumplestiltskin » Mon Aug 03, 2020 4:58 am

Richard, Thanks for the detailed mbox explanation. I'm thinking that mucking about in the mbox file is substantially above my pay grade. :lol:

This specific task I am attempting may be unnecessary, as it turns out. As I was examining the Add-on "EditEmailSubject MX" which I have installed in TBird, I located an option checkbox which, when enabled, writes only to the local database. It was checked by default. I decided to be brave and uncheck it. Then I used the "Edit Subject" menu item (which the Add-on installed) to change the subject line and watched and waited for a number of seconds for any result (half-expecting smoke...). I saw the eMail duplicated, then saw the duplicate with the changed Subject line, then saw the original eMail being deleted. And that seems to be exactly the sort of behavior that my process would have done:

1. Drag the eMail to the desktop.
2. Use my app to find the Subject line and change it.
3. Drag the changed eMail back into the IMAP inbox in the TBird window.
4. Delete the old eMail.

I'm going to complete my app and post it back up here just in case it might prove useful. Certainly it's a great tool for learning. Now that I'm just about retired, I'll concentrate more on LC than I had done in the past.

Thanks again to all.

JohnEevans
Posts: 2
Joined: Tue Oct 06, 2020 7:38 am

Re: Searching a text field

Post by JohnEevans » Fri Oct 30, 2020 3:03 pm

Good advice. Was helpful for me too. Thanks guys.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Searching a text field

Post by FourthWorld » Fri Oct 30, 2020 5:02 pm

How John -

Learning the workflows behind email parsing is an interest of mine, informing some of the tools I'm working on.

May I ask how this thread was useful for what you're making with LiveCode?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Talking LiveCode”