简体   繁体   English

可执行文件是否总是在MacOS上打开终端窗口?

[英]Do executable files always open a terminal window on MacOS?

I'm on MacOS X, and I'm pretty new to app-bundle-type things. 我在MacOS X上,对于应用程序捆绑类型的东西还很陌生。 I am writing a program that opens a window and registers mouse input -- not a command line tool. 我正在编写一个打开窗口并注册鼠标输入的程序-而不是命令行工具。 When I compile my code (written in C, if that is important) into an executable file (a "unix executable file") and then run that file directly (by double clicking it, for example), a terminal window pops up, and then the program's window pops up. 当我将代码(用C编写,如果很重要)编译为可执行文件(“ unix可执行文件”),然后直接运行该文件(例如,双击该文件)时,将弹出一个终端窗口,然后然后程序窗口弹出。 Likewise, if I navigate to the directory of the executable and open it from the command line, it pops open /another/ terminal window and then the program's window. 同样,如果我导航到可执行文件的目录并从命令行打开它,则它会先打开/ another /终端窗口,然后是程序窗口。

However, if I wrap the executable in a bundle (thus, I suppose, turning it into a proper "app"), then when I run the app, either by double clicking or from the command line, the program's window opens and no new terminal window is created. 但是,如果我将可执行文件打包成一个包(因此,我想将其变成一个适当的“应用程序”),那么当我通过双击或从命令行运行该应用程序时,该程序的窗口将打开且没有新窗口终端窗口已创建。 Is this merely a property of the "app bundle"'s architecture? 这仅仅是“应用程序捆绑包”体系结构的一个属性吗? Or is there a way that I can run the raw executable without incurring another terminal window? 还是有一种方法可以运行原始可执行文件而不会引起另一个终端窗口? I suspect that I'm misunderstanding something fundamental. 我怀疑我误解了一些基本知识。 Thanks in advance! 提前致谢!

I believe what you're seeing is correct. 我相信您所看到的是正确的。 In order for a separate window to not pop-up, you'd need to encapsulate it into a bundle. 为了不弹出单独的窗口,您需要将其封装到一个包中。

Launching by double-clicking a bundle, or using the 'open' command from Terminal uses Apple's Launch Services , which maintains a list of known (registered) applications. 双击捆绑包或使用Terminal中的“ open”命令启动 ,将使用Apple的Launch Services ,其中维护了已知(注册)应用程序的列表。 When an application (bundle) is introduced to the system, it is registered with Launch Services and associated with its URI (eg com.apple.calculator), which is present in the bundle's Info.plist 将应用程序(捆绑包)引入系统后,它会在Launch Services中注册并与其URI(例如com.apple.calculator)相关联,该URI出现在捆绑包的Info.plist中

Other items in the Info.plist tell launch services how to handle the application, such as checking if the minimum or maximum version of the OS has been exceeded, or whether or not to display a dock item. Info.plist中的其他项告诉启动服务如何处理应用程序,例如检查是否已超过操作系统的最低或最高版本,或者是否显示停靠项。

A lone binary doesn't have an associated Info.plist manifest, so its behaviour can't be varied and a Terminal window is opened. 一个单独的二进制文件没有关联的Info.plist清单,因此其行为无法更改,并且打开了Terminal窗口。

Bare executable files are essentially treated as documents by Launch Services (the framework that the Finder, Dock, and open command use when you open stuff). 原始可执行文件在本质上被Launch Services(当您打开内容时Finder,Dock和open命令使用的框架)视为文档。 The application which handles such documents is Terminal. 处理此类文档的应用程序是Terminal。 So, when you open an executable in that fashion, it launches Terminal if it's not already running and tells it to open the document. 因此,当您以这种方式打开可执行文件时,如果可执行文件尚未运行,它将启动Terminal并告诉其打开文档。 Terminal does this by opening a new shell window and auto-typing the path to the "document" as a command line. 终端通过打开一个新的shell窗口并以命令行自动键入“文档”的路径来实现此目的。

Launch Services handles bundled apps as, well, apps. Launch Services还可以处理捆绑的应用程序。 If the app is already running, it activates it and possibly has it open a new untitled window. 如果该应用程序已经在运行,则将其激活,并可能会打开一个新的无标题窗口。 Otherwise, it launches it. 否则,它将启动它。

As Rob Napier notes in the comments, if you run an executable directly from the command line (or if some already-running app launches it using NSTask or fork+exec), it will simply run. 正如Rob Napier在评论中指出的那样,如果直接从命令行运行可执行文件(或者如果某些已经在运行的应用程序使用NSTask或fork + exec启动它),它将简单地运行。 Launch Services won't be involved, so Terminal will not be asked to open the executable as a document. 不会涉及启动服务,因此不会要求终端将可执行文件作为文档打开。

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

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