3

I'm creating a macOS app that tracks your time automatically. For the moment, I'm able to get workspace's frontMostApplication and get the app name, but I can't cant the window title. In fact, there is one, but it is not the one who is expected.

Look at this code :

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.optionAll)
let cgWindowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let cgWindowListInfo2 = cgWindowListInfo as NSArray? as? [[String: AnyObject]]

for windowDict in cgWindowListInfo2 ?? [] {
    print(windowDict)
}

The output for Xcode :

[
    "kCGWindowBounds": {
        Height = 1012;
        Width = 1680;
        X = 0;
        Y = 38;
    },
    "kCGWindowMemoryUsage": 1248,
    "kCGWindowAlpha": 1,
    "kCGWindowNumber": 140,
    "kCGWindowOwnerName": Xcode,
    "kCGWindowSharingState": 1,
    "kCGWindowIsOnscreen": 1,
    "kCGWindowLayer": 0,
    "kCGWindowStoreType": 1,
    "kCGWindowOwnerPID": 492,
    "kCGWindowName": AppDelegate.swift
]

As you can see on the last line, instead of having the project name, I have the current project name (my-project.xcodeproj or juste my-project), I have the current file name.

Is there a way to get the opened file or document name for every apps on Mac ?

Thanks.

fredericdnd
  • 968
  • 1
  • 14
  • 25
  • Possible duplicate of [Swift - Get file path of currently opened document in another application](https://stackoverflow.com/questions/49076103/swift-get-file-path-of-currently-opened-document-in-another-application) – Willeke Aug 10 '19 at 08:19
  • 1
    Did you ever figure this out – Lucas van Dongen Mar 29 '20 at 19:25

0 Answers0