I am trying to make a flutter desktop application that tracks down app usage and anize them.
For this, it is crucial to get active working window name that user is using. I founded how to achieve active window name capturing on windows environment using win32 package, but I cannot find anything equivalent in macOS.
My thought was to use native swift code and use ffigen for dart interop.
This is the code and ffigen settings that I have used, and tested it.
import AppKit
@objc(ApplicationManager)
@objcMembers
public class ApplicationManager: NSObject {
public override init() {
super.init()
}
public func getRunningApplications() -> [String] {
let runningApps = NSWorkspace.shared.runningApplications
return runningAppspactMap { $0.localizedName }
}
}
swiftc -c RunningApplicationsProvider.swift \
-module-name RunningApplicationsModule \
-emit-objc-header-path RunningApplicationsProvider.h \
-emit-library -o libRunningApplicationsProvider.dylib
To generate required Object-C entrypoint for dart interop
ffigen:
name: RunningApplicationsModule
description: Bindings for RunningApplicationsProvider.
language: objc
output: 'lib/src/generated/running_apps_bindings.dart'
exclude-all-by-default: true
objc-interfaces:
include:
- 'ApplicationManager'
headers:
entry-points:
- 'ios/native/RunningApplicationsProvider.h'
ffigen yaml config
And finally, actual code that I have tested upon.
import 'dart:ffi';
import 'package:opened_application/src/generated/running_apps_bindings.dart';
void main() {
// No DynamicLibrary, as it isnt' used.
final appManager = ApplicationManager.new1();
final runningApps = appManager.getRunningApplications();
print(runningApps);
}
I did this based on /interop/objective-c-interop#swift-example this example, but it keeps throwing error that it cannot create a class on the dart side.