简体   繁体   中英

Check if an app is installed on MacOS using the Terminal

I'm making a small cross-platform CLI tool in TypeScript/NodeJs. Its key feature requires that it needs to check which browsers are installed on the host. On Linux and Windows, it works flawlessly. 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.

I know it's a Unix-like OS but I can't test my tool. Can someone please help me because I don't physically own a MacBook so I cannot test which bash command should I use. 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.

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.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.
  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. Their locations cover the entire Spotlight index: normally the entire hard drive minus Spotlight exclusions.

For #1, expanding on @mschmidt's answer we can use, for example:

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

For #2 we can use:

mdfind -name 'Google Chrome.app'

This would find it anywhere in Spotlight and this is indeed what open -a can open.

Tested on Catalina.

The Mac OS is built on the Unix version Free BSD. Many Linux commands will probably work on the Mac as well. To access that part of the OS, the terminal emulators "Terminal" and "iTerm" can be used. The "which" command works fine on a Mac.

If your challenge is just to find the browsers on a Mac computer I's use the ls command piped into grep. Most apps on a Mac computer are stored in the Applications folder. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM