diff --git a/.github/scripts/assert.sh b/.github/scripts/assert.sh new file mode 100755 index 0000000..6f977af --- /dev/null +++ b/.github/scripts/assert.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -e +cd ~/output || exit 1 +until [ -f ci-results.json ] +do + printf "waiting for pdubs results..." + sleep 15 +done +jq --exit-status .[0].kCGWindowOwnerName ci-results.json >/dev/null + diff --git a/.github/scripts/release.sh b/.github/scripts/release.sh new file mode 100755 index 0000000..5b09640 --- /dev/null +++ b/.github/scripts/release.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -ex +swift build -c release --arch arm64 --arch x86_64 +cd .build/apple/Products/Release || exit 1 +tar -czvf pdubs.tar.gz pdubs +shasum --algorithm 256 pdubs.tar.gz | tee pdubs.tar.gz.sha256 +shasum -c pdubs.tar.gz.sha256 + diff --git a/.github/scripts/terminal-pdubs.sh b/.github/scripts/terminal-pdubs.sh new file mode 100755 index 0000000..a47cea8 --- /dev/null +++ b/.github/scripts/terminal-pdubs.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +cd "$GITHUB_WORKSPACE" || exit 1 +swift run 1> ~/output/ci-results.json 2> ~/output/ci-stderr.log + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0d39be1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: ci +on: + workflow_dispatch: + pull_request: + branches: + - 'main' +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-13, macos-12, macos-11] + timeout-minutes: 5 + steps: + - uses: actions/checkout@v2 + + - name: Test terminal app window properties + shell: bash + run: | + set -x + swift build + mkdir -p ~/output + cd .github/scripts + chmod 777 assert.sh + chmod 777 terminal-pdubs.sh + open -a Terminal "$GITHUB_WORKSPACE/.github/scripts/terminal-pdubs.sh" + ./assert.sh + ls -la ~/output + cat ~/output/ci-results.json + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: output ${{ matrix.os }} + path: ~/output + if-no-files-found: error + + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..812052e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,50 @@ +name: Release +on: + workflow_dispatch: + push: + branches: + - 'main' + paths: + - 'Sources/**' +jobs: + build-and-release: + runs-on: macos-13 + permissions: + contents: write + issues: write + pull-requests: write + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Build release artifacts + shell: bash + run: | + set -x + mkdir -p ~/output + .github/scripts/release.sh + cd .build/apple/Products/Release + cp pdubs.tar.gz pdubs.tar.gz.sha256 ~/output + ls -la ~/output + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: output ${{ matrix.os }} + path: ~/output + if-no-files-found: error + + - uses: actions/setup-node@v3 + with: + node-version: lts/* + + - run: | + npm install @semantic-release/git @semantic-release/changelog -D + npx semantic-release + env: + GH_TOKEN: ${{ secrets.PDUBS_CI_TOKEN }} + GITHUB_TOKEN: ${{ secrets.PDUBS_CI_TOKEN }} + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/.releaserc b/.releaserc new file mode 100644 index 0000000..c2974d1 --- /dev/null +++ b/.releaserc @@ -0,0 +1,37 @@ +{ + "branches": "main", + "plugins": [ + "@semantic-release/commit-analyzer", + "@semantic-release/release-notes-generator", + [ + "@semantic-release/changelog", + { + "changelogFile": "CHANGELOG.md" + } + ], + [ + "@semantic-release/git", + { + "assets": [ + "CHANGELOG.md" + ], + "message": "chore(release): ${nextRelease.version}\n\n${nextRelease.notes}" + } + ], + [ + "@semantic-release/github", + { + "assets": [ + { + "path": ".build/apple/Products/Release/pdubs.tar.gz", + "label": "pdubs.tar.gz" + }, + { + "path": ".build/apple/Products/Release/pdubs.tar.gz.sha256", + "label": "pdubs.tar.gz.sha256" + } + ] + } + ] + ] +} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..d1ea99b --- /dev/null +++ b/Package.swift @@ -0,0 +1,15 @@ +// swift-tools-version: 5.5 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "pdubs", + dependencies: [], + targets: [ + + .executableTarget( + name: "pdubs", + dependencies: []), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..3666f59 --- /dev/null +++ b/README.md @@ -0,0 +1,149 @@ +# 🦬 pdubs +pdubs is a simple command-line utility to return macos window information for a given pid. If a given pid does not have an associated window, then it will check all of its ancestors. The window information for the first pid that is associated will be returned. The window information is a list in JSON format. + +You may supply one optional parameter providing the pid. If no parameter is provided, then it will search for the current processes pid. + +## 🤔 Motivation +I wanted an easy way to get the window ID of my current process so that I could take a screenshot from the command-line with [screencapture](https://ss64.com/osx/screencapture.html). +### screenshot example +```bash +win=$(./pdubs | jq .[0].kCGWindowNumber); screencapture -l"$win" pdubs.png +``` +![pdubs](https://github.com/mikesmithgh/pdubs/assets/10135646/5e389586-717a-4f60-9f59-30a40eea1548) + +## 📦 Installation + +### Download the binary for your system +```sh +curl --silent --fail --location --output pdubs.tar.gz https://github.com/mikesmithgh/pdubs/releases/latest/download/pdubs.tar.gz +curl --silent --fail --location --output pdubs.tar.gz.sha256 https://github.com/mikesmithgh/pdubs/releases/latest/download/pdubs.tar.gz.sha256 +if shasum -c pdubs.tar.gz.sha256; then + tar -xvf pdubs.tar.gz +else + rm pdubs.tar.gz +fi +``` +Move the binary `pdubs` to the desired location and place on your `$PATH` + +For example, +```sh +mv pdubs ~/bin +``` + +## 🍎 Supported OS versions +- macOS 13 Ventura +- macOS 12 Monterey +- macOS 11 Big Sur + +## 🔨 Swift commands + +### debug build +```sh +swift build +``` + +### release build +```sh +swift build -c release --arch arm64 --arch x86_64 +cd .build/apple/Products/Release || exit 1 +tar -czvf pdubs.tar.gz pdubs +shasum --algorithm 256 pdubs.tar.gz | tee pdubs.tar.gz.sha256 +``` + +### run +```sh +swift run +``` + +## 👩‍💻 Usage examples + +### current process +```sh +./pdubs +``` +```json +[ + { + "kCGWindowName" : "./pdubs", + "kCGWindowStoreType" : 1, + "kCGWindowOwnerName" : "kitty", + "kCGWindowAlpha" : 1, + "kCGWindowSharingState" : 1, + "kCGWindowBounds" : { + "X" : 3440, + "Height" : 1055, + "Y" : 385, + "Width" : 1920 + }, + "kCGWindowIsOnscreen" : true, + "kCGWindowOwnerPID" : 57175, + "kCGWindowNumber" : 9382, + "kCGWindowMemoryUsage" : 2288, + "kCGWindowLayer" : 0 + }, + { + "kCGWindowStoreType" : 1, + "kCGWindowName" : "vi README.md ", + "kCGWindowLayer" : 0, + "kCGWindowOwnerName" : "kitty", + "kCGWindowOwnerPID" : 57175, + "kCGWindowMemoryUsage" : 2288, + "kCGWindowNumber" : 9432, + "kCGWindowSharingState" : 1, + "kCGWindowIsOnscreen" : true, + "kCGWindowAlpha" : 1, + "kCGWindowBounds" : { + "X" : 1720, + "Height" : 1415, + "Y" : 25, + "Width" : 1720 + } + } +] +``` + +### target process +```sh +./pdubs 62556 +``` +```json +[ + { + "kCGWindowStoreType" : 1, + "kCGWindowNumber" : 9422, + "kCGWindowAlpha" : 1, + "kCGWindowBounds" : { + "X" : 0, + "Height" : 1415, + "Y" : 25, + "Width" : 1720 + }, + "kCGWindowMemoryUsage" : 2288, + "kCGWindowOwnerPID" : 62553, + "kCGWindowLayer" : 0, + "kCGWindowSharingState" : 1, + "kCGWindowName" : "-bash", + "kCGWindowIsOnscreen" : true, + "kCGWindowOwnerName" : "iTerm2" + } +] +``` + +## 🕵️ Troubleshooting + +### Developer cannot be verified warning +``` +"pdubs" cannot be opened because the developer cannot be verified. +``` +If you receive the warning message while trying to execute pdubs, this is most likely because you manually downloaded the file from the release page. Depending on your download method, Apple will quarantine an app if it is not by an identified developer. I do not have an Apple developer account which is why you will see this warning. + +Before removing the app from quarantine, please verify the checksum has not been changed with the `shasum` command. If this fails, then delete and download pdubs from the release page. +```sh +shasum -c pdubs.tar.gz.sha256 +``` + +You can manually resolves the quarantine by control-clicking and opening the application from Finder or via the following command. +```sh +xattr -d com.apple.quarantine pdubs +``` +See [What should I do about com.apple.quarantine?](https://superuser.com/questions/28384/what-should-i-do-about-com-apple-quarantine) for additional information. diff --git a/Sources/pdubs/pdubs.swift b/Sources/pdubs/pdubs.swift new file mode 100644 index 0000000..6f901ff --- /dev/null +++ b/Sources/pdubs/pdubs.swift @@ -0,0 +1,63 @@ +import Foundation +import AppKit + +@main +public struct pdubs { + + public static func main() { + let arguments = CommandLine.arguments + + var pid = ProcessInfo.processInfo.processIdentifier + if arguments.count > 1 { + pid = Int32(arguments[1]) ?? -1 + } + + var parentWindows: [[String: Any]] = [] + + while pid > 0 && parentWindows.isEmpty { + let onScreenWindows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as! [[String: Any]] + parentWindows = onScreenWindows.filter{ w in + return (w[kCGWindowOwnerPID as String] as? pid_t) == pid + } + + if !parentWindows.isEmpty { + do { + var formatting: JSONSerialization.WritingOptions = [ + .prettyPrinted + ] + + if #available(macOS 10.13, *) { + formatting.insert(.sortedKeys) + } + + if #available(macOS 10.15, *) { + formatting.insert(.withoutEscapingSlashes) + } + let jsonData = try JSONSerialization.data(withJSONObject: parentWindows, options: formatting) + + if let jsonString = String(data: jsonData, encoding: .utf8) { + print(jsonString) + } + } catch { + print("Error converting array to JSON: \(error.localizedDescription)") + } + } + + pid = getParentPID(forPID: pid) ?? -1 + } + } + + private static func getParentPID(forPID pid: Int32) -> Int32? { + var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid] + var info: kinfo_proc = kinfo_proc() + var size = MemoryLayout.stride + + let result = sysctl(&mib, u_int(mib.count), &info, &size, nil, 0) + if result == 0 { + let parentPID = info.kp_eproc.e_ppid + return parentPID + } else { + return nil + } + } +}