【发布时间】:2019-05-03 00:06:36
【问题描述】:
我使用 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 中完美运行。
【问题讨论】:
-
@Willeke,我在 info.plist 中添加了“NSAppleEventsUsageDescription”,在权利中设置了“App Sandbox” No,功能也是,应用沙箱已关闭。但它仍然不起作用。如果我在 Safari 中选择任何文本并从我的应用程序中运行 appleScript(通过与我设置 appleScript 的按钮交互),Safari 应用程序会被激活,但我没有得到选定的数据。
-
我也在我的应用程序中使用了辅助功能,所以当我现在从 Xcode 运行我的应用程序时,Xcode 被添加到“安全和隐私”->“辅助功能”中
-
Mojave 中的一项新安全功能围绕系统对话框展开,提示您授予对想要控制其他应用程序的应用程序的访问权限。我也没有得到任何这样的对话。我在启动时只有一个对话框,用于在系统偏好设置中授予我的应用程序的可访问性权限。
-
请在上下文中显示实际代码。什么是最新应用程序?你用脚本做什么?提供 MCVE。
标签: swift macos applescript