Title: Re: Changing margins [Script: long]
Yes, indeed, as David says: If you want lines
SHORTER than the "standard" length, you can
insert returns or use a script to do that. I've
written such a script, and I just realized I have
never shared it with this list, so I will do so
now (see below). I've plagiarized code for the
unwrap and hard wrap subroutines from multiple
sources, I forget who; I think Paul Berkowitz did
this particular unwrap routine, although I've
written several myself in the past. I'll use my
script on this message before I send it, so you
can see the result, giving it a maximum line
length of 50. I will do the rewrapping <before> I
insert my script, however, because I do not want
the script to be hard-wrapped! (Script Editor
does not like all those extra line breaks.) After
inserting the script I will turn the message into
HTML format because this <preserves> the longer
lines in the script text.

The hard-wrapping affects only new text lines,
lines you write. That is, anything that is quoted
(lines starting with ">") will NOT be rewrapped.

However, David please note: There is no way
currently in Entourage to send lines LONGER than
the length Entourage chooses to wrap at. That's
why several of us have pleaded for "Format
flowed" or whatever it is called, that simply
does not break lines, but passes them along as
the author writes them.

Here is my script. Call it "Hard Wrap Text", copy
it to script editor, and then save the result in
your Entourage Script Menu Items folder.

Couple of further remarks on the script: It will
work on incoming messages only if you first
select "Edit Message" from the Message menu, or
click on the Edit button in the toolbar. It will
operate on the entire message content <unless>
you first select some text; then it operates only
on the selection.

--AppleScript begins
tell application "Microsoft Entourage" --
    if not my CheckMessageWindow() then return
    set theWindow to the front window
    set theText to the selection of the theWindow
    set ReplaceAll to false
    if theText is "" then
        set theText to the content of the theWindow
        set ReplaceAll to true
    end if
    set theText to my unwrap(theText)
    display dialog "What line length?" buttons {"40", "72", "Custom length"}
    set lineLen to button returned of result
    if lineLen is "Custom length" then set lineLen to text returned of (display dialog "Enter desired length." default answer "60")
    set lineLen to lineLen as number
    if lineLen = 0 or lineLen > 1024 then
        display dialog "Illegal line length:" & lineLen buttons {"Cancel"} giving up after 5
        return
    end if
    set theText to my hardwrap(theText, lineLen)
    try
        if ReplaceAll then
            set the content of theWindow to theText
        else
            set the selection to theText
        end if
    on error
        display dialog "Can't replace text; if this is an incoming message, please select 'Edit Message' and try the script again."
    end try
end tell --
on CheckMessageWindow()
    tell application "Microsoft Entourage"
        set msgClass to the class of the front window
        if msgClass is not draft window and msgClass is not message window then
            display dialog "This script is designed to work with a message window frontmost." buttons {"Okay"} with icon stop
            return false
        end if
    end tell
    return true
end CheckMessageWindow

on hardwrap(t, lineLen)
    set temp to every paragraph of t
    set newx to ""
    repeat with oneP in temp
        set oneP to oneP as string
        if oneP = "" then -- Blank paragraph
            set newx to newx & return
        else if oneP begins with ">" then -- Don't wrap quoted lines
            set newx to newx & oneP & return
        else
            repeat while (length of oneP) > 0
                if ((length of oneP) is less than or equal to lineLen) then
                    set l to oneP
                    set oneP to ""
                else
                    set l to text 1 thru lineLen of oneP
                    set savedel to AppleScript's text item delimiters
                    set AppleScript's text item delimiters to " "
                    set lineList to text items of l
                    --If we encounter a "word" longer than lineLen, pass it unbroken
                    if (count lineList) > 1 then -- We can break the line
                        set l to items 1 thru -2 of lineList
                        set oneP to item -1 of lineList & text (lineLen + 1) thru -1 of oneP
                    else -- Cannot break
                        set l to oneP
                        set tempLen to length of l
                        set lineList to text items of l
                        if (count lineList) > 1 then -- Break at first possible point
                            set l to item 1 of lineList
                            set oneP to (text items 2 thru -1 of lineList) as text
                        else -- No break in this line
                            set l to item 1 of lineList
                            set oneP to ""
                        end if
                    end if
                end if
                set newx to newx & l & return
            end repeat
            --        set newx to newx & return
        end if
    end repeat
end hardwrap
(*
Unwrap Handler (Allen Watson <[EMAIL PROTECTED]>, July 3, 2000)

A routine you can use to remove hard line endings from text files.
Recognizes two returns in succession as a paragraph break.
Does not alter lines that begin with an email "quote" character (>).
To use this handler, get your text into a variable such as thisText, and then

set thisText to my unwrap(thisText)

*)


on unwrap(inString)
    if inString is "" then
        return inString
    end if
    set theLines to the paragraphs of inString
    set theNewText to ""
    set theLast to 0
    repeat with theLine in theLines
        set theLength to the length of theLine
        if theLength is not 0 then
            if first character of theLine is ">" then
                set theLast to 3
            end if
        end if
        if theLast is 0 then
            set theNewText to theNewText & theLine
        else if theLast is 1 then
            set theNewText to theNewText & return & return & theLine
        else if theLast is 3 then
            set theNewText to theNewText & return & theLine
        else if theLength is not 0 then
            set theNewText to theNewText & " " & theLine
        end if
        if theLength is 0 then
            set theLast to 1
        else
            set theLast to 2
        end if
    end repeat
    repeat while theNewText contains "  "
        set theNewText to my replaceText("  ", " ", theNewText)
    end repeat
    return theNewText & return
    
end unwrap

on replaceText(lookingFor, replacingWith, inString)
    set olddelis to AppleScript's text item delimiters
    
    set AppleScript's text item delimiters to lookingFor
    set theList to (every text item of inString)
    
    set AppleScript's text item delimiters to replacingWith
    set inString to theList as string
    
    set AppleScript's text item delimiters to olddelis
    
    return inString
end replaceText


--Applescript ends

On or near 7/19/01 3:59 PM, Adam Boettiger at
[EMAIL PROTECTED] observed:
> Allen wrote:
>> something like that. I suspect, though, that you (or others) would also like
>> to be able to use <shorter> line lengths...am I right?
>
> Well...yes. Er, is it me or was it rather presumptuous of MS to make that
> decision for us? <G> Yes, there are standards, but as you point out, there
> are valid reasons for wanting continuous text (commands) and/or shorter
> margins/widths. I moderate a few discussion lists and like the option of
> shorter margins because it makes the material easier for readers to skim.
>
> Thanks Allen. Maybe in the next version of ER.
>
> AB
>
>
>

-- Add me to Palm/Visor:
http://signature.coola.com/?[EMAIL PROTECTED]
Allen Watson <[EMAIL PROTECTED]> XNS name:
=Allen Watson A Mac family since 1984
<http://home.earthlink.net/~allenwatson/>
Applescripts for Outlook Express and Entourage:
<http://homepage.mac.com/allenwatson/>

Reply via email to