【问题标题】:List up my apps installable with brew-cask列出我可以使用 brew-cask 安装的应用程序
【发布时间】:2020-07-27 20:20:42
【问题描述】:

我想知道我的哪些应用可以使用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
...

【问题讨论】:

  • 你能详细说明一下吗?也许您正在尝试查看您已已安装cask 应用程序列表,* 或当前安装的应用程序可以*升级?
  • 所有应用程序都是手动安装的(我今天知道 brew-cask)。我想知道它们中的哪些也可以从 brew-cask 获得。
  • 看看this answer有没有用,听起来和你想做的差不多
  • @solzard 我添加了一个答案,希望对你有帮助:)

标签: macos homebrew homebrew-cask


【解决方案1】:

这可以使用 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- 我们现在可以过滤此列表以仅从之前的列表中提取安装 .apps 的木桶:

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)"

【讨论】:

  • 看起来这不再适用于 Homebrew >=3.0.0,因为 brew cask 命令不再存在。此外,brew info --cask 仅接受 v2 用于 json 标志,这似乎不适用于提供的 jq 命令:$ brew search --casks | xargs brew info --cask --json=v2 > allcasks.json 然后 cat allcasks.json | jq -r --argjson list '{[…]}' '.[]|(.artifacts|map(.[]?|select(type=="string")|select(in($list)))|first) as $app|select($app)|"\(.token): \($app)"' 失败并出现 jq: error (at <stdin>:1): Cannot index array with string "artifacts"
【解决方案2】:

您可以在终端上使用brew search,例如这些示例:

  • brew search vlc
  • brew search mamp
  • brew search slack
  • ...等

您将获得与您的搜索相对应的可用酿造桶,您可以使用brew cask install mamp 安装它(用您自己的应用替换mamp

您也可以访问此页面https://formulae.brew.sh/cask/ 查看所有可用的木桶。

如果您的应用已经安装,您需要使用brew cask install --force mamp强制重新安装并链接您已安装的应用。

更多解释,你可以去这里https://sourabhbajaj.com/mac-setup/Homebrew/Cask.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多