Text Scripting in SubEthaEdit
Posted by dom Tue, 06 Jun 2006 10:25:00 GMT
With the release of SubEthaEdit 2.5 we updated our support for AppleScript so you can finally do AppleScripts that change the content of a document. Since the standard text scripting does not support any text change we had to do it ourself. So how do you change text in SubEthaEdit via AppleScript?
First and foremost every Scripter should take a look at the supplied AppleScript Dictionary which can be used as reference when AppleScripting. To do that you simply drag SubEthaEdit onto the Dock icon of the Script Editor, or via the menu: File → Open Dictionary….
For inserting text there are 2 new objects: selection and insertion point. The selection is a property of a window, but you can also access it via the document or the application directly. To insert text you have to set the contents of a selection or insertion point:
… will replace the currently selected text in the front window of the front document with "foobar".
Insertion points belong to a plain text object and are a to-many relationship. Any plain text object and selection has length + 1 insertion points which can be addressed by index. So if you want to insert text before character 5 you can do
You can leave out the of plain text part because plain text is the implied container of document. More examples of possible insertions:
set contents of insertion point 5 of paragraph 2 of front document to "(5th character of line 2)"
-- insert "foobar" at the end of the current text
set contents of last insertion point of front document to "foobar"
If you want to replace a paragraph you can also set the contents of a paragraph directly. In addtion to contents a paragraph has the property innerContents which gives you the contents without the newline. This property can be set too.
set innerContents of paragraph 3 of front document to "didn't remove the line ending"
If you want to replace larger areas of text you have to first set the selection and then set its contents. You can change the selection by many ways:
-- before of the second character
set selection to 2
-- extend the selection to the next 4 characters
set length of selection to 4
-- do the same in one step
set selection to [2, 5]
-- shrink the selection to [4,5]
set startCharacterIndex of selection to 4
-- shrink the selection to an insertion point
-- before the fourth caracter
set nextCharacterIndex of selection to 4
-- do the same via an insertion point
set selection to insertion point 4 of front document
-- select the second paragraph
set selection to paragraph 2 of front document
-- extend the selection to be a paragraph range
set startCharacterIndex of selection to startCharacterIndex of paragraph (startLineNumber of selection) of front document
set nextCharacterIndex of selection to nextCharacterIndex of paragraph (endLineNumber of selection) of front document
That's all for now - we hope you find this information useful. Feel free to ask additional questions in the comments.
What is you rationale for not implementing the Text Suite?
This is a great feature. One improvement I think would be really good is if the seescriptsettings() had another setting named "modes", which could contain a list of modes (comma-seperated, perhaps) to which the script is relevant. This would be useful for two reasons:
1) I don't like putting things inside obscure subdirectories inside application bundles. I'd rather have all my custom scripts in ~/Library/Application Support/SubEthaEdit/Scripts.
2) Scripts could be added to the "Mode" menu for multiple modes without having to be duplicated.
I'm thinking that if the "modes" setting is empty, the script would just be placed in the global scripts menu.
SubEthaedit is a powerful text editor. The most important new feature in this release is the improvement to SubEthaEdit's AppleScript dictionary. It allows you to to script text manipulation routines in AppleScript and other scripting languages.
ElinaSmith
It seems that Text scripting with the new version of the SubEthaEdit would be very much easier. It allows to insert and to remove the paragraphs with minimal writing.but can you tell how do the script appear in the menu?
what does auto complete dictionaries means in subetha edit?
good information about the x code but can you tell me how to translate apple script to x code?
This is great post, and I’m glad to see you sharing it with your readers.