I have used AppleScript to get selected text from third party app. Which is working fine in osx 10.13 but stopped working in osx 10.14.
From searching, got one suggestion to add "NSAppleEventsUsageDescription" in info.plist, but that is also not working for me.
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.