支持"样式文本"在脚本的Mac适用的(可可脚本) [英] Support "styled text" in a scriptable Mac application (Cocoa Scripting)

查看:194
本文介绍了支持"样式文本"在脚本的Mac适用的(可可脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序所支持脚本与AppleScript的。

My app supports being scripted with Applescript.

我想使样式文本内容,存放在NSAttributedString对象,提供给一个AppleScript用户。

I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.

我想我可以简单地用NSAttributedString类提供样式文本,就像我的NSString类提供纯文本,但并不奏效 - 可可脚本则报告说,它不能转换或强制数据

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的不知道样式文本类型的,如在这个例子:

AppleScript does know the "styled text" type, as seen in this example:

set stxt to "foo" as styled text

所以,如果AppleScript的默认知道这种类型的,应该不是可可脚本引擎支持的话也不知?

So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?

推荐答案

由于总是有解决的问题AS很多选择。
在我的脚本的文本编辑器(TED),我实现了文本套房,这是基于富文本(NSTextStorage,NSMutableAttributedString的子类)。我希望能够到脚本标签在我的段落,所以我加了一个风格的记录,它包含了所有的段落样式信息。这让我写这样的脚本:

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

由于P1是富文本,第二个文档的第一个文档的第一段的文本和格式都结束了。

Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.

您也可以要求某一段文字,在那里我已经实现了常用的文本套房性能,以及为段落样式(由NSParagraphStyle支持的风格属性的属性,因为我希望能够给剧本制表位):

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       

结果:
{高度:60.0,斜体:假的,大小:24,样式:{段后间距:0.0,头缩进:0.0,行中断模式:0,对齐:4,行间距:0.0,最小线高度:0.0,第一行头缩进:0.0,之前段落间距:0.0,标签:{28L,56L,84L,112L,140L,168L,196L,224L,252L 280L,308L,336L},尾缩进:0.0,最大线高度:0.0,行高多:0.0,默认标签间隔:0.0},颜色:蓝色,宽度:164.109375,字体:黑体 ,大胆:假的,等级:属性来看}

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.

这篇关于支持"样式文本"在脚本的Mac适用的(可可脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