How about something simpler:
Set Field [ textmanip::line 1 ; Let ( [
words = WordCount ( Left ( textmanip::text entry ; 45 ) ) ;
over = Length ( LeftWords ( textmanip::text entry ; words ) ) > 45
LeftWords ( textmanip::text entry ; words - over )
Set Field [ textmanip::text entry ; RightWords ( textmanip::text entry ; WordCount ( textmanip::text entry ) - WordCount ( textmanip::line 1 ) ) ]
Note that any punctuation at the split points will be lost (as you know from your previous thread).
thanks for the response, your script makes sense to me, but unfortunately I need to somehow preserve all punctuation while I do this. I've been fooling with Left and trying to figure out a way to get Position to return a correct position including punctuation. I've been trying to apply Position when I'm trying to copy a certain number of words out of a field that comprise no more than 45 characters including punctuation. I started with a loop that looked a little like this:
set loopcount = 0
if(Length(Leftwords(text, loopcount)) > 45
set field receiver too Left(text; Position(text; Leftwords(text;loopcount -1);1;1)
exit loop if(length(leftwords(text, loopcount)) >45
loopcount = loopcount +1
Obviously I don't have the right syntax, but this gives you an idea of what I'm trying to do. The problem I'm having in my head is how do I include punctuation. For example, if my text is = "alpha beta delta!" and in Position I use length with leftwords to get the number of characters to use with my left function, it's conceivable that I could get "alpha beta delta" and then in the next line "! and whatever is next" I need to keep sentences together punctuation-wise. Somehow I'm just having a brain block with how to do this. Can you or anyone give me another nudge in the right direction?
Unfortunately, I don't have a universal solution for this, because punctuation is punctuation, and unless you are looking at it, it's hard to say whether it belongs to the previous word or the next one. As I already suggested in the other thread, a space between the words could be used as a reliable guide - provided there IS a space somewhere betwen the words. If you can be confident that such space will always be there, then you can find it and break the line exactly at that position.
BTW, I am puzzled why you need to do this at all. It was only when I re-read the other thread, that I realized you're into subtitles. I believe any decent subtitling application will accept subtitles separated by a double carriage return, and break them up into lines automatically.
Ironically, I've been working with several subtitling programs, and although one does this function (wraps the text based on line length so the two subtitle lines are balanced), it doesn't allow export of an stl text file with the carriage returns included. It only allows export of a TIFF based solution which isn't what I need. The other one has regular word wrap, but it ends up making the subtitles very unbalanced. The process of making them balanced is mostly manual and laborious and time intensive. Not good for the wrists. The programs I have access to and have been using don't do this in a satisfactory manner.
So there's no way to include trailing punctuation (i.e. if a word ends with a period there's no way to reliably break either before that word or after the period, or question mark or exclamation point etc.?) Is there any way to know if a whole word has been selected? I was thinking about a more brute force approach where one uses a loop set selection and copy and paste to a strict validation field to use a trial and error method of copying and pasting words into a destination field. If the paste goes over 45 chars, an error is thrown and we step back one character unless that divides a word. Again not sure if there's any way to do that. . . thanks for your continued help
Well, it's been a while, so my recollection may be mistaken. I always did my breaking manually anyway, to make it easier to read.
Back to the subject: it is certainly possible to include all punctuation, leading or trailing. The problem is telling the two apart. If all you have between two words are punctuation marks with no space, it takes a human (a literate one) to make the call. IMHO, you will very rarely encounter such situation in spoken word, so you could just go with the splitPoint being =
Position ( text ; " " ; 46 ; - 1 )
the top line =
Left ( text ; splitPoint - 1 )
and the remaining rext =
Right ( text ; Length ( text ) - splitPoint )
thanks a lot, that worked perfectly!