1. 简介

Mac 下手动打开目录、文件和应用程序等和 Windows 都很一不样,用惯 Windows 的用户刚开始使用 Mac 系统可能会非常不习惯。好在 Mac 下有一个非常方便的命令行工具 open,它能很方便地打开 Mac 上的目录、文件和应用程序。

2. 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-s <partial SDK name>][-b <bundle identifier>] [-a <application>] [-u URL] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
-s For -h, the SDK to use; if supplied, only SDKs whose names contain the argument value are searched.
Otherwise the highest versioned SDK in each platform is used.
-u, --url URL Open this URL, even if it matches exactly a filepath
-i, --stdin PATH Launches the application with stdin connected to PATH; defaults to /dev/null
-o, --stdout PATH Launches the application with /dev/stdout connected to PATH;
--stderr PATH Launches the application with /dev/stderr connected to PATH to
--env VAR Add an enviroment variable to the launched process, where VAR is formatted AAA=foo or just AAA for a null string value.

2.1 打开目录

1
open <path1> [<path2> ...]

其中,<path1> 为需要打开的目录,可以同时打开多个目录项,打开的目录最终是在 Finder 窗口显示。

2.2 打开文件

1
open <file1> [<file2> ...]

其中,<file1> 为需要打开的文件,可以同时打开多个文件项,以对应文件格式的默认应用打开对应文件。

2.3 定位文件

如果想定位文件所在位置并在 Finder 中打开对应目录,则可以使用以下命令行:

1
open -R <file1> [<file2> ...]

其中,<file1> 为需要定位的文件,可以同时定位多个文件项。

2.4 打开应用程序

1
open -a <app1> [<app2> ...]

其中,<app1> 为需要打开的应用程序,可以同时打开多个应用程序项。

2.5 打开 URLs

1
2
open <url1> [<url2> ...]
open -a Firefox <url1> [<url2> ...]

其中,<url1> 为需要打开的 URL,以默认浏览器打开;可以通过 -a 参数更换其它浏览器打开。

2.6 头文件

open 另一大用途是,定位并打开对应的头文件:

1
2
open -h <header1> [<header2> ...]
open -a TextEdit -h <header1> [<header2> ...]

其中,<header1> 为需要打开的头文件,以默认程序打开;可以通过 -a 参数更换其它应用程序打开。

附录