Support "styled text" in a scriptable Mac application (Cocoa Scripting) - macos

My app supports being scripted with Applescript.
I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.
I thought I could simply deliver styled text with the NSAttributedString class, just like I deliver plain text with the NSString class, but that does not work - Cocoa Scripting then reports that it cannot convert or coerce the data.
I wonder if I'm missing something or if this is just plain impossible with the standard classes supported by Cocoa Scripting?
AppleScript does know the "styled text" type, as seen in this example:
set stxt to "foo" as styled text
So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?

As always there are many choices for solving an AS problem.
In my scriptable text editor (Ted), I implemented the Text Suite, which is based on rich text (NSTextStorage, a subclass of NSMutableAttributedString). I wanted to be able to script tabs in my paragraphs, so I added a style record, which contains all the paragraph style information. This lets me write scripts like this:
tell application "Ted"
set doc1 to make new document at beginning with properties {name:"Document One"}
tell doc1
set p1 to make new paragraph at end with data "Paragraph One" with properties {size:24, color:maraschino}
set p2 to make new paragraph at end with data "Paragraph Two" with properties {style:style of paragraph 1}
set color of paragraph 1 to blue
end tell
set doc2 to make new document at beginning with properties {name:"Document Two"}
copy p1 to beginning of doc2
properties of paragraph 1 of doc2
end tell
Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.
You can also ask for the properties of a piece of text, where I have implemented the usual Text Suite properties, as well as a "style" property for paragraph style (backed by NSParagraphStyle, since I wanted to be able to script the tab stops):
properties of paragraph 1 of doc2
Result:
{height:60.0, italic:false, size:24, style:{paragraph spacing after:0.0, head indent:0.0, line break mode:0, alignment:4, line spacing:0.0, minimum line height:0.0, first line head indent:0.0, paragraph spacing before:0.0, tabs:{"28L", "56L", "84L", "112L", "140L", "168L", "196L", "224L", "252L", "280L", "308L", "336L"}, tail indent:0.0, maximum line height:0.0, line height multiple:0.0, default tab interval:0.0}, color:blue, width:164.109375, font:"Helvetica", bold:false, class:attribute run}
This works well for passing rich text within my application, but may not be as useful for passing styled text to other applications, which may be what you wanted to do. I think adding a "style" property (of type record) is probably the best way to convey style info for use in other scriptable apps. Then in the second app, the scripter can make use of any properties in the style record that the second app understands.

It looks like there is no implicit support for styled text in AppleScript. And there is also no common interchange record type for passing styled text.
AppleScript was developed in the pre-OSX days when styled text was often represented by a combination of a plain text (in System or MacRoman encoding) and a styl resource. With Unicode came an alternative format of a ustl style format. These are still used with the Carbon Pasteboard API (PasteboardCreate etc.) today. Yet, none of these seem to have made it into the use with AppleScript.
The fact that AppleScript knows of a styled text type has no special meaning. Even its class is just text.
Update
I just found that Matt Neuburg's book "AppleScript The Definitive Guide" mentions styled text and gives an example where it's indeed showing a record containing both the plain text (class ktxt) and style data (class ksty) with data of type styl, just as I had expected above. He also points out that most applications don't use that format, though.
So, it appears using a record with style resource data is indeed the intended way, only that hardly anyone knows about it. Go figure.

Related

Extract Hyperlinks From Rich Text Clipboard Contents Or Text Selection On The Mac

I would like to be able to get a list of all the hyperlinked URLs in any formatted text that I select on the Mac (formatted text such as a web page or a word processor document).
Preferably I'd like use Applescript or Automator to extract this list of hyperlinks from the text (so that I can then use Applescript to perform further processing on these URLs).
Note that I am talking about hyperlinks being extracted from formatted text, not just extracting URLs from text containing plaintext URLs.
This hyperlink extraction from formatted text seems like it should be a simple programming task, but I have been struggling to find a way to do this in either Applescript or Automator.
Automator can be set to accept rich text input from a text selection, or can input rich text from the clipboard, but I cannot find any way to access this rich text as a string within Automator or Applescript, such that I can then extract the hyperlinked URLs from the string of rich text data.
Once I get access to the rich text data as a string, there will be no problem in extracting the URLs.
Any suggestions on how I might solve this issue are gratefully received.
Applescript itself does not unpack embedded text, so you'll have to use a helper app one way or another. You can use do shell script 'textutil' to do some unimbedding of links:
perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' |
textutil -stdin -stdout -convert html -format rtf
Then, you'll have to extract the URLs. I would suggest using the Automator action 'Extract Data' to do this. If you set the whole thing up as an Automator Workflow, you could invoke it from Applescript. Or, if you save it as a Service, you can just run the whole thing from the Service.
Here's a screenshot of that method that should show what you need:
Let me know if you have questions. You can see variations on this technique here.
Update:
If you want to create this into a Service, it is difficult to coerce the built-in input from Automator into RTF. An effective method is to ignore the input and do a
keystroke "c" using command down
to copy the selected text to the clipboard and then use the workflow from there. See example:

