[英]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.