Goal
I want to print out to console in Swift, full window title of the frontmost window of the frontmost active application, whenever the title (or the active app of course) changes. I am quite new to both Swift/Objective-C, so I'd be also very glad if any solution in ObjC is suggested too. (Since there is such thing called AppleScriptObjc, but I just couldn't understand how it works)
What I've done so far
class activeApp: NSObject {
override init() {
super.init()
NSWorkspace.shared.notificationCenter.addObserver(self,
selector: #selector(printMe(notification:)),
name: NSWorkspace.didActivateApplicationNotification,
object:nil)
}
@objc func printMe(notification: NSNotification) {
let app = notification.userInfo!["NSWorkspaceApplicationKey"] as! NSRunningApplication
let myAppleScript = """
tell application "System Events"
return name of first window of (first application process whose frontmost is true)
end tell
"""
let script = NSAppleScript(source: myAppleScript)!;
var errorDict : NSDictionary?
print(script.executeAndReturnError(&errorDict).stringValue as Any)
print(app.localizedName!)
}
}
let runme = activeApp()
RunLoop.main.run()}
I'm aware that this code only prints window title and active app whenever the active app changes, not when the title changes while staying in the same app. However, I want to handle with that after I handle the errors it's showing on the console.
So everytime I build this code, it first prints these two messages on console only the first time active app changes:
2017-11-06 15:49:20.113732+0300 commandline[82318:6291735] MessageTracer: load_domain_whitelist_search_tree:73: Search tree file's format version number (0) is not supported
2017-11-06 15:49:20.113787+0300 commandline[82318:6291735] MessageTracer: Falling back to default whitelist
Then it only prints this message everytime the applescript code executes (so everytime active app changes):
2017-11-06 15:49:20.394204+0300 commandline[82318:6291735] AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference.
It prints the same message more times (like 3, 4 etc) if I write the applescript code with more tell's and if's
Although these errors are shown in console, the code works properly and prints the title and app name every time "active app" changes.
EDIT FOR ERRORS: An apple developer indicated that these errors are actual bugs and will be solved
Questions
Any helpful info about the errors(warnings) would be great as I have no idea after googling them all? EDIT: Probably a bug with macOS 10.13, will be solved
Do you think of any other way then doing this without applescript? I couldn't find any other methods to get the window title without applescript.
If Applescript is the way to do it, is this the best way or any other ways would be more efficient? Like using ApplescriptObjc or etc.
Is there anyway to execute the applescript code when the title changes, instead of simply putting it into loop and getting the info every X second (just like using a notification for app change, with a similar approach maybe?)
NOTE: I have looked for hours for possible solutions in other asked similar questions, however it really wasn't out there. Please don't suggest a link of the similar titled questions, they contain helpful info about my issue but not directly answering these questions. Thanks in advance, happy coding!