The ancient Text-Edit Plus (no longer compatible with modern MacOS) was a relatively easy text editor to automate with AppleScript, with loads of support scripts available courtesy of the late Douglas Adams. I long for another scriptable text editor suitable for someone (me) who is pretty clueless at scripting and who is bound to need help once I have such a text editor. I need one where I can script text manipulation — mainly taking info from HTML tables and converting it into normal text. It would be able to remove dates and format a very few words bold-face, ideally being able to add color to some, and would have line-breaks at predictable points. Understandable wild cards would be great.
BB-Edit looks powerful but difficult to this scripting idiot ($ signs that mean something other than $ signs, serious-looking UI, etc.). I believe Jedit is scriptable, as are KotEditor and Text-Edit but I’m not sure about online support for them.
Any thoughts would be much appreciated!
BBEdit is very scriptable and seems to be the only app these days (at least among those I’ve used) which is recordable. Using it to search and replace text becomes really easy once you get the hang of it as you can record a regular expression search and it will generate the correct applescript syntax.
It was this functionality that finally got me to try and create my own applescripts rather than just copy them. I couldn’t get omnioutliner to deal with inconsistent html so I ended up using bbedit, first to manually run regex searches on text and then to script them. I would use it to clean up and rearrange lists I’d copied from wikipedia (for example) and then deposit the resulting text in omnioutliner (where, over time, I learned how to automatically format the text). I used bbedit for the regex stuff because it came with a great and coherent guide on regular expressions and its search dialogue is easy to use. Eventually I learned how to do all of the work in omnioutliner.
FWIW, BBEdit’s UI is also pretty flexible and you can hide a lot of the advanced stuff. And of course, it has a free version that is really good. For myself, eventually I bought the app
The challenge for you though is that bbedit is a plain text editor, unlike Text-Edit Plus —which works with rich text— so no bold text and no coloured text.
If you learn a few things in applescript, like how to find dates in a text and remove them, then you could likely just go with textedit.
Thanks for your thoughtful reply, Mockman. From what you tell me, I could do most manipulation in BBEdit and then hand the result on the TextEdit (the intended destination) for formatting. I must say I did seek help with BBEdit on some site and found the well-intended help so far beyond me it was like trying to understand ancient Greek. Others have suggested avoiding scripting in TextEdit, not sure why but possibly it’s considered too basic (but not Basic😉). I’ll poke around in both of them.
I use CotEditor. It is free and scriptable.
You could do most manipulation within textedit and complex (or all) manipulation in bbedit. That said, it’s all speculation as only you know what kind of tasks you have in mind.
Consider posting a specific question and see what kind of responses it garners.
Thanks Piyomaru and Mockman for your thoughts. I shall indeed, as Mockman suggests, post details soon about what I’m after. I’m hoping there’s a way to post attachments although I don’t see one.
I wonder if I’m breaking site etiquette by asking for too much but here goes … I want to get a text formatting result from a website HTML table that, after manipulation in Applescript, looks like this:
AMZN Evercore ISIMark Mahaney29 BUY $240
BTO Bank of America SecuritiesLawson Winder2,193 BUY —
LNR ScotiabankJonathan Goldman2,465 HOLD $77
QBR.B CIBCStephanie Price704 BUY $39
I can get the above result with Applescript in the old Tex-Edit Plus but it doesn’t run on a modern MacOS. The tickers are in green, retained from the original website, HOLD is reformatted to bold and blue (SELL and TENDER to bold in red). The spacing isn’t ideal but it’s good enough. I do like spaces between ratings and price targets. I’m starting with text from a website table that, when pasted straight into TextEdit, looks like this:
28 Oct 2024
AMZN
Evercore ISIMark Mahaney29BUY$240.0028 Oct 2024
LNR
ScotiabankJonathan Goldman2,465HOLD$77.0028 Oct 2024
QBR.B
CIBCStephanie Price704BUY$39.0028 Oct 2024
BTO
Bank of America SecuritiesLawson Winder2,193BUY-?-?-
— The tickers retain their green.
The same info looks like this when pasted straight into BBEdit:
28 Oct 2024
AMZN
Evercore ISI
Mark Mahaney
29
BUY
$240.00
28 Oct 2024
LNR
Scotiabank
Jonathan Goldman
2,465
HOLD
$77.00
28 Oct 2024
QBR.B
CIBC
Stephanie Price
704
BUY
$39.00
28 Oct 2024
BTO
Bank of America Securities
Lawson Winder
2,193
BUY
BBEdit decides there are more line/paragraph endings than Tex-Edit Plus does, presumably interpreting HTML differently. It also removes the green formatting of the tickers.
I would paste the Applescript-formatted result into a TextEdit file. Sorry for the name confusion — TextEdit, Tex-Edit Plus — two entirely different programs.
In my Applescript version, note the removal of dates, removal of .00 endings on dollar amounts, spaces inserted between BUY and $x.xx, HOLD formatted blue boldface (and SELL and TENDER formatted red boldface), color of ticker symbols left as green, and all formatted to 13-point Arial, finally with alphabetization by ticker symbols. The result should also be able to run without stumbling on such odd Canadian tickers as QBR.B and GRT.UN. I don’t want the result in table format, which is harder to edit — just lines each showing ticker, institution, analyst, analyst ranking, stock rating and price target.
I think a scriptable substitute for Tex-Edit Plus would need to handle text formatting and be able to use wildcards or other pattern recognition(?). I think BBEdit treats $-signs as coding, which throws me but someone smarter could presumably make it see $ as text.
Ideally the code would be as simple as possible for a novice to follow, so not with conditionals.
The text editors / word processors I have are BBEdit, CotEditor, Jedit Ω, Pages, Text-Edit and Word. I would get another if need be. The only one I’ve made sense of was Tex-Edit Plus, my failing.
I include my Tex-Edit Plus Applescript to show how I approached it logistically. A real programmer would doubtless make something much more refined but this one has been reliable on MacOs up to Mojave.
tell application "Tex-Edit Plus"
activate
make new window
paste
select window 1
replace window 1 looking for "
" replacing with "—" with cases matching -- removes blank lines
replace window 1 looking for "
" replacing with " " with cases matching
replace window 1 looking for "—" replacing with "
" with cases matching -- remove sblank lines again (This 2nd step is necessary but I don't remember why!)
replace window 1 looking for "BUY$" replacing with " BUY $" with cases matching
replace window 1 looking for "BUY-?-?-" replacing with " BUY —" with cases matching
-- Text-Edit Plus pastes "—" as "-?-?-". This line forces "—"
replace window 1 looking for "HOLD$" replacing with " HOLD $" with cases matching
replace window 1 looking for "HOLD-?-?-" replacing with " HOLD —" with cases matching
replace window 1 looking for "SELL$" replacing with " SELL $" with cases matching
replace window 1 looking for "SELL-?-?-" replacing with " SELL —" with cases matching
replace window 1 looking for "TENDER$" replacing with " TENDER $" with cases matching
replace window 1 looking for "TENDER-?-?-" replacing with " TENDER —" with cases matching
replace window 1 looking for "^?^? ^?^?^?. 20^?^?" replacing with "" with cases matching
replace window 1 looking for ".00" replacing with "" with cases matching -- strips cents when .00
replace window 1 looking for "HOLD" replacing with "HOLD" replacing with styles {style:{bold}, color:blue}
replace window 1 looking for "SELL" replacing with "SELL" replacing with styles {style:{bold}, color:red}
replace window 1 looking for "TENDER" replacing with "TENDER" replacing with styles {style:{bold}, color:blue}
select window 1
select contents of window 1
sort selection -- alphabetizes by ticker
set size of selection to 13
set font of selection to "Arial"
activate
replace window 1 replacing multiple blank lines with nothing
replace window 1 looking for "^?^? Jan 20^?^?" replacing with "" with cases matching
-- ^? is Tex-Edit Plus's wildcard, used here for stripping dates
replace window 1 looking for "^?^? Feb 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Mar 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Apr 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? May 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Jun 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Jul 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Aug 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Sept 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Oct 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Nov 20^?^?" replacing with "" with cases matching
replace window 1 looking for "^?^? Dec 20^?^?" replacing with "" with cases matching
replace window 1 looking for "
" replacing with "—" with cases matching -- removes blank lines (what, again?)
set bounds of front window to {75, 500, 950, 800} -- sizes and positions window for convenience
end tell
Code formatting corrected for MacScripter by NG. See Markdown Reference.
Hi @silvercoyote.
MacScripter uses Markdown codes to format text in posts. The way to post AppleScript code here is to use plain text and to put three backticks on separate lines above and below:
```
AppleScript code
```
This makes the text appear in a box with an “Open in Script Editor” button.
I’ve done it for you above.
Hi @silvercoyote.
Here’s an initial attempt which works with your TextEdit paste result example as pasted from this MacScripter page into a TextEdit document. That is, the actual text may not be exactly the same as that pasted from the Web page you’re using. There are several ways one might approach this task. I’ve gone for using ASObjC methods to edit and arrange the text and sorting out the formatting in TextEdit afterwards. Let me know how it goes….
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
tell application "TextEdit"
activate
set plainText to text of front document -- Forget the formatting for now.
end tell
-- Get the text as an NSMutableString.
set str to current application's class "NSMutableString"'s stringWithString:(plainText)
set regex to current application's NSRegularExpressionSearch
-- Delete empty lines.
str's replaceOccurrencesOfString:("\\R++") withString:(linefeed) options:(regex) range:({0, str's |length|()})
-- Replace linefeeds after tickers with spaces.
str's replaceOccurrencesOfString:("(?<=AMZN|LNR|QBR\\.B|BTO)\\n") withString:(space) options:(regex) range:({0, str's |length|()})
-- Delete dates.
str's replaceOccurrencesOfString:("\\d{1,2} (?:Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) 20\\d\\d") withString:("") options:(regex) range:({0, str's |length|()})
-- Delete the empty line at the top where the first date was.
str's replaceOccurrencesOfString:("^\\n") withString:("") options:(regex) range:({0, str's |length|()})
-- Delete ".00" at the end of dollar values.
str's replaceOccurrencesOfString:("(\\$\\d++)\\.00") withString:("$1") options:(regex) range:({0, str's |length|()})
-- Insert spaces before and after these words.
str's replaceOccurrencesOfString:("(\\S)(BUY|HOLD|SELL|TENDER)(\\S)") withString:("$1 $2 $3") options:(regex) range:({0, str's |length|()})
-- Correct " -?-?-" if it occurs.
str's replaceOccurrencesOfString:("-?-?-") withString:("—") options:(0) range:({0, str's |length|()})
-- Get an array of the paragraphs, sort, and coerce to AppleScript text.
set theLines to str's componentsSeparatedByString:(linefeed)
set sortedLines to theLines's sortedArrayUsingSelector:("compare:")
set txt to (sortedLines's componentsJoinedByString:(linefeed)) as text
-- Sort out the formatting in TextEdit.
tell application "TextEdit"
tell front document
set its text to txt
tell its text
set font to "Arial"
set size to 13
repeat with thisParagraph in paragraphs
-- The "tickers" aren't necessarily "words".
set color of thisParagraph's characters 1 thru ((offset of " " in thisParagraph) - 1) to {0, 32767, 0}
end repeat
tell (words where (it is "HOLD")) to set {its font, its color} to {"Arial bold", {0, 0, 65535}}
tell (words where ((it is "SELL") or (it is "TENDER"))) to set {its font, its color} to {"Arial bold", {65535, 0, 0}}
end tell
end tell
end tell
A quick note of thanks, @Nigel_Garvey. Amazingly the broker website is down at the moment and I have to dash out this a.m. but the script definitely compiles! I’ll report back later to tell you how well it works — doubtless perfectly😀
Hi again @Nigel_Garvey
First, I’m impressed you whipped up a script so quickly — and flying blind, in that you don’t have direct access to the source broker’s website.
To update, today I’m using a fresh group of three stocks which when pasted straight to TextEdit appear like this:
01 Nov 2024
BBD.B
ScotiabankKonark Gupta280HOLD$120.0001 Nov 2024
AMZN
SusquehannaShyam Patil549BUY$230.0001 Nov 2024
META
DBSSachin Mittal160BUY$655.00
The tickers themselves appear green without me having to format them. By the way, when I paste to this site each line has an empty line after it, which I’ve removed.
Now, to what happens. I see that with your script I start by pasting the data into a TextEdit window before running the script. This is what I get (with the first “word” of each line — “ScotiabankKonark”, for instance — green):
AMZN SusquehannaShyam Patil549 BUY $230
BBD.B
DBSSachin Mittal160 BUY $655
META
ScotiabankKonark Gupta280 HOLD $120
Incidentally HOLD does appear correctly as bold blue, and I see that you’ve successfully removed the dates and the incidents of “.00”. A wrinkle is that although the script sorts alphabetically by the first “word” of each line, not all lines are starting with tickers.
FYI, this is what Script Editor’s Result window shows: {“Arial bold”, {65535, 0, 0}}
I’m amazed, frankly, that although it’s not perfect you’ve got it coming along well so soon.
Incidentally when I try to save your script I get an error message that “C and Objective-C pointers cannot be saved in scripts.”
Hi @silvercoyote.
Thanks for the feedback. The reason some of the tickers weren’t joined to the following lines is that the script was specifically looking for the tickers you gave earlier. I’ve now changed the approach simply to join each pair of lines, assuming that, at the point where it’s done, the text consists only of alternating sticker/non-sticker lines.
The greens have to be reapplied because the text is edited by the script itself rather than the script telling TextEdit to do it. The script receives the text from TextEdit as AppleScript text
, which has no colour or font information at all. This is then converted to an Objective-C NSMutableString
and the editing’s done using AppleScriptObjC. The final result’s coerced back to AppleScript text
and TextEdit’s told to reset the text of its front document to this. In the absence of formatting data, and assuming TextEdit’s in RTF mode, the application displays the text with the document’s default settings. The script then tells it to apply the required fonts and colours.
The {“Arial bold”, {65535, 0, 0}} result is simply the result of the last thing the script does, which is to set the font and colour of any instances of “SELL” or “TENDER”. The three numbers in {65535, 0, 0} represent the respective amounts of red, green, and blue in the colour, the possible values ranging from 0 to 65535. You may like to experiment with these if you’re not happy with the shades the script currently gives.
Here’s the new version. I’ve arranged it into handlers so that all the variables are locals and hopefully there’ll be no attempt to save their values along with the script:
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
main()
on main()
tell application "TextEdit"
activate
set plainText to text of front document -- Forget the formatting for now.
end tell
-- Tidy up the text,
set plainText to tidyText(plainText)
-- Reinsert it into the TextEdit document and sort out the fonts and colours.
tell application "TextEdit"
tell front document
set its text to plainText
tell its text
set font to "Arial"
set size to 13
-- Tickers in green.
repeat with thisParagraph in paragraphs
-- The "tickers" aren't necessarily "words".
set color of thisParagraph's characters 1 thru ((offset of " " in thisParagraph) - 1) to {0, 32767, 0}
end repeat
considering case
-- "HOLD"s in blue.
tell (words where (it is "HOLD")) to set {its font, its color} to {"Arial bold", {0, 0, 65535}}
-- "SELL"s and "TENDER"s in red.
tell (words where ((it is "SELL") or (it is "TENDER"))) to set {its font, its color} to {"Arial bold", {65535, 0, 0}}
end considering
end tell
end tell
end tell
end main
on tidyText(plainText)
-- Get the text as an NSMutableString.
set str to current application's class "NSMutableString"'s stringWithString:(plainText)
set regex to current application's NSRegularExpressionSearch
-- Delete dates.
str's replaceOccurrencesOfString:("\\d{1,2} (?:Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) 20\\d\\d") withString:("") options:(regex) range:({0, str's |length|()})
-- Delete the empty line at the top where the first date was …
str's replaceOccurrencesOfString:("^\\R") withString:("") options:(regex) range:({0, str's |length|()})
-- … and any other empty lines.
str's replaceOccurrencesOfString:("\\R++") withString:(linefeed) options:(regex) range:({0, str's |length|()})
-- Assuming what remains is alternating sticker and non-sticker lines, append each non-ticker line to the preceding ticker with a space.
str's replaceOccurrencesOfString:("([A-Z.]++)\\n(.++)") withString:("$1 $2") options:(regex) range:({0, str's |length|()})
-- Delete ".00" at the end of dollar values.
str's replaceOccurrencesOfString:("(\\$\\d++)\\.00") withString:("$1") options:(regex) range:({0, str's |length|()})
-- Insert spaces before and after these words.
str's replaceOccurrencesOfString:("(\\S)(BUY|HOLD|SELL|TENDER)(\\S)") withString:("$1 $2 $3") options:(regex) range:({0, str's |length|()})
-- Correct " -?-?-" if it occurs.
str's replaceOccurrencesOfString:("-?-?-") withString:("—") options:(0) range:({0, str's |length|()})
-- Get an array of the paragraphs and sort it.
set theLines to str's componentsSeparatedByString:(linefeed)
set sortedLines to theLines's sortedArrayUsingSelector:("compare:")
-- Return as a linefeed-delimited text in AppleScript format.
return (sortedLines's componentsJoinedByString:(linefeed)) as text
end tidyText
This one is fantastic! It gets everything exactly right including the colours (I also tested red) and I’m able to save it without Script Editor complaining about C and Objective-C pointers. Runs faster too. I get the feeling you’ve had to be cleverer about scripting TextEdit than I was with Tex-Edit Plus (although I was resorting to sticky plaster and rubber bands).
My partner says you’ll think me a freeloader, especially when I make a final request. Is it possible for the script to start by taking the data (first copied by me from the source website) from the clipboard, creating the TextEdit window and pasting it there before doing all the manipulation?
Your email arrived in the early hours, Mountain Time, so I first feared you’d been beavering through the night. Then I realized you spell colours with a u, so am hoping you “merely” set aside time in daytime Blighty or somewhere else on the eastern side of the pond.
Anyway, thanks again.
Hi @silvercoyote.
It’s theoretically possible, yes. How straightforward it would be to adapt the current script depends on what’s actually on the clipboard — something you say is interpreted differently by the RTF editor TextEdit from how it’s interpreted by the plain text editor BBEdit. With any luck, all that’s needed is to get the text from the clipboard instead of TextEdit, as in the version below, where only the first few lines of the main() handler are different. Otherwise the text tidying up will need some changes too.)
Yeah. I’m a Brit in the UK. We’ve been on GMT (our equivalent of Standard Time) for the past week.
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
main()
on main()
-- Get the text from the clipboard
set plainText to (the clipboard as text)
-- Tidy it up.
set plainText to tidyText(plainText)
-- Open it in a new TextEdit document and sort out the fonts and colours.
tell application "TextEdit"
activate
set newDoc to (make new document with properties {text:plainText})
tell newDoc's text
set font to "Arial"
set size to 13
-- Tickers in green.
repeat with thisParagraph in paragraphs
-- The "tickers" aren't necessarily "words".
set color of thisParagraph's characters 1 thru ((offset of " " in thisParagraph) - 1) to {0, 32767, 0}
end repeat
considering case
-- "HOLD"s in blue.
tell (words where (it is "HOLD")) to set {its font, its color} to {"Arial bold", {0, 0, 65535}}
-- "SELL"s and "TENDER"s in red.
tell (words where ((it is "SELL") or (it is "TENDER"))) to set {its font, its color} to {"Arial bold", {65535, 0, 0}}
end considering
end tell
end tell
end main
on tidyText(plainText)
-- Get the text as an NSMutableString.
set str to current application's class "NSMutableString"'s stringWithString:(plainText)
set regex to current application's NSRegularExpressionSearch
-- Delete dates.
str's replaceOccurrencesOfString:("\\d{1,2} (?:Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) 20\\d\\d") withString:("") options:(regex) range:({0, str's |length|()})
-- Delete the empty line at the top where the first date was …
str's replaceOccurrencesOfString:("^\\R") withString:("") options:(regex) range:({0, str's |length|()})
-- … and any other empty lines.
str's replaceOccurrencesOfString:("\\R++") withString:(linefeed) options:(regex) range:({0, str's |length|()})
-- Assuming what remains is alternating sticker and non-sticker lines, append each non-ticker line to the preceding ticker with a space.
str's replaceOccurrencesOfString:("([A-Z.]++)\\n(.++)") withString:("$1 $2") options:(regex) range:({0, str's |length|()})
-- Delete ".00" at the end of dollar values.
str's replaceOccurrencesOfString:("(\\$\\d++)\\.00") withString:("$1") options:(regex) range:({0, str's |length|()})
-- Insert spaces before and after these words.
str's replaceOccurrencesOfString:("(\\S)(BUY|HOLD|SELL|TENDER)(\\S)") withString:("$1 $2 $3") options:(regex) range:({0, str's |length|()})
-- Correct " -?-?-" if it occurs.
str's replaceOccurrencesOfString:("-?-?-") withString:("—") options:(0) range:({0, str's |length|()})
-- Get an array of the paragraphs and sort it.
set theLines to str's componentsSeparatedByString:(linefeed)
set sortedLines to theLines's sortedArrayUsingSelector:("compare:")
-- Return as a linefeed-delimited text in AppleScript format.
return (sortedLines's componentsJoinedByString:(linefeed)) as text
end tidyText
Adding the clipboard-copying stage does mess things up, just as you warned. I’ll study your script to see if I can do a tweaked version on my own. Meanwhile your pre-clipboard script is pretty cool. I’ve named it in your honour, Ratings by Garvey.
One never knows how one’s going to be immortalised!
@silvercoyote, would you mind giving the following clipboard reading version a try? I’m hoping that deriving an NSAttributedString derived from the RTF data on the clipboard and reading its ‘string’ property will produce text interpreted in the same way as pasting it into TextEdit. If so, the current editing code should be OK.
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
main()
on main()
-- Get the RTF data from the clipboard.
set thePasteboard to current application's class "NSPasteboard"'s generalPasteboard()
set RTFData to (thePasteboard's dataForType:("public.rtf"))
-- If none, finish.
if (RTFData is missing value) then error number -128
-- Otherwise derive an NSAttibutedString from the data and read its 'string' property (an NSString).
set attrStr to current application's class "NSAttributedString"'s alloc()'s initWithRTF:(RTFData) documentAttributes:(missing value)
set str to attrStr's |string|()
-- Get a tidied up version of the string as AppleScript text.
set plainText to tidyText(str)
-- Open the text in a new TextEdit document and sort out the fonts and colours.
tell application "TextEdit"
activate
set newDoc to (make new document with properties {text:plainText})
tell newDoc's text
set font to "Arial"
set size to 13
-- Tickers in green.
repeat with thisParagraph in paragraphs
-- The "tickers" aren't necessarily "words".
set color of thisParagraph's characters 1 thru ((offset of " " in thisParagraph) - 1) to {0, 32767, 0}
end repeat
considering case
-- "HOLD"s in blue.
tell (words where (it is "HOLD")) to set {its font, its color} to {"Arial bold", {0, 0, 65535}}
-- "SELL"s and "TENDER"s in red.
tell (words where ((it is "SELL") or (it is "TENDER"))) to set {its font, its color} to {"Arial bold", {65535, 0, 0}}
end considering
end tell
end tell
end main
on tidyText(str)
-- Work with a mutable copy of the NSString parameter.
set str to str's mutableCopy()
set regex to current application's NSRegularExpressionSearch
-- Delete dates.
str's replaceOccurrencesOfString:("\\d{1,2} (?:Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) 20\\d\\d") withString:("") options:(regex) range:({0, str's |length|()})
-- Delete the empty line at the top where the first date was …
str's replaceOccurrencesOfString:("^\\R") withString:("") options:(regex) range:({0, str's |length|()})
-- … and any other empty lines.
str's replaceOccurrencesOfString:("\\R++") withString:(linefeed) options:(regex) range:({0, str's |length|()})
-- Assuming what remains is alternating sticker and non-sticker lines, append each non-ticker line to the preceding ticker with a space.
str's replaceOccurrencesOfString:("([A-Z.]++)\\n(.++)") withString:("$1 $2") options:(regex) range:({0, str's |length|()})
-- Delete ".00" at the end of dollar values.
str's replaceOccurrencesOfString:("(\\$\\d++)\\.00") withString:("$1") options:(regex) range:({0, str's |length|()})
-- Insert spaces before and after these words.
str's replaceOccurrencesOfString:("(\\S)(BUY|HOLD|SELL|TENDER)(\\S)") withString:("$1 $2 $3") options:(regex) range:({0, str's |length|()})
-- Correct " -?-?-" if it occurs.
str's replaceOccurrencesOfString:("-?-?-") withString:("—") options:(0) range:({0, str's |length|()})
-- Get an array of the paragraphs and sort it.
set theLines to str's componentsSeparatedByString:(linefeed)
set sortedLines to theLines's sortedArrayUsingSelector:("compare:")
-- Return as a linefeed-delimited text in AppleScript format.
return (sortedLines's componentsJoinedByString:(linefeed)) as text
end tidyText
@Nigel_Garvey
I’m not meant to reply, on account of the “solved” status.
Regardless, I pondered your first clipboard version for a while — Greek comes to mind — then went for a walk in the snow (Edmonton, Alberta, first of the season). Anyway your script is perfect.
I see that it works on my old Intel iMac as well. No surprise, being Yosemite-friendly and hence good for the 32-bit compatible Mojave I’ve relied on for Tex-Edit Plus. But that’s good because I’m gradually moving over to my M3 Air in Sequoialand. My final step will be a new backup drive.
I can’t thank you enough. If you ever find yourself in the wilds of Alberta …