以编程方式选择文本编辑的文本范围 [英] Programmatically select text range in TextEdit

查看:689
本文介绍了以编程方式选择文本编辑的文本范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能(通过AppleScript的,可可或碳)选择(高亮)的文本编辑的文本范围?
我trye​​d这code,但不工作:

Is it possible to select (Highlight) a range of text in TextEdit (by AppleScript,Cocoa or Carbon)? I tryed this code but not work:

set value of attribute "AXSelectedTextRange" to {selStart, selLen}

看来这个属性是只读的。
谢谢你。

It seems this attribute is readonly. Thanks.

推荐答案

不知道如何与AppleScript的做到这一点(应该是可能的,虽然),使用辅助的API,你可以做这样的事情:

Not sure how to do it with AppleScript (should be possible though), with the accessibility APIs, you could do something like this:

AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement);
CFRange range = CFRangeMake(0, 10);
AXUIElementSetAttributeValue(focussedElement, kAXSelectedTextRangeAttribute, AXValueCreate(kAXValueCFRangeType, &range));
CFRelease(focussedElement);
CFRelease(systemWideElement);

如果聚焦在文本编辑窗口,将选择前10个字符。

That would select the first 10 characters if the TextEdit window is focussed.

这篇关于以编程方式选择文本编辑的文本范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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