简体   繁体   English

使用终端检查 MacOS 上是否安装了应用程序

[英]Check if an app is installed on MacOS using the Terminal

I'm making a small cross-platform CLI tool in TypeScript/NodeJs.我正在用 TypeScript/NodeJs 制作一个小的跨平台 CLI 工具。 Its key feature requires that it needs to check which browsers are installed on the host.它的关键特性要求它需要检查主机上安装了哪些浏览器。 On Linux and Windows, it works flawlessly.在 Linux 和 Windows 上,它可以完美运行。 I did a lot of research on how I can achieve that and currently I'm using "reg query" for Windows and "which" for Unix-based OS - Linux, but I don't know where to start with MacOS.我对如何实现这一目标进行了大量研究,目前我正在使用 Windows 的“reg query”和基于 Unix 的操作系统 - Linux 的“which”,但我不知道从哪里开始使用 MacOS。

I know it's a Unix-like OS but I can't test my tool.我知道这是一个类 Unix 操作系统,但我无法测试我的工具。 Can someone please help me because I don't physically own a MacBook so I cannot test which bash command should I use.有人可以帮助我,因为我实际上没有 MacBook,所以我无法测试我应该使用哪个 bash 命令。 I'm not asking you to write the complete code, just which command(s) to use or even better, a working example (screenshot) of the command that works properly.我不是要你写完整的代码,只是要使用哪个命令,或者甚至更好,一个正常工作的命令的工作示例(屏幕截图)。 I know I could use "which", "type" or even "open -Ra" (not sure about this one) I just don't know what's their output.我知道我可以使用“which”、“type”甚至“open -Ra”(不确定这个)我只是不知道他们的输出是什么。

The command:命令:

mdfind "kMDItemKind == 'Application'"

will output a list of installed Apps on the system (one per line) with their paths.将输出系统上已安装的应用程序列表(每行一个)及其路径。 Eg:例如:

/Applications/Safari Technology Preview.app /Applications/Safari Technology Preview.app
/Applications/Safari.app /应用程序/Safari.app

You can search your supported browsers in this list.您可以在此列表中搜索您支持的浏览器。

It depends on what is meant by "installed".这取决于“已安装”的含义。 I can think of two different definitions:我可以想到两种不同的定义:

  1. Apps appearing in Launchpad.出现在 Launchpad 中的应用程序。
  2. Apps appearing in System Information > Applications which could be launched via open -a {app} from a terminal or navigating to the app folder and double-clicking on it.出现在系统信息 > 应用程序中的应用程序可以通过从终端open -a {app}或导航到应用程序文件夹并双击它来启动。 Their locations cover the entire Spotlight index: normally the entire hard drive minus Spotlight exclusions.它们的位置覆盖整个 Spotlight 索引:通常是整个硬盘驱动器减去 Spotlight 排除项。

For #1, expanding on @mschmidt's answer we can use, for example:对于#1,我们可以扩展@mschmidt 的答案,例如:

mdfind -name 'Google Chrome.app' -onlyin /Applications -onlyin ~/Applications -onlyin /System/Applications

(I'm not positive that those are the only roots used by Launchpad.) (我不确定这些是 Launchpad 使用的唯一根。)

For #2 we can use:对于#2,我们可以使用:

mdfind -name 'Google Chrome.app'

This would find it anywhere in Spotlight and this is indeed what open -a can open.这可以在 Spotlight 的任何地方找到它,这确实是open -a可以打开的。

Tested on Catalina.在 Catalina 上测试。

The Mac OS is built on the Unix version Free BSD. Mac OS 建立在 Unix 版本的 Free BSD 上。 Many Linux commands will probably work on the Mac as well.许多 Linux 命令可能也适用于 Mac。 To access that part of the OS, the terminal emulators "Terminal" and "iTerm" can be used.要访问操作系统的该部分,可以使用终端仿真器“Terminal”和“iTerm”。 The "which" command works fine on a Mac. “which”命令在 Mac 上运行良好。

If your challenge is just to find the browsers on a Mac computer I's use the ls command piped into grep.如果您的挑战只是在 Mac 计算机上找到浏览器,我将使用通过管道传输到 grep 的 ls 命令。 Most apps on a Mac computer are stored in the Applications folder. Mac 计算机上的大多数应用程序都存储在“应用程序”文件夹中。 This folder is located at the root level.此文件夹位于根级别。 To find a specific browser I'd type:要查找特定的浏览器,我会输入:

> cd Applications 
> ls -l | grep -i BrowserName

You can do this for each browser you know of.您可以为您知道的每个浏览器执行此操作。 I don't know how to find browsers in general.我不知道如何找到一般的浏览器。 If you do you may just try the same command on the Mac.如果你这样做了,你可以在 Mac 上尝试相同的命令。

If you need more help feel free to leave a comment.如果您需要更多帮助,请随时发表评论。

You can also use the following:您还可以使用以下内容:

mdfind -name 'kMDItemFSName=="*.app"' -onlyin /Volumes/Macintosh_HD/Applications/

or或者

mdfind -name 'kMDItemFSName=="*.app"' -onlyin /Volumes/Macintosh\ HD/Applications/ |cut -d "/" -f3|grep .app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 && 和 || 检查已安装的工具 - Using && and || to check installed tools 在 MacOS 上的 Terminal.app 中为自定义键绑定扩展可用键 - Extend available keys for custom key bindings in Terminal.app on MacOS 通过 brew 安装了 mysql 但终端找不到它。 在 macos 上别名的正确方法是什么? - Installed mysql via brew but terminal can't find it. What's the proper way to alias it on a macos? Emacs + OSX中的Bash找不到通过terminal.app安装的gem? - Bash in Emacs + OSX not finding gems installed via terminal.app? 如何使用正则表达式在MacOS终端中基于扩展名删除文件? - How to delete files based on the extension in MacOS terminal using regex? 使用 MacOS 终端连接到 SQL 服务器时登录失败 - Login failed while connecting to SQL server using MacOS terminal 使用MacOS终端在同一目录中的多个VCF文件上执行Plink - Execute Plink on multiple VCF files in the same directory using the MacOS terminal Bash 脚本 - 如何检查是否安装了 package 并根据终端结果执行操作 - Bash Script - How to check if a package is installed and perform action based on terminal results 如何仅使用 1 个终端检查来自 localhost 的应用程序响应 - How to check app response from localhost with only 1 terminal 在macOS上分发可执行终端命令 - Distributing executable terminal commands on macOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM