Private/Get-AppMetadata.ps1
|
function Get-AppMetadata { param( [string]$BundlePath, [string]$ExecutablePath, [string]$FallbackName ) $infoPlist = "" if (-not [string]::IsNullOrWhiteSpace($BundlePath)) { $infoPlist = Join-Path $BundlePath "Contents/Info.plist" } # Single plutil call reads entire plist as JSON — replaces 8 PlistBuddy spawns $plist = Get-PlistData -PlistPath $infoPlist $bundleDisplayName = [string]$plist.CFBundleDisplayName $bundleName = [string]$plist.CFBundleName $bundleExecutable = [string]$plist.CFBundleExecutable $bundleId = [string]$plist.CFBundleIdentifier $bundleVersion = [string]$plist.CFBundleVersion $bundleShortVersion = [string]$plist.CFBundleShortVersionString $copyright = [string]$plist.NSHumanReadableCopyright $getInfo = [string]$plist.CFBundleGetInfoString # Single mdls call with all needed keys — replaces 3 separate mdls spawns $mdDisplayName = "" $mdAuthors = "" $mdVersion = "" if (-not [string]::IsNullOrWhiteSpace($BundlePath) -and (Test-Path -LiteralPath $BundlePath)) { $mdlsRaw = /usr/bin/mdls -name kMDItemDisplayName -name kMDItemAuthors -name kMDItemVersion "$BundlePath" 2>$null foreach ($line in $mdlsRaw) { if ($line -match '^kMDItemDisplayName\s*=\s*"(.+)"$') { $mdDisplayName = $Matches[1] } elseif ($line -match '^kMDItemAuthors\s*=\s*\(\s*"(.+)"\s*\)$') { $mdAuthors = $Matches[1] } elseif ($line -match '^kMDItemVersion\s*=\s*"(.+)"$') { $mdVersion = $Matches[1] } } } $displayName = @( $bundleDisplayName, $mdDisplayName, $bundleName, ($FallbackName -replace "\.app$", ""), (Split-Path -Leaf $ExecutablePath) ) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -First 1 $version = @( $bundleShortVersion, $mdVersion, $bundleVersion ) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -First 1 [pscustomobject]@{ DisplayName = $displayName BundleDisplayName = $bundleDisplayName BundleName = $bundleName BundleExecutable = $bundleExecutable BundleId = $bundleId BundleVersion = $bundleVersion BundleShortVersion = $bundleShortVersion Version = $version MdDisplayName = $mdDisplayName MdAuthors = $mdAuthors Copyright = $copyright GetInfoString = $getInfo } } |