我正在嘗試從通知中心獲取 UI 元素的位置。當我獲得坐標串列(一個 x 和 y posn
)然后分別參考每個坐標時,我可以這樣做。但是,當我嘗試直接執行此操作時,出現型別強制錯誤。
System Events got an error: Can’t make item 1 of position of group 1 of UI element 1 of scroll area 1 of window "Notification Center" of process "Notification Center" into type specifier.
.
這是示例代碼:
activate application "NotificationCenter"
tell application "System Events"
tell process "Notification Center"
-- works
set xyPositions to position of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
set xPos to item 1 of xyPositions
-- fails with error
set xPos to item 1 of position of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
end tell
end tell
我想知道發生了什么,因為這兩種方法看起來是等價的。
uj5u.com熱心網友回復:
它們看起來相同,但它們不是:
set xyPositions to position of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
真的是簡寫:
set xyPositions to get position of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
當 AppleScript 看到物件說明符時,它會確定該說明符是否指向本地 AppleScript 值(字串、串列、記錄等)或遠程應用程式(在本例中為系統事件)的屬性或元素。
如果說明符指向一個應用程式,并且說明符還不是應用程式命令(move
、duplicate
、delete
等)的引數,AppleScript 會自動執行get
命令(也稱為“隱式獲取”)來決議該物件說明符。在您的示例中,這將回傳position
屬性的值,它是一個 AppleScript 串列。第二行就如你所料的那樣作業了:
set xPos to item 1 of xyPositions
因為 AppleScript 提取該串列的第一項。
當您將兩行合并為一行時,您是在告訴應用程式獲取位置屬性的第 1 項:
set xPos to [get] item 1 of position of group 1 of UI element 1 of scroll area 1 of window "Notification Center"
然而,應用程式并不知道如何做到這一點——它只能訪問其物件模型中的物件。它不知道如何操作原始字串、串列、記錄等的內容;只有 AppleScript 可以做到這一點。因此應用程式命令失敗。
如果您希望將這兩行合并為一行,則必須get
在操作其結果之前使用顯式命令來決議應用程式說明符:
set xPos to item 1 of (get position of group 1 of UI element 1 of scroll area 1 of window "Notification Center")
是的,這很混亂。但這就是適合您的 AppleScript:它試圖變得“簡單”和“有用”,以便非專家也能輕松使用它,但實際上它所做的只是混淆了表面上友好的語法下實際發生的事情。當它起作用時,就像奧術魔法一樣。當它混淆時,它就像神秘的魔法。就這樣吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533649.html
標籤:苹果系统苹果脚本
上一篇:命令列工具總是崩潰“dyld:惰性符號系結失敗:找不到符號:____chkstk_darwin”
下一篇:缺少模式匹配的正則運算式