我的 cocoa mac 应用程序中使用的 AppleScript 在 osx 10.14 中停止工作
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 中也能完美运行。
你在以前的系统中说"it worked perfectly"。我觉得这很难相信,因为您的代码几乎所有内容都是错误的。我更正了您的代码并让您的脚本正常工作,几乎没有困难。
我将尝试描述我所做的事情。
为了打好基础,我在脚本编辑器中 运行 编写了一个版本的脚本(当然删除了反斜杠和字符串插值):
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 中打印选择。
既然你告诉 "Safari" 激活,"System Events" to tell process "Safari"
... 就没有必要了。简单地使用 "System Events"
到 keystroke "c" using {command down}
完成同样的事情。这不是什么大不了的事,但在各处消除了不必要的代码行,使浏览代码更容易、更清晰。此外,在 keystroke "c" using {command down}
命令之前不添加额外的 delay 0.3
,50% 的时间在我的系统上返回一个空剪贴板。
此 AppleScript 代码适用于使用最新版本的 macOS Mojave 的我。
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
命令由 Standard Additions 而不是 System Events 处理(正如@user3439894 在他的评论中提到的) ),从 System Events
tell 块中删除 set myData to (the clipboard) as text
,使我能够成功删除 delay 0.1
命令。
或选项 2
实际上,转念一想,如果您只想在 Safari 中使用它,那么下面一行 AppleScript 代码就可以满足您的需要。
您必须在 Safari 的开发菜单 中启用 允许 JavaScript 来自 Apple 事件 选项才能使用 do JavaScript
.
tell application "Safari" to set myData to (do JavaScript "''+document.getSelection()" in document 1)
我只解决了 AppleScript 部分,因为@matt 在他的 post.
中彻底涵盖了所有其他问题
我已经使用 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 中也能完美运行。
你在以前的系统中说"it worked perfectly"。我觉得这很难相信,因为您的代码几乎所有内容都是错误的。我更正了您的代码并让您的脚本正常工作,几乎没有困难。
我将尝试描述我所做的事情。
为了打好基础,我在脚本编辑器中 运行 编写了一个版本的脚本(当然删除了反斜杠和字符串插值):
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 中打印选择。
既然你告诉 "Safari" 激活,"System Events" to tell process "Safari"
... 就没有必要了。简单地使用 "System Events"
到 keystroke "c" using {command down}
完成同样的事情。这不是什么大不了的事,但在各处消除了不必要的代码行,使浏览代码更容易、更清晰。此外,在 keystroke "c" using {command down}
命令之前不添加额外的 delay 0.3
,50% 的时间在我的系统上返回一个空剪贴板。
此 AppleScript 代码适用于使用最新版本的 macOS Mojave 的我。
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
命令由 Standard Additions 而不是 System Events 处理(正如@user3439894 在他的评论中提到的) ),从 System Events
tell 块中删除 set myData to (the clipboard) as text
,使我能够成功删除 delay 0.1
命令。
或选项 2
实际上,转念一想,如果您只想在 Safari 中使用它,那么下面一行 AppleScript 代码就可以满足您的需要。
您必须在 Safari 的开发菜单 中启用 允许 JavaScript 来自 Apple 事件 选项才能使用 do JavaScript
.
tell application "Safari" to set myData to (do JavaScript "''+document.getSelection()" in document 1)
我只解决了 AppleScript 部分,因为@matt 在他的 post.
中彻底涵盖了所有其他问题