使用 Cocoa 接受参数并运行系统命令
我是 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 texthello 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想处理命令行参数并有一个 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 anNSArray
.It can also give you an
NSDictionary
of environment variables, which can also be really useful, from theenvironment
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 forwriteToFile:atomically:encoding:error:
.如果您正在编写没有 UI 的纯输入/输出类型的应用程序,只需使用 C/C++/Objective-C(无论您喜欢哪种)编写直接的 Unix 风格应用程序。您的程序的参数将显示在传递给您的
main
函数的argc
和argv
参数中:如果您确实有一个真实的 UI,通常
main
函数由 Xcode 自动生成,并放置在名为main.m
的文件中 - 它是一个存根函数,通过周围的自动释放池调用NSApplicationMain
它。您可以修改它以保存argc
和argv
和/或立即处理它们。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
andargv
parameters that get passed to yourmain
function:If you do have a real UI, typically the
main
function is autogenerated by Xcode and placed in a file namedmain.m
—it's a stub function that callsNSApplicationMain
with an autorelease pool around it. You can modify this to save offargc
andargv
and/or process them right there.