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

sol*_*ard 6 macos homebrew homebrew-cask

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


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

# /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
...
Run Code Online (Sandbox Code Playgroud)
# package names of apps available on brew-cask
alfred
appcleaner
bettertouchtool
calibre
cheatsheet
clip
...
Run Code Online (Sandbox Code Playgroud)

bfo*_*ine 5

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

\n
    \n
  1. 假设您的文件名都不包含换行符(不太可能),您可以通过组合命令.app来获取 JSON 数组形式的列表lsjq以 JSON 数组形式获取列表。然而,由于我们\xe2\x80\x99将使用该列表作为查找,所以\xe2\x80\x99最好创建一个对象:

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

    这将创建一个对象,其中每个应用程序作为键,并且1值(该值在这里并不重要)。它输出类似:

    \n
    {"1Password 7.app":1,"Amphetamine.app":1, "Firefox.app":1, \xe2\x80\xa6}\n
    Run Code Online (Sandbox Code Playgroud)\n
  2. \n
  3. 您可以使用 列出所有 3,500 多个可安装的木桶brew search --casks。为了获取描述一个或多个桶(包括.app它们安装的桶)的 JSON,您可以使用brew cask info --json=v1 <cask> \xe2\x80\xa6.

    \n

    将这两者结合起来,我们可以获得一个巨大的 JSON,描述所有可安装的木桶:

    \n
    brew search --casks \'\' \\\n| xargs brew info --cask --json=v2 \\\n> allcasks.json\n
    Run Code Online (Sandbox Code Playgroud)\n

    该命令在我的机器上大约需要 10 秒,因此将其保存在文件中是个好主意。

    \n
  4. \n
  5. 我们现在可以过滤此列表以仅提取安装的木桶.app我们现在可以过滤此列表,以仅从之前的列表中

    \n
    cat allcasks.json \\\n| jq -r --argjson list \'{\xe2\x80\xa6the list\xe2\x80\xa6}\' \'.[]|.[]|(.artifacts|map(.[]?|select(type=="string")|select(in($list)))|first) as $app|select($app)|"\\(.token): \\($app)"\'\n
    Run Code Online (Sandbox Code Playgroud)\n

    代替{\xe2\x80\xa6the list\xe2\x80\xa6}为我们之前创建的对象。

    \n

    这会打印如下内容:

    \n
    1password: 1Password 7.app\nfirefox: Firefox.app\ngoogle-chrome: Google Chrome.app\n\xe2\x80\xa6\n
    Run Code Online (Sandbox Code Playgroud)\n
  6. \n
\n
\n

如果您喜欢冒险,这里有一个可以同时执行所有这些命令的单行代码:

\n
1password: 1Password 7.app\nfirefox: Firefox.app\ngoogle-chrome: Google Chrome.app\n\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n
\n

细分jq

\n
.[] # flatten the list\n |  # then for each element:\n .[] # flatten the list\n |  # then for each element:\n   ( # take its artifacts\n     .artifacts\n      # then for each one of them\n      | map(\n         # take only arrays\n         .[]?\n         # select their string elements\n         | select(type=="string")\n         # that are also in the list\n         | select(in($list)\n       )\n   )\n   # take the first matching artifact\n   | first)\n   # and store it in $app\n   as $app\n # then take only the elements with a non-empty $app\n | select($app)\n # and print their name (.token) and the app ($app)\n |"\\(.token): \\($app)"\n
Run Code Online (Sandbox Code Playgroud)\n