gpt4 book ai didi

macos - 列出可使用 brew-cask 安装的应用程序

转载 作者:行者123 更新时间:2023-12-03 23:07:45 43 4
gpt4 key购买 nike

我想知道我的哪些应用程序可以使用 brew cask 安装命令。
我该怎么做?

规范
我想要做的是从 /Applications 中的所有应用程序中提取 brew-cask 上也可用的应用程序并列出他们的包名。

# /Applications
Alfred 4.app
App Store.app
AppCleaner.app
Automator.app
Be Focused Pro.app
BetterTouchTool.app
Bitdefender
Bluetooth Explorer.app
Books.app
Calculator.app
Calendar.app
CheatSheet.app
Chess.app
Clipy.app
...
# package names of apps available on brew-cask
alfred
appcleaner
bettertouchtool
calibre
cheatsheet
clip
...

最佳答案

这可以使用 Homebrew 的 JSON API 以及一些 jq 来实现。魔法( brew install jq )。

1- 假设您没有 .app文件名包含换行符(不太可能),您可以使用 a command combining ls and jq 将列表作为 JSON 数组获取.但是,由于我们将使用该列表作为查找,因此最好创建一个对象:

ls /Applications | \grep '\.app$' | jq -Rsc 'split("\n")[:-1]|map({(.):1})|add'

这将创建一个以每个应用程序为键的对象和 1作为一个值(这里的值不重要)。它输出类似:
{"1Password 7.app":1,"Amphetamine.app":1, "Firefox.app":1, …}

2- 您可以使用 brew search --casks 列出所有 3,500 多个可安装的木桶.为了获得描述一个或多个木桶的 JSON,包括 .app他们安装,你可以使用 brew cask info --json=v1 <cask> … .

结合这两者,我们可以得到一个巨大的 JSON 描述所有可安装的桶:
brew search --casks | xargs brew cask info --json=v1 > allcasks.json

这个命令在我的机器上需要大约 10 秒,所以将它保存在一个文件中是个好主意。

3- 我们现在可以过滤此列表以仅提取安装 .app 的木桶s 来自我们之前的列表:
cat allcasks.json | jq -r --argjson list '{…the list…}' '.[]|(.artifacts|map(.[]?|select(type=="string")|select(in($list)))|first) as $app|select($app)|"\(.token): \($app)"'

替换 {…the list…}使用我们之前创建的对象。

这会打印如下内容:
1password: 1Password 7.app
firefox: Firefox.app
google-chrome: Google Chrome.app


如果您喜欢冒险,这里有一个一次性执行所有这些命令:
brew search --casks|xargs brew cask info --json=v1|jq -r --argjson l "$(ls /Applications|\grep '\.app$'|jq -Rsc 'split("\n")[:-1]|map({(.):1})|add')" '.[]|(.artifacts|map(.[]?|select(type=="string")|select(in($l)))|first) as $a|select($a)|"\(.token): \($a)"'
jq的故障命令:
.[] # flatten the list
| # then for each element:
( # take its artifacts
.artifacts
# then for each one of them
| map(
# take only arrays
.[]?
# select their string elements
| select(type=="string")
# that are also in the list
| select(in($list)
)
)
# take the first matching artifact
| first)
# and store it in $app
as $app
# then take only the elements with a non-empty $app
| select($app)
# and print their name (.token) and the app ($app)
|"\(.token): \($app)"

关于macos - 列出可使用 brew-cask 安装的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61218987/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com