[macOS] Use mas CLI to install Mac App for scripting and automation

mas

mas is a simple command line interface for the Mac App Store - https://www.apple.com/app-store/. Designed for scripting and automation.

Installation

Homebrew

Homebrew - https://brew.sh/ is the preferred way to install:

1
$ brew install mas

Usage

Each application in the Mac App Store has a product identifier which is also used for mas-cli commands. Using mas list will show all installed applications and their product identifiers.

1
2
3
4
$ mas list
446107677 Screens
407963104 Pixelmator
497799835 Xcode

It is possible to search for applications by name using mas search which will search the Mac App Store and return matching identifiers. Include the --price flag to include prices in the result.

1
2
3
4
5
$ mas search Xcode
497799835 Xcode
688199928 Docs for Xcode
449589707 Dash 3 - API Docs & Snippets. Integrates with Xcode, Alfred, TextWrangler and many more.
[...]

To install or update an application simply run mas install with an application identifier:

1
2
3
$ mas install 808809998
==> Downloading PaintCode 2
==> Installed PaintCode 2

If you want to install the first result that the search command returns, use the lucky command.

1
2
3
$ mas lucky twitter
==> Downloading Twitter
==> Installed Twitter

Please note that this command will not allow you to install (or even purchase) an app for the first time: use the purchase command in that case.


1
2
3
$ mas purchase 768053424
==> Downloading Gapplin
==> Installed Gapplin

Please note that you may have to re-authenticate yourself in the App Store to complete the purchase. This is the case if the application is not free or if you configured your account not to remember the credentials for free purchases.


Use mas outdated to list all applications with pending updates.

1
2
3
$ mas outdated
497799835 Xcode (7.0)
446107677 Screens VNC - Access Your Computer From Anywhere (3.6.7)

mas is only able to install/update applications that are listed in the Mac App Store itself. Use softwareupdate(8) utility for downloading system updates (like iTunes, Xcode Command Line Tools, etc)

To install all pending updates run mas upgrade.

1
2
3
4
5
6
7
$ mas upgrade
Upgrading 2 outdated applications:
Xcode (7.0), Screens VNC - Access Your Computer From Anywhere (3.6.7)
==> Downloading Xcode
==> Installed Xcode
==> Downloading iFlicks
==> Installed iFlicks

Updates can be performed selectively by providing the app identifier(s) to mas upgrade

1
2
3
4
5
$ mas upgrade 715768417
Upgrading 1 outdated application:
Xcode (8.0)
==> Downloading Xcode
==> Installed Xcode

Sign-in

To sign into the Mac App Store for the first time run mas signin.

1
2
3
$ mas signin [email protected]
==> Signing in to Apple ID: [email protected]
Password:

⚠️ Due to breaking changes in the underlying API that mas uses to interact with the Mac App Store, the signin command has been temporarily disabled on macOS 10.13+ ⛔. For more information on this issue, see #164.


If you experience issues signing in this way, you can ask to signin using a graphical dialog (provided by Mac App Store application):

1
2
$ mas signin --dialog [email protected]
==> Signing in to Apple ID: [email protected]

You can also embed your password in the command.

1
2
$ mas signin [email protected] 'ZdkM4f$gzF;gX3ABXNLf8KcCt.x.np'
==> Signing in to Apple ID: [email protected]

Use mas signout to sign out from the Mac App Store.

1
$ mas signout

When something doesn’t work

If you see the error “This redownload is not available for this Apple ID either because it was bought by a different user of the item was refunded or cancelled.”, it’s probably because you haven’t installed the app through the App Store yet. See #46 - https://github.com/mas-cli/mas/issues/46#issuecomment-248581233.

If mas doesn’t work for you as expected (e.g. you can’t update/download apps), run mas reset and try again. If the issue persists, please file a bug - https://github.com/mas-cli/mas/issues/new! All your feedback is much appreciated ✨

Using tmux

mas operates via the same system services as the Mac App Store. These exist as separate processes with communication through XPC. As a result of this, mas experiences similar problems as the pasteboard when running inside tmux. A wrapper tool exists - https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard to fix pasteboard behaviour which also works for mas.

You should consider configuring tmux to use the wrapper but if you do not wish to do this it can be used on a one-off basis as follows:

1
2
3
$ brew install reattach-to-user-namespace

$ reattach-to-user-namespace mas install

References

[1] mas-cli/mas: Mac App Store command line interface - https://github.com/mas-cli/mas

[2] Homebrew - https://brew.sh/