macos 使用命令行在 OS X 上启动应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1308755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:21:03  来源:igfitidea点击:

Launch an app on OS X with command line

macos

提问by psychotik

I want to launch an app on OSX from a script. I need pass it command line arguments. Unfortunately, opendoesn't accept command line args.

我想从脚本在 OSX 上启动一个应用程序。我需要传递它的命令行参数。不幸的是,open不接受命令行参数。

The only option I can think of is to use nohup myApp > /dev/null &to launch my app so it can exist independently of the script that launches it.

我能想到的唯一选择是用来nohup myApp > /dev/null &启动我的应用程序,这样它就可以独立于启动它的脚本而存在。

Any better suggestions?

有什么更好的建议吗?

采纳答案by Ned Deily

In OS X 10.6, the opencommand was enhanced to allow passing of arguments to the application:

在 OS X 10.6 中,该open命令得到了增强,以允许将参数传递给应用程序:

open ./AppName.app --args -AppCommandLineArg

But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Eventsfor files like herefor Cocoa apps or herefor Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.

但是对于旧版本的 Mac OS X,并且因为应用程序包不是为了传递命令行参数而设计的,所以传统的机制是使用Apple Events来处理文件,例如此处用于 Cocoa 应用程序或此处用于 Carbon 应用程序。您也可以通过在使用环境变量中传递参数来做一些笨拙的事情。

回答by John McDonnell

As was mentioned in the question here, the opencommand in 10.6 now has an argsflag, so you can call:

正如这里的问题中提到的open,10.6 中的命令现在有一个args标志,因此您可以调用:

open -n ./AppName.app --args -AppCommandLineArg

open -n ./AppName.app --args -AppCommandLineArg

回答by NSSec

An application bundle (a .appfile) is actually a bunch of directories. Instead of using openand the .appname, you can actually move in to it and start the actual binary. For instance:

一个应用程序包(一个.app文件)实际上是一堆目录。您实际上可以进入它并启动实际的二进制文件,而不是使用open.app名称。例如:

$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper

That is the actual binary that might accept arguments (or not, in LittleSnapper's case).

那是可能接受参数的实际二进制文件(或不接受,在LittleSnapper's 的情况下)。

回答by eshedg

In case your app needs to work on files (what you would normally expect to pass as: ./myApp *.jpg), you would do it like this:

如果您的应用程序需要处理文件(您通常希望传递为:)./myApp *.jpg,您可以这样做:

open *.jpg -a myApp

回答by Paul Irish

I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:

我会推荐 MathieuK 提供的技术。就我而言,我需要使用 Chromium 进行尝试:

> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts

I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)

我意识到这并不能解决 OP 的问题,但希望它可以节省其他人的时间。:)

回答by George

I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChromefor normal browsing and altChrometo use the special profile, to be easily started by keying ?-space to bring up Spotlight, then 'reg' or 'alt', then Enter.

我想要运行两个单独的 Chrome 实例,每个实例都使用自己的配置文件。我希望能够从Spotlight启动它们,这是我启动 Mac 应用程序的习惯。换句话说,我需要两个常规的 Mac 应用程序,regChrome用于正常浏览和altChrome使用特殊配置文件,通过键入 ?-space 来打开 Spotlight,然后按“reg”或“alt”,然后按 Enter 即可轻松启动。

I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.

我想实现上述目标的蛮力方法是在各自的名称下制作两个 Google Chrome 应用程序包的副本。但这很丑陋并且使更新复杂化。

What I ended up with was two AppleScriptapplications containing two commands each. Here is the one for altChrome:

我最终得到的是两个AppleScript应用程序,每个应用程序包含两个命令。这是一个altChrome

do shell script "cd /Applications/Google\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
do shell script "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"

The second line starts Chrome with the alternate profile (the --user-data-dirparameter).

第二行以备用配置文件(--user-data-dir参数)开始 Chrome 。

The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.

第一行尝试为两个应用程序提供不同的图标,但未成功。最初,它似乎工作正常。但是,Chrome迟早会重新读取其图标文件,并获取与最后启动的两个应用程序中的任何一个相对应的图标文件,从而导致两个运行的应用程序具有相同的图标。但我并没有费心去尝试修复它——我将两个浏览器放在不同的桌面上,在它们之间导航也不是问题。

回答by Let Me Tink About It

Beginning with OS X Yosemite, we can now use AppleScriptand Automatorto automate complex tasks. JavaScript for automation can now be used as the scripting language.

OS X Yosemite开始,我们现在可以使用AppleScriptAutomator来自动化复杂的任务。用于自动化的 JavaScript 现在可以用作脚本语言。

This page gives a good example example scriptthat can be written at the command line using bash and osascriptinteractive mode. It opens a Safari tab and navigates to example.com.

此页面提供了一个很好的示例脚本,可以使用 bash 和osascript交互模式在命令行中编写。它会打开一个 Safari 选项卡并导航到example.com.

http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab); 
window.currentTab = tab;

回答by rien333

openalso has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:

open还有一个 -a 标志,您可以使用它从 Applications 文件夹中按名称(或通过带有 -b 标志的包标识符)打开应用程序。您可以将其与 --args 选项结合使用以获得您想要的结果:

open -a APP_NAME --args ARGS

To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:

要在 VLC 播放器中打开一个视频,该视频应该以因子 2x 缩放并循环播放,例如执行:

open -a VLC --args -L --fullscreen

Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)

请注意,我无法将命令输出到终端。(虽然我没有尝试任何东西来解决这个问题)

回答by Keith

With applescript:

使用applescript:

tell application "Firefox" to activate

回答by zerocog

Why not just set add path to to the bin of the app. For MacVim, I did the following.

为什么不将添加路径设置为应用程序的 bin。对于 MacVim,我做了以下工作。

export PATH=/Applications/MacVim.app/Contents/bin:$PATH

An alias, is another option I tried.

别名,是我尝试过的另一种选择。

alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim 

With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.

使用导出路径,我可以调用应用程序中的所有命令。我在 MacVim 上的测试中参数通过得很好。而别名,我必须为 bin 中的每个命令设置别名。

mvim README.txt
gvim Anotherfile.txt

Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.

享受别名和路径的力量。但是,您确实需要在升级操作系统时监控更改。