2012-05-08 32 views
1

閱讀問題「Programmatically select text range in TextEdit」後,我很好奇爲什麼AXSelectedTextRange屬性無法在AppleScript中設置,即使屬性指示它可設置。無法在AppleScript中設置可設置的輔助功能屬性

打開一個TextEdit文檔,輸入幾行,高亮顯示一部分,然後運行下面的腳本進行測試。

activate application "TextEdit" 
tell application "System Events" 
    set attr to attribute "AXSelectedTextRange" of text area 1 of scroll area 1 of window "Untitled" of application process "TextEdit" 
    attr's properties -- determine if settable 
    set attr's value to {1, 2} 
    delay 1 
    attr's value 
end tell 

對於AppleScript如何處理我誤解的可訪問性屬性有什麼不同嗎?

回答

2

由於所請求的類(包含類NSRange [或CFRange在碳]的對象的NSValue)不存在的AppleScript。在Objective-C中,AppleScript中的類列表等於類NSArray

又如:

set value of attribute "AXPosition" of window 1 of application process "TextEdit" to {30, 30} 

什麼也不做,因爲該值必須是一個NSPoint

當獲得在AppleScript的屬性值,「系統事件」轉換NSRangeNSpointNSSize的NSRect的NSArray包含數字(一個AppleScript 列表),但確實當您要設置值時,不要將列表轉換爲這些類型。

  • NSAccessibility Protocol Reference:NSAccessibilitySelectedTextRangeAttribute:選擇文本(NSValue)的範圍內。類NSValue可以含有這些類(NSRange,NSpoint,NSSize或的NSRect)

  • 中的對象在Carbon Accessibility Reference:kAXSelectedTextRangeAttribute:CFTypeRef:當前選擇的文本的範圍(始端&字符位置)。該屬性通常是可設置的。該值是一個編碼的CFRange結構。

因此,有可能在Objective-C,AppleScriptObjC或者可以使用可可或碳API,像UI瀏覽器任何其他語言 - 但不是在AppleScript的。

+0

真的很棒的答案! – kopischke

+0

很好的解釋傑克,謝謝! – adayzdone

-1

是的。我不知道的確切原因,但幾年來許多腳本編寫者問同樣的問題(來源:macscripter)。似乎即使屬性settable設置爲true,AXSelectedTextRange屬性的值似乎也是隻讀的。

+0

謝謝DJ。我可以通過UI瀏覽器的屬性抽屜來設置它,而不是通過AS,很奇怪。 – adayzdone

+0

這並不回答這個問題(這是「爲什麼」),它只是證實了這個問題。 @ jackjr300's [answer](http://stackoverflow.com/a/10511156/990363)雖然。 – kopischke

相關問題