简体   繁体   English

我的可可mac应用中使用的AppleScript在osx 10.14中停止工作

[英]AppleScript used in my cocoa mac app, stopped working in osx 10.14

I have used AppleScript to get selected text from third party app. 我已经使用AppleScript从第三方应用程序中获取选定的文本。 Which is working fine in osx 10.13 but stopped working in osx 10.14. 在osx 10.13中可以正常工作,但在osx 10.14中停止工作。

From searching, got one suggestion to add "NSAppleEventsUsageDescription" in info.plist, but that is also not working for me. 通过搜索,有一个建议在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))")

        }

It works perfectly in os 10.12 & 10.13 & ScriptEditor also. 它也可以在OS 10.12和10.13&ScriptEditor中完美运行。

在此处输入图片说明

Since you are telling "Safari" to activate, having "System Events" to tell process "Safari" ... Is not necessary. 由于您要激活"System Events" to tell process "Safari" ,因此不需要"System Events" to tell process "Safari" ...。 Simply using "System Events" to keystroke "c" using {command down} accomplishes the same thing. 只需使用keystroke "c" using {command down}使用"System Events"keystroke "c" using {command down}完成相同的操作。 It's not a huge deal but eliminating unnecessary lines of code here and there, makes navigating through code easier and cleaner. 这不是什么大问题,但是消除了此处到处的不必要的代码行,使得在代码中导航更加容易和简洁。 In addition, without adding an additional delay 0.3 before the keystroke "c" using {command down} command, returned an empty clipboard on my system 50% of the time. 另外,在不keystroke "c" using {command down}命令在keystroke "c" using {command down}之前增加delay 0.3的额外delay 0.3下,我的系统上有50%的时间返回了一个空剪贴板。

This AppleScript code works for me using the latest version of macOS Mojave. 使用最新版本的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

Since clipboard commands are handled by Standard Additions and not System Events (as mentioned by @user3439894 in his comment), removing set myData to (the clipboard) as text from the System Events tell block, allowed me to successfully remove the delay 0.1 command. 由于clipboard命令是由标准添加而不是系统事件处理的 (如@ user3439894在其注释中所述),因此从System Events告诉块中删除set myData to (the clipboard) as text ,使我能够成功删除delay 0.1命令。

OR OPTION 2 或选项2

Actually, on second thought, if you are looking to only use this in Safari, this following one line of AppleScript code will do what you need. 实际上,再三考虑一下,如果您只想在Safari中使用它,那么下面一行AppleScript代码将满足您的需求。

You must enable the Allow JavaScript from Apple Events option in Safari's Develop menu to use do JavaScript . 您必须启用Safari的“开发”菜单中的“ 允许来自Apple事件的JavaScript”选项才能使用do JavaScript

tell application "Safari" to set myData to (do JavaScript "''+document.getSelection()" in document 1)

I have only addressed the AppleScript part because @matt thoroughly covered every other issue in his post. 我只解决了AppleScript部分,因为@matt彻底涵盖了他帖子中的所有其他问题。

You say "it worked perfectly" in previous systems. 您说在以前的系统中“工作得很好”。 I find that difficult to believe, since almost everything about your code is wrong. 我很难相信,因为关于代码的几乎所有内容都是错误的。 I corrected your code and got your script to work, with very little difficulty. 我更正了您的代码,并使您的脚本可以正常工作,几乎没有什么困难。

I'll try to describe what I did. 我会尽力描述我的所作所为。

To prepare the ground, I ran a version of your script in Script Editor (removing the backslashes and string interpolation of course): 为了准备基础,我在脚本编辑器中运行了一个脚本版本(当然,删除了反斜杠和字符串插值):

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

The script didn't run at first, but a dialog sent me to System Preferences -> Security & Privacy -> Accessibility, where I checked Script Editor and System Events. 脚本最初没有运行,但是出现了一个对话框,将我引导至系统偏好设置->安全和隐私->可访问性,在此我检查了脚本编辑器和系统事件。

在此处输入图片说明

Now I was ready to create the app. 现在,我准备创建该应用程序。 My app is called AnotherAppleScriptExample. 我的应用程序称为AnotherAppleScriptExample。 In its entitlements, sandboxing is NO. 在其权利中,沙箱为NO。

在此处输入图片说明

In its Info.plist is this entry: 在其Info.plist中是以下条目:

在此处输入图片说明

My version of your code (fixing the various Swift mistakes) is this: 我的代码版本(修正了各种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)
            }
        }

I ran the app. 我运行了应用程序。 I got two dialogs. 我有两个对话框。 First this: 首先这个:

在此处输入图片说明

I clicked OK. 我单击确定。 Then this: 然后这样:

在此处输入图片说明

I clicked Open System Preferences. 我单击了“打开系统偏好设置”。 In System Preferences, I checked my app (now both System Events and my app are checked): 在系统偏好设置中,我检查了我的应用程序(现在同时检查了系统事件和我的应用程序):

在此处输入图片说明

Now the ground is fully prepared. 现在地面已经做好充分的准备。 I quit the app and ran it again. 我退出了应用程序,然后再次运行。 The script worked correctly, printing the selection in Safari. 该脚本可以正常工作,可以在Safari中打印所选内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM