使用 Cocoa 接受参数并运行系统命令

发布于 2024-12-21 10:34:41 字数 782 浏览 7 评论 0原文

我是 Cocoa 和 Xcode 的新手,但我真的很想学习。问题是我花了很多时间使用 PHP、perl、python 和 shell 脚本等脚本语言进行工作,虽然 Cocoa 比在 OS X 上的终端中运行 shell 脚本强大得多,但它也是一个有点难进去。

不过我有一个假设:如果我可以开始使用 Cocoa 和 Xcode 来实际做一些事情,那么我会更加适应它并逐渐开始释放它越来越多的潜力。

这就是我希望 StackOverflow 发挥作用的地方。看,我习惯于编写将参数(主要是文件名)作为输入的脚本,并对它们进行操作 - 要么调度其他程序,要么读取和写入基于文本的内容。因此,考虑到这一点,如何编写一个简单的 Cocoa 应用程序来接受输入、将该输入保存到文件并启动系统命令?

所需的输入:

open -a MyApp.app --args "hello world"

所需的输出:

  • 文件 / tmp/test.txt 已创建,现在包含文本 hello world
  • 通过运行 /usr/bin/php -v 检查 php 版本,输出为然后附加到/tmp/test.txt (只是想调用一些系统命令)

我知道这种事情很容易用 bash 脚本之类的 1-2 行来完成,但我真的想看看我是否可以在 Cocoa 应用程序中实现这一点。我提前道歉,因为我意识到这是一个非常菜鸟的问题 - 只是试图以一种感觉熟悉的方式开始。

I am new to Cocoa and Xcode, but I really do want to learn. The problem is that I spend a lot of my time doing work in scripting languages like PHP, perl, python, and shell scripting, and while Cocoa is a lot more powerful than running a shell script in Terminal on OS X, it is also a bit harder to get into.

I have a hypothesis though: If I can start using Cocoa and Xcode to actually do some things then I'll get more comfortable with it and gradually start to unlock more and more of it's potential.

That's where I hope StackOverflow comes in. See, I'm used to writing scripts that take paramaters (mostly file names) as inputs, and act on them - either dispatching other programs or reading and writing to the text-based contents. So with that in mind, How can I write a simple Cocoa application that will accept input, save that input to a file, and launch a system command?

Desired input:

open -a MyApp.app --args "hello world"

Desired output:

  • File /tmp/test.txt is created and now contains text hello world
  • The php version is checked by running /usr/bin/php -v and the output is then appended to /tmp/test.txt (just wanted to call some system command)

I know this sort of thing is very easy to do with just 1-2 lines of something like bash scripting, but I'd really like to see if I could make this happen in a Cocoa app. I apologize in advance because I realize this is very much a noob question - just trying to get started in a way that feels familiar.

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

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

发布评论

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

评论(2

你的笑 2024-12-28 10:34:41

如果你想处理命令行参数并有一个 GUI,你肯定会想看看 NSProcessInfo。除此之外,它还有一个名为 arguments 的方法,该方法将命令行参数作为 NSArray 返回。

它还可以通过 environment 方法为您提供环境变量的 NSDictionary,这也非常有用。

编辑:我忘了提一下,如果你想将字符串的内容保存到文件中,NSString已经有一个方法了!查看 writeToFile:atomically:encoding:error: 的文档。

If you want to process command-line arguments and have a GUI, you'll definitely want to look at NSProcessInfo. Among other things, it has a method called arguments that returns the command-line args as an NSArray.

It can also give you an NSDictionary of environment variables, which can also be really useful, from the environment method.

Edit: I forgot to mention, if you want to save the contents of a string to file, NSString already has a method for that! Look at the docs for writeToFile:atomically:encoding:error:.

七七 2024-12-28 10:34:41

如果您正在编写没有 UI 的纯输入/输出类型的应用程序,只需使用 C/C++/Objective-C(无论您喜欢哪种)编写直接的 Unix 风格应用程序。您的程序的参数将显示在传递给您的 main 函数的 argcargv 参数中:

int main(int argc, char **argv)
{
    // argc is the total number of arguments (including the program name)
    // argv[0] is the first argument (the program name)
    // argv[1] is the second argument (first real argument)
    // ...
    // argv[argc-1] is the last argument
}

如果您确实有一个真实的 UI,通常main 函数由 Xcode 自动生成,并放置在名为 main.m 的文件中 - 它是一个存根函数,通过周围的自动释放池调用 NSApplicationMain它。您可以修改它以保存 argcargv 和/或立即处理它们。

If you're writing a purely input/output type of application that doesn't have an UI, just write a straight Unix-style application in C/C++/Objective-C (whichever you prefer). Your program's arguments will show up in the argc and argv parameters that get passed to your main function:

int main(int argc, char **argv)
{
    // argc is the total number of arguments (including the program name)
    // argv[0] is the first argument (the program name)
    // argv[1] is the second argument (first real argument)
    // ...
    // argv[argc-1] is the last argument
}

If you do have a real UI, typically the main function is autogenerated by Xcode and placed in a file named main.m—it's a stub function that calls NSApplicationMain with an autorelease pool around it. You can modify this to save off argc and argv and/or process them right there.

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