home ~ projects ~ socials

Get A List Of The Active Apps In SwiftUI

Sorted List

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("This Is A Placeholder")
            let runningApps = NSWorkspace.shared.runningApplications
            let runningAppsSorted = runningApps.sorted(by: {
                $0.localizedName ?? "unknown name" < $1.localizedName ?? "unknown name"
            })
            List(runningAppsSorted, id: \.self.localizedName) { i in
                Text(i.localizedName ?? "unknow name")
            }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

Unsorted List

import SwiftUI

struct ContentView: View {
    let runningApps = NSWorkspace.shared.runningApplications
    var body: some View {
        List(runningApps, id: \.self.localizedName) {
            i in
            Text(i.localizedName ?? "unknow name")
        }
    }
}

#Preview {
    ContentView()
}
-- end of line --

References