Applescript: Detect double instances

Here's how a text document in Apple Pages might be structured:
CHARACTER #1: Dialogue
CHARACTER #2: Dialogue
CHARACTER #1: Dialogue
Action description.
CHARACTER #1: Dialogue
My question:
Using Applescript, is there a way to detect that the last two dialogue entries stem from the same character, even though there is an action description in between?
CHARACTER names are defined by a paragraph style and always appear in ALL CAPS.
Many thanks for your time!
Disappointingly, the more recent versions of Pages (like 5.5.2, which I have right now) are very limited in AppleScript support. One should be able to access specific properties of paragraphs, including paragraph style (this seemed to be possible in past versions, 5-6 yrs ago), but this is not possible. You'd be better off exporting as an rtf (for example) and using a more complete script-able app -- but guess what? Pages doesn't even support exporting to RTF.
A quick bit of advice: If you do end up exporting as Word document, use the older "doc" version instead of the "docx" (under 'Advanced' setting in export), or at least test between them. docx can kill screenplay-formatted documents (which you seem to be working with).
I'd also suggest that you be more specific with your question, like what the style consists of, and any code you've actually tried (which is considered basic form here on StackOverflow).

Does an Indesign syntax highlighter exist?

Does anybody know of a non-manual method of highlighting syntax when pasted in InDesign?
I'm trying to show code of a project in an InDesign documentation but don't want to have to manually highlight the code, and preferably add numbered rows too.
Is there a plugin to achieve this?
This is the style in which I'd like to format the code.
Cheers
Not sure if you worked out an answer to this, but there's no magic button that will solve your problem.
However, InDesign does have a facility in each Paragraph style called GREP that can do what you're looking for.
This lets you write 'regex' or 'regular expressions' that are just rules for what to apply a given character style to. Yes, they look about as meaningful as Harry Potter incantations at first glance, but 2 or 3 simple regexes will get you a long way.
For instance:
(\<|<)!--\s*.*?\s*--(\>|>)
Will target HTML comments only.
(?<=").*?(?=")
This will target anything wrapped in straight double quotes.
(?<=\().*?(?=\))
This will target any text inside parentheses ().
There's an '#' symbol button in that GREP style next to where you type the regex - that gives you a drop-down menu that is almost like a 'Regex Wizard'. Try that too.
When you've got a regex that works, create a new character style for the text color and select it in the 'Apply Style' input.
Regexr.com has a tool that is good for testing this stuff. Paste your code sample in the bottom panel and your line of regex in the top. The bits that it targets will turn blue.
There is a searchable community panel on the left where people have probably already written expressions like the one you need.
I'm working out a JavaScript highlighter at the moment. It's a shame there's no communal 'Indesign style sharing library'.
Best of luck.
If had luck pasting syntax highlighted code into a Rich Text editor like Libre Office and then pasting it into Indesign. Just make sure whatever font your syntax highlighted code is in is also in InDesign because you'll get font missing errors when you pre-flight the book.

How do I edit a text buffer (or selected text) with the Visual Studio 2010 SDK

I want to create a simple extension which modifies text buffers based on a command. No sample, documentation or template that I've found so far explains anything about working with text buffers. Anyone got a clue how to do this?
What I want to end up with is a format selection/document extension for text files, that wrap content around 72 characters per line.
I found this extension together with sample very helpful, and now I have something which works. Though it was very counter intuitive at first, I was trying to get the at the code window while this example instead uses a command filter to fiddle with the text view by extending the editor.
The ITextView interface provides access to the text in the editor, you can access the Buffer through that and make changes that way.
Link
http://github.com/noahric/alignassignments

Using AppleScript to hide Keynote Text Fields in A Slide

I am no AppleScript Jedi, I've only done a few simple things, but I haven't been able to figure this one out and could use some help:
My wife uses slides for her Art History courses and would like to use the same slides for exams (sans identifying names). Rather than create a new presentation, I'd like a tool that iterates through the slides and hides the text fields.
Looking through the Keynote dictionary didn't give me any clues as to how to approach this, any ideas?
AFAIK, with Applescript you can only access the title and the body boxes of the slides. If the text you wish to remove is consistently in either of these boxes the simplest solution would be to loop through the slides replacing that text and then saving a copy of the document.
tell application "Keynote"
open "/Path/To/Document"
repeat with currentSlide in slides of first slideshow
set title of currentSlide to " "
set body of currentSlide to " "
end repeat
save first slideshow in "/Path/To/Document without answers"
end tell
If the text is in a container created with the textbox tool, I don't think you can solve it with Applescript, but Keynote uses an XML based file format, so you could try doing it by editing the XML with your scripting language of choice. The XML schema is documented in the iWork Programming Guide.

Resources