Skip to content

Commit

Permalink
Adds --hidden to fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
xenodium committed Sep 10, 2023
1 parent fe124f1 commit 77a4ebd
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import AVFoundation
import ArgumentParser
import Cocoa

let packageVersion = "0.5.1"
let packageVersion = "0.6.1"

var recorder: WindowRecorder?

Expand All @@ -41,6 +41,9 @@ struct RecordCommand: ParsableCommand {
@Flag(name: .shortAndLong, help: "List recordable windows.")
var list: Bool = false

@Flag(name: .long, help: "Also include hidden windows when listing.")
var hidden: Bool = false

@Option(
name: [.customShort("x"), .long],
help: ArgumentHelp(
Expand Down Expand Up @@ -82,10 +85,15 @@ struct RecordCommand: ParsableCommand {
}

if list {
NSWorkspace.shared.printWindowList()
NSWorkspace.shared.printWindowList(includeHidden: hidden)
Darwin.exit(0)
}

if hidden {
print("Error: can't use --hidden with anything other than --list")
Darwin.exit(1)
}

if let windowIdentifier = screenshot {
if record != nil {
print("Error: can't use --screenshot and --record simultaneously")
Expand Down Expand Up @@ -217,8 +225,8 @@ struct WindowInfo {
}

extension NSWorkspace {
func printWindowList() {
for window in allWindows() {
func printWindowList(includeHidden: Bool) {
for window in allWindows(includeHidden: includeHidden) {
if window.title.isEmpty {
print("\(window.identifier) \(window.app)")
} else {
Expand All @@ -228,15 +236,16 @@ extension NSWorkspace {
}

func window(identifiedAs windowIdentifier: CGWindowID) -> WindowInfo? {
allWindows().first {
allWindows(includeHidden: true).first {
$0.identifier == windowIdentifier
}
}

func allWindows() -> [WindowInfo] {
func allWindows(includeHidden: Bool) -> [WindowInfo] {
var windowInfos = [WindowInfo]()
let windows =
CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[String: Any]]
CGWindowListCopyWindowInfo(includeHidden ? .optionAll : .optionOnScreenOnly, kCGNullWindowID)
as? [[String: Any]]
for app in NSWorkspace.shared.runningApplications {
for window in windows ?? [] {
if let windowPid = window[kCGWindowOwnerPID as String] as? Int,
Expand Down Expand Up @@ -507,7 +516,7 @@ func resolveWindowID(_ windowIdentifier: String) -> CGWindowID {
if let identifier = CGWindowID(windowIdentifier) {
return identifier
}
if let window = NSWorkspace.shared.allWindows().filter({
if let window = NSWorkspace.shared.allWindows(includeHidden: true).filter({
$0.app.trimmingCharacters(in: .whitespacesAndNewlines)
.caseInsensitiveCompare(windowIdentifier.trimmingCharacters(in: .whitespacesAndNewlines))
== .orderedSame
Expand Down

0 comments on commit 77a4ebd

Please sign in to comment.