OSX 相当于 ShellExecute?

发布于 2024-07-19 03:01:28 字数 134 浏览 5 评论 0原文

我有一个 C++ 应用程序,正在从 Win32 移植到 OSX。 我希望能够启动任意文件,就像用户打开它们一样。 在 Windows 上使用 ShellExecute 这很容易。 如何在 Mac 上完成同样的事情?

谢谢!

I've got a C++ app that I'm porting from Win32 to OSX. I'd like to be able to launch arbitrary files as if the user opened them. This is easy on windows using ShellExecute. How do I accomplish the same thing on the Mac?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2024-07-26 03:01:28

您可以在任何 C++ 应用程序中调用 system();。 在 OSX 上,您可以使用 open 命令启动事物就像点击它们一样。

从打开的文档中:

open 命令打开文件(或目录或 URL),就像双击文件的图标一样。 如果未指定应用程序名称,则使用通过 LaunchServices 确定的默认应用程序打开指定的文件。

总而言之,它看起来像:

string command = "open " + filePath;
system(command.c_str());

You can call system(); in any C++ application. On OSX, you can use the open command to launch things as if they were clicked on.

From the documentation for open:

The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified files.

All together, it would look like:

string command = "open " + filePath;
system(command.c_str());
诺曦 2024-07-26 03:01:28

如果您使用可可,另一个建议是:

[[NSWorkspace sharedWorkspace] openFile:@"pathToFile"];

NSWorkspace 中还有其他类似的方法。 例如,要打开应用程序或 URL:

[[NSWorkspace sharedWorkspace] launchApplication:@"pathToApplication"];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"URL"]];

通过 [NSWorkspace sharedWorkspace] 进行操作可以为您提供比标准 C system() 调用更多的控制权。

编辑:请注意,您可以使用 Objective-C++ 将C++代码与Objective-C代码混合,从而调用cocoa方法。

Another suggestion if you're working with cocoa:

[[NSWorkspace sharedWorkspace] openFile:@"pathToFile"];

There are other similar methods in NSWorkspace as well. For example to open an application or a URL:

[[NSWorkspace sharedWorkspace] launchApplication:@"pathToApplication"];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"URL"]];

Working through [NSWorkspace sharedWorkspace] can give you a bit more control than the standard C system() call.

Edit: Note that you can use Objective-C++ to mix C++ code with Objective-C code and thereby call cocoa methods.

怀念你的温柔 2024-07-26 03:01:28

您可以简单地使用 system(); 功能。 例如,假设您想将扩展坞放在屏幕的一角。

您可以简单地说:

system(defaults write com.apple.dock pinning -string end);
sleep(1f);
system(killall Dock);

就是这么简单。
希望我有帮助:)

You can simply use the system(); function. For example, say you wanted to put your dock into the corner of the screen.

You can simply put:

system(defaults write com.apple.dock pinning -string end);
sleep(1f);
system(killall Dock);

It's that easy.
Hope I helped :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文