macos 如何从命令行启动 Mac OS X 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8387371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to startup a Mac OS X application from command line?
提问by yodaFirst Name Lulu
"open -a" is not the answer wanted, because I want to debug the Mac OS X application automatically. This means it's better if someone can give the command line like [program] [args]format. So ltrace mechanism can make [program]as target for debugging and take [args]as input.
“open -a”不是想要的答案,因为我想自动调试 Mac OS X 应用程序。这意味着如果有人可以提供像[program] [args]格式的命令行,那就更好了。所以ltrace机制可以将[program]作为调试目标,并以[args]作为输入。
I have tried command line like "/Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" /Users/poc.pptx, only Microsoft Point process started but the poc.pptx not opened.
我尝试过像“/Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint”/Users/poc.pptx 这样的命令行,只有 Microsoft Point 进程启动但 poc.pptx 没有打开。
After grepping the Microsoft Point with pptx file opened, it's something like: /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint -psn_0_307275, there is no argument "poc.pptx".
在打开 pptx 文件的情况下 grepping Microsoft Point 后,它类似于:/Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint -psn_0_307275,没有参数“poc.pptx”。
I even manually use "gdb /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" and "set args /Users/poc.pptx", and then "r", the target application can not run with the certain file opened.
我什至手动使用“gdb /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint”和“set args /Users/poc.pptx”,然后“r”,目标应用程序无法运行某个文件打开了。
I am confused about this, so, is there someone can help me to solve this problem?
我对此感到困惑,所以,有人可以帮助我解决这个问题吗?
Thank you!
谢谢!
回答by RealCasually
open -b com.microsoft.PowerPoint <filename>
seems to work for me to open presentations from the command line.
open -b com.microsoft.PowerPoint <filename>
从命令行打开演示文稿似乎对我有用。
回答by dadua
Go to file directory and then type
转到文件目录,然后键入
open -a "Microsoft PowerPoint" <filename.ppt>
Here "Microsoft PowerPoint" is the name of power point application, please check name of power point if it is different in your application directory.
这里的“Microsoft PowerPoint”是power point 应用程序的名称,如果您的应用程序目录中的power point 名称不同,请检查power point 的名称。
This is working perfectly fine on my MAC (OSX 10.8).
这在我的 MAC (OSX 10.8) 上运行良好。
We can also give complete path instead of just file name.
我们还可以提供完整的路径,而不仅仅是文件名。
open -a "Microsoft PowerPoint" <ppt file path>
This is also working fine.
这也工作正常。
回答by Monolo
Not sure if this will help you (depends on how you want to do your debugging), but you can use AppleScript from the command line, like this:
不确定这是否对您有帮助(取决于您希望如何进行调试),但您可以从命令行使用 AppleScript,如下所示:
%osascript <<<EOD
tell application "Excel" to open "Users:xxx:Documents:sheet.xls"
EOD
When entered this way, your script can contain several lines, it does not have to be limited to a single one.
以这种方式输入时,您的脚本可以包含多行,而不必仅限于一行。
回答by Ken Thomases
If PowerPoint is not opening a document passed as a command-line argument, then that's a reflection on how PowerPoint was coded. There's nothing anybody but Microsoft can do about that.
如果 PowerPoint 没有打开作为命令行参数传递的文档,那么这反映了 PowerPoint 的编码方式。对此,除了微软之外,任何人都无能为力。
The OS does not normally use that technique to tell applications to open documents. Instead, it passes Apple Events to the application. Cocoa will, by default, accept command-line arguments and treat them similarly to such Apple Events, but apparently PowerPoint is overriding that default behavior.
操作系统通常不会使用该技术来告诉应用程序打开文档。相反,它将 Apple 事件传递给应用程序。默认情况下,Cocoa 将接受命令行参数并将它们与此类 Apple 事件类似地对待,但显然 PowerPoint 正在覆盖该默认行为。
If you want to debug or trace PowerPoint, I recommend that you do it in two steps. First, launch it without arguments under the debugger or trace program. Then, tell it to open a document. You can do that in the normal way, using the Finder and/or Dock, or you can use open -a ...
. Such a request to open a document will not launch a second instance of PowerPoint, it will deliver an event to the already-running PowerPoint which you are debugging/tracing. So, the result should be similar to what you seem to want.
如果您想调试或跟踪 PowerPoint,我建议您分两步进行。首先,在调试器或跟踪程序下不带参数启动它。然后,告诉它打开一个文档。您可以使用 Finder 和/或 Dock 以正常方式执行此操作,也可以使用open -a ...
. 此类打开文档的请求不会启动 PowerPoint 的第二个实例,它会将事件传送到您正在调试/跟踪的已运行的 PowerPoint。因此,结果应该与您想要的相似。
回答by Sven van Zoelen
I know this is a old question, but here is my 2ct anyway.
I add the applications I want to open through command line in /usr/local/bin
as a symlink.
我知道这是一个老问题,但无论如何这是我的 2ct。我将要通过命令行打开的应用程序添加/usr/local/bin
为符号链接。
I never run into any problems, but as Ken stated it depends how a application handles arguments.
我从未遇到任何问题,但正如 Ken 所说,这取决于应用程序如何处理参数。
Example with Visual Studio:
Visual Studio 示例:
First I check what makes the application start bij executing the file inside the App contents like:
首先,我检查是什么让应用程序开始执行应用程序内容中的文件,例如:
$ /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
$ /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
If that works, then I create the symlink as follows (ln -s <path-to-app> <path-to-symlink>
):
如果可行,那么我会按如下方式创建符号链接 ( ln -s <path-to-app> <path-to-symlink>
):
$ ln -s /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron /usr/local/bin/vs
$ ln -s /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron /usr/local/bin/vs
After that I can start up Visual Studio with the current folder loaded as:
之后,我可以启动 Visual Studio,并将当前文件夹加载为:
~/Development/SomeProject $ vs .
~/Development/SomeProject $ vs .