http://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app

in the spirit of sharing... this is a method I use frequently to run shell scripts. you can add a script to your product bundle (in the copy phase of the build) and then have the script be read and run at runtime. note: this code looks for the script in the privateFrameworks sub-path. warning: this could be a security risk for deployed products, but for our in-house development it is an easy way to customize simple things (like which host to rsync to...) without re-compiling the application, but just editing the shell script in the bundle.

//------------------------------------------------------
-(void) runScript:(NSString*)scriptName
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"]; NSArray *arguments;
NSString* newpath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] privateFrameworksPath], scriptName];
NSLog(@"shell script path: %@",newpath);
arguments = [NSArray arrayWithObjects:newpath, nil];
[task setArguments: arguments]; NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe]; NSFileHandle *file;
file = [pipe fileHandleForReading]; [task launch]; NSData *data;
data = [file readDataToEndOfFile]; NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"script returned:\n%@", string);
}
//------------------------------------------------------

Execute a terminal command from a Cocoa app的更多相关文章

  1. JFace dailog button事件中刷新透视图异常 Trying to execute the disabled command org.eclipse.ui.window.closePerspective

    报错的代码为 protected void buttonPressed(int buttonId) { Display.getDefault().syncExec(new Runnable() { p ...

  2. 错误解决:SharePoint Designer 2010编辑后,出现数据源控件未能执行插入命令,data source control failed to execute the insert command

    打了SharePoint 2010 最新的SP 2的补丁,但是使用SharePoint Designer 2010 定义任何一个列表的“插入视图”时,总是出现标题那样的错误: 数据源控件未能执行插入命 ...

  3. svn Please execute the 'Cleanup' command. 问题解决

    1由于使用svn 更新文件出错,导致svn中断,然后就一直循环出现  ‘’Please execute the 'Cleanup' command‘’ 问题: 查找网上方案 . 有使用sqlite3 ...

  4. Build step 'Execute Windows batch command' marked build as failure

    坑爹的Jenkis,在执行windows命令编译.NET项目的时候命令执行成功了,但是却还是报了这样一个错: Build step 'Execute Windows batch command' ma ...

  5. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  6. SVN更新报错问题(Please execute the 'Cleanup' command)

    SVN更新报错问题(Please execute the 'Cleanup' command) https://segmentfault.com/a/1190000012571289 svn: E20 ...

  7. 【SVN】Please execute the 'Cleanup' command.

    背景 项目有个新的bug,我需要提取一个新的分支,但是提取之后,更新分支出现了这个问题 Please execute the 'Cleanup' command. 原因 由于使用SVN更新文件出错,导 ...

  8. SVN报错之“Error: Please execute the 'Cleanup' command. ”

    问题 Error: Please execute the 'Cleanup' command. 需要清理下,注意SVN拉数据的时候别打开其中的问题 解决方案

  9. 如何从Terminal Command Line编译并运行Scope

    Ubuntu SDK我们大部分的开发者是非常有效的.它甚至可以帮助我们进行在线调试.在这篇文章中,我们介绍了如何使用command line编译和执行我们scope. 1)创建一个主Scope 我们能 ...

随机推荐

  1. Java中的while(true)

    while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环

  2. Python网络编程(进程池、进程间的通信)

    线程池的原理:        线程池是预先创建线程的一种技术.线程池在还没有任务到来之前,        创建一定数量的线程,放入空闲队列中.这些线程都是处于睡眠状态,        即均为启动,不消 ...

  3. python基础训练营01

    一.基础讲解: 1.1 文件末尾的.py后缀,指出这个文件,是一个python文件,因此,系统将使用python解释器来运行该文件,确定文件中每一个单词的含义. 1.2 python编辑/运行方法: ...

  4. beta版本冲刺五

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  5. express框架 中间件

  6. 除了基本类型,其余类型基本上大部分new出来 java.sql 包下面要不需要new

    除了基本类型,其余类型基本上大部分new出来 java.sql 包下面要不需要new

  7. 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义 ...

  8. Generator实质

    Generator实质 来源: <http://blog.liuwanlin.info/generatorshi-zhi/>  superlin •  September 15, 2015 ...

  9. AOJ.800 热身之开关灯

    热身之开关灯 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 276 Submis ...

  10. 遇到问题---java---myeclipse中maven项目引用另一个导致的resource文件混乱的问题

    遇到情况 情况是这样的,我们在构建项目时,经常会把一些公用的类和配置提取出去,作为一个公共项目.然后把公共项目作为一个jar包构件引入我们当前的项目中. 引入方式是 <dependency> ...