I’d like to use TextEdit as a poor-man’s logging facility. I found some AppleScript code out there, but I can’t seem to get any of it to work.
Here’s what I’d like to do, in the order of importance:
Get the current document
Append a line to the document
Clear all text in the document
Create a new named document
Get a named document.
Same as #2, but supplying at least the text color, and possibly the font size.
If I could just get the first two working, I’d be happy. I think I know how to do #1, but I put it there because if anyone is going to help me with #2, they’d obviously have to know how to do #1.
I’m fine with AppleScript. JXA is OK also, but I suspect if there’s existing code around, it’s probably AppleScript.
As I said, the examples I’ve found are almost all from a 2014 Developer’s Conference, and the code shown there doesn’t work, at least not for me, on my El Capitan system.
OK, this is working. I’ve got more work to do, but at least this works:
(function() {
'use strict'
var TextEdit = (function() {
var _textEdit;
function _getTextEdit() {
if (!_textEdit)
_textEdit = Application("TextEdit");
return _textEdit;
}
return {
// "options" is optional
appendParagraph: function(doc, text, options) {
var te = _getTextEdit();
if (!options)
options = {};
var para = te.Paragraph(options, text + "\n");
doc.paragraphs.push(para);
return para;
},
getCurrentDocument: function() {
return _doc;
},
getDocument: function(name) {
var te = _getTextEdit();
for (var i = 0; i < te.documents.length; i++) {
doc = te.documents[i];
if (doc.name() === name)
return doc;
}
return null;
},
newDocument: function(name) {
var te = _getTextEdit();
var doc = te.Document({name: name});
te.documents.push(doc);
return doc;
}
};
})();
var doc = TextEdit.getDocument("Log") || TextEdit.newDocument("Log");
TextEdit.appendParagraph(doc, "Test", { color:"red", size:20});
})();
The truly odd thing is the way I have to find the document with the name I want. I should be able to do this:
var doc = _textEdit.documents("Log");
But it’s not really working. It kind of works (I think), but you can’t tell if it fails. “doc” always contains something. So that’s why I use the loop.
Don't! TextEdit is a detestable scripting instrument.
Download Tex-Edit Plus and use it instead. While Tex-Edit Plus is shareware ($15.00 U.S.), the demo version is entirely unlimited in time and functionality.
Or use the Console.app.
Or use TextWrangler (freeware).
Or use Smile (freeware).
All that said I do use TextEdit on occasion for scripts/macros, because I know it will be on everyone's system.
--------------------------------------------------------------------------------
# Manage the front document using a peristent window ID.
--------------------------------------------------------------------------------
tell application "TextEdit"
set frontDocWinID to id of front window
tell window id frontDocWinID
tell its document
properties
end tell
end tell
end tell
--------------------------------------------------------------------------------
# Clear the text of the front document.
--------------------------------------------------------------------------------
tell application "TextEdit"
set frontDocWinID to id of front window
tell window id frontDocWinID
tell its document
set its text to ""
end tell
end tell
end tell
--------------------------------------------------------------------------------
# Append Text to front document.
--------------------------------------------------------------------------------
tell application "TextEdit"
tell front document
make new paragraph at after last character with data ("Log it!" & linefeed)
set font of last paragraph to "Menlo"
set size of last paragraph to 14
end tell
end tell
--------------------------------------------------------------------------------
# Make new named window.
--------------------------------------------------------------------------------
set filePath to (path to desktop folder as text) & "newRtfFile.rtf"
tell application "TextEdit"
set newDoc to make new document
save newDoc in file filePath
end tell
--------------------------------------------------------------------------------
# Get a Named Document.
--------------------------------------------------------------------------------
tell application "TextEdit"
set theDoc to first document whose name starts with "newRtfFile"
end tell
--------------------------------------------------------------------------------
Absolutely. Even more so if you pay Tom Bender some money.
I've been a registered user ever since it became shareware, and Tom is one of the good guys.
(Kagi went belly-up, and I don't think he's switched payment agents yet. I'm sure he can still handle direct payments though.)
Tex-Edit Plus lacks some Cocoa goodies, but it more than makes up for this in scriptability and is very lightweight (resource-wise).
Here's a small scripting sample.
set saveFolder to path to desktop folder as text
tell application "Tex-Edit Plus"
activate
make new document
set text of front document to "Stub Text.........."
delay 1.0
set frontDoc to name of front document
set newDoc to make new document
delay 1.0
tell newDoc
set its name to "I'm a Log!"
delay 1.0
set its text to "Some Goofy Text"
delay 1.0
save it in (saveFolder & "I changed my mind!")
end tell
set index of document frontDoc to 1
delay 1.0
tell front document
set its text to ""
delay 1.0
set end of its text to "Log line 01" & return
end tell
delay 1.0
tell front document
set end of its text to "Log line 02"
delay 1.0
set style of first word of last line to bold
set style of second word of last line to bold
set style of last word of last line to italic
end tell
end tell
tell application "Tex-Edit Plus"
make new document with properties {bounds:{207, 44, 1112, 1196}, contents:"DEFAULT TEXT"}
end tell
tell application "Tex-Edit Plus"
tell front window
tell selection
set font to "Times"
set size to 9
set style to plain
set color to black
set first indent to 6
set space below to 6
set bottom border to none
set justification to left
end tell
end tell
end tell
tell application "Tex-Edit Plus"
tell front document
tell paragraph 2
set font to "Times"
set size to 18
set style to {bold, italic, underline}
end tell
end tell
end tell
Still trying to understand how that could possibly happen. Perhaps that says alot about how often people use them to pay for software, in other words... People don't pay for software :- /
Funny I used to use it ages ago and forgot about it.
So what are some of the ways you are using Tex-Edit? I want to know to jog my awareness of some things I might consider doing.
Just copied those Tex-Edit Plus examples from Chris.
But I’m still not sure:
how to embed the 2nd script (set font to “Times” to 9) into a “full” script which opens a document ”Myloggs”. I know it has to be done with nested tell-blocks…
tell application "Tex-Edit Plus"
set fileToOpen to (path to home folder as text) & "_Tmp ƒ:untitled folder 3:test doc.rtf"
open alias fileToOpen
tell front document
tell paragraph 2
set font to "Times"
set size to 18
set style to {bold, italic, underline}
end tell
end tell
save front document as «class RTF »
close front document
end tell
I think we are all doing that. I was more curious about some loftier ways you might be using it.
For example, one way I would like to use it (if it were needed for this process) would be to snag emailed attachments coming from a sign-up web form. Would you use Tex-Edit for this or just AppleScript and/or Keyboard Maestro? Or would using perl or some other choice already have some solution available?
You're missing the boat. The following is a completely invalid path:
Documents/_test2/test doc.rtf
You’ve tried to change an HFS path into a POSIX Path, but you don't have the POSIX Path format correct.
Home-directory notation would look like this:
~/'Documents/_test2/test doc.rtf'
* It's quoted, because of the spaces in the file name – note that ~/ is NOT quoted.
Home-directory notation does NOT work in AppleScript, and POSIX Paths are NOT supported everywhere – which is why I always use HFS paths and aliases, unless I have a specific reason to do otherwise.
A normal POSIX Path would look like this:
/Users/YourUserName/Documents/_test2/test doc.rtf
I prefer to use Home-directory notation when possible, because it is more portable than a hard-coded path.
--------------------------------------------------------------------------------
# AppleScript equivalent of Home-directory notation:
set filePathHFS to (path to documents folder as text) & "_test2:test doc.rtf"
# Turning the HFS path into an alias for opening with Tex-Edit Plus.
set fileAlias to alias filePathHFS
tell application "Tex-Edit Plus"
activate
open fileAlias
tell text of front document
set font to "Menlo"
set size to 14
end tell
end tell
--------------------------------------------------------------------------------
[quote=“Tunes, post:17, topic:4653”]
For example, one way I would like to use it (if it were needed for this process) would be to snag emailed attachments coming from a sign-up web form.[/quote]
They are generated from Jotform and are sent from a website. So the text file is consistent. It’s attached to an email then mailed to me. I want to be able to take the forms and ingest them into Filemaker or at least an Excel sheet because I can easily get that into Filemaker Pro. I will take a look to see if I can find a previous form that was mailed and post that here.