[英]AppleScript used in my cocoa mac app, stopped working in osx 10.14
我已經使用AppleScript從第三方應用程序中獲取選定的文本。 在osx 10.13中可以正常工作,但在osx 10.14中停止工作。
通過搜索,有一個建議在info.plist中添加“ NSAppleEventsUsageDescription”,但這對我也不起作用。
let latestApp = "Safari"
//Write script to activate the app and get the selected text to our app
let script = """
tell application \"\(latestApp)\"
activate
end tell
tell application \"System Events\"
tell process \"\(latestApp)\"
keystroke \"c\" using {command down}
delay 0.1
set myData to (the clipboard) as text
return myData
end tell
end tell
"""
let scriptObject = NSAppleScript.init(source: script)
let errorDict: AutoreleasingUnsafeMutablePointer<NSDictionary?>? = nil
var returnDescription:NSAppleEventDescriptor? = nil
returnDescription = scriptObject?.executeAndReturnError(errorDict)
if( returnDescription != nil ){
if( kAENullEvent != returnDescription?.descriptorType ){ //successful execution
if( cAEList == returnDescription?.descriptorType ){
print("return value")
}else{
print("Returned string : \(String(describing: returnDescription?.stringValue))")
let selectedStr = returnDescription?.stringValue!
if( (selectedStr?.count)! > 0 ){
print("selectedStr is :\(String(describing: selectedStr))")
}
}
}
}else{
print("Error is : \(String(describing: errorDict))")
}
它也可以在OS 10.12和10.13&ScriptEditor中完美運行。
由於您要激活"System Events" to tell process "Safari"
,因此不需要"System Events" to tell process "Safari"
...。 只需使用keystroke "c" using {command down}
使用"System Events"
來keystroke "c" using {command down}
完成相同的操作。 這不是什么大問題,但是消除了此處到處的不必要的代碼行,使得在代碼中導航更加容易和簡潔。 另外,在不keystroke "c" using {command down}
命令在keystroke "c" using {command down}
之前增加delay 0.3
的額外delay 0.3
下,我的系統上有50%的時間返回了一個空剪貼板。
使用最新版本的macOS Mojave,此AppleScript代碼對我有用。
tell application "Safari" to activate
delay 0.2 -- Adjust As Needed
tell application "System Events" to keystroke "c" using {command down}
set myData to (the clipboard) as text
由於clipboard
命令是由標准添加而不是系統事件處理的 (如@ user3439894在其注釋中所述),因此從System Events
告訴塊中刪除set myData to (the clipboard) as text
,使我能夠成功刪除delay 0.1
命令。
或選項2
實際上,再三考慮一下,如果您只想在Safari中使用它,那么下面一行AppleScript代碼將滿足您的需求。
您必須啟用Safari的“開發”菜單中的“ 允許來自Apple事件的JavaScript”選項才能使用do JavaScript
。
tell application "Safari" to set myData to (do JavaScript "''+document.getSelection()" in document 1)
我只解決了AppleScript部分,因為@matt徹底涵蓋了他帖子中的所有其他問題。
您說在以前的系統中“工作得很好”。 我很難相信,因為關於代碼的幾乎所有內容都是錯誤的。 我更正了您的代碼,並使您的腳本可以正常工作,幾乎沒有什么困難。
我會盡力描述我的所作所為。
為了准備基礎,我在腳本編輯器中運行了一個腳本版本(當然,刪除了反斜杠和字符串插值):
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari.app"
keystroke "c" using {command down}
delay 0.1
set myData to (the clipboard) as text
return myData
end tell
end tell
腳本最初沒有運行,但是出現了一個對話框,將我引導至系統偏好設置->安全和隱私->可訪問性,在此我檢查了腳本編輯器和系統事件。
現在,我准備創建該應用程序。 我的應用程序稱為AnotherAppleScriptExample。 在其權利中,沙箱為NO。
在其Info.plist中是以下條目:
我的代碼版本(修正了各種Swift錯誤)是這樣的:
let app = "Safari.app"
let script = """
tell application "\(app)"
activate
end tell
tell application "System Events"
tell process "\(app)"
keystroke "c" using {command down}
delay 0.1
set myData to (the clipboard) as text
return myData
end tell
end tell
"""
if let scriptObject = NSAppleScript(source: script) {
var error: NSDictionary? = nil
let result = scriptObject.executeAndReturnError(&error)
if( kAENullEvent != result.descriptorType ){
print(result.stringValue as Any)
}
}
我運行了應用程序。 我有兩個對話框。 首先這個:
我單擊確定。 然后這樣:
我單擊了“打開系統偏好設置”。 在系統偏好設置中,我檢查了我的應用程序(現在同時檢查了系統事件和我的應用程序):
現在地面已經做好充分的准備。 我退出了應用程序,然后再次運行。 該腳本可以正常工作,可以在Safari中打印所選內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.