Execute a terminal command from a Cocoa app

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);
}
//------------------------------------------------------
时间: 2024-10-22 14:35:43

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

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

报错的代码为 protected void buttonPressed(int buttonId) { Display.getDefault().syncExec(new Runnable() { public void run() { localpmtsStreamViewsAction.refreshPerspective(localviewId, localw); } }); super.buttonPressed(buttonId); } 抛异常: org.eclipse.core.co

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

坑爹的Jenkis,在执行windows命令编译.NET项目的时候命令执行成功了,但是却还是报了这样一个错: Build step 'Execute Windows batch command' marked build as failure 综合了几个stackoverflow上的答案,原因如下: jenkins执行windows命令,若退出代码不为0 ,则jenkins会将构建标记为失败 我解决的方法:在bat脚本最后一行加上 exit 0 Build step 'Execute Windo

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

1由于使用svn 更新文件出错,导致svn中断,然后就一直循环出现  ''Please execute the 'Cleanup' command'' 问题: 查找网上方案 . 有使用sqlite3 解决问题的 网上下载 sqlite3  ,解压后,配置好环境变量[网上很多教程]:  运行cmd,进入你的项目文件的 .svn 隐藏文件中, 比如: D:\Code\.svn文件夹下,执行sqlite3 wc.db,打开数据库: 执行.table 可以查看表名: 执行delete from work

SVN Update Error: Please execute the 'Cleanup' command

在使用SVN做更新代码的时候,有时会碰到无法更新的情况,一般会提示如下的信息,说是: Working copy 'E:\mySVNDirectory' locked Please execute the 'Cleanup' command. 解决方法很简单,只要点击鼠标右键-> clearn up一下即可 如果上面方法不起作用,并且提示“已经被锁定,无法更新”的信息, 原因很可能是: 1. 有文件正在更新或上传,该文件夹被锁定. 2. 更新或上传的时候动作没有完成,导致本地存在锁定状态没有释放.

svn提交更新代码提示Please execute the 'Cleanup' command

那可能是提交或更新代码的过程意外终止,第二次提交或更新会报这个错误 更新或上传的时候动作没有完成,导致本地存在锁定状态没有释放 或者有文件正在更新或上传,该文件夹被锁定. 解决办法: 将对应文件夹里的.svn文件夹内的lock文件删除(.svn默认被隐藏,查看设置显示隐藏文件夹) 重新从仓库checkout一个项目下来,替换刚刚那个提交不上去的文件夹中的.svn即可. svn提交更新代码提示Please execute the 'Cleanup' command 原文地址:https://www

解决 Previous operation has not finihsed; run ‘cleanup’ if it was interrupted Please execute the ‘Cleanup’ command

更新时遇到这个问题,解决方法如下: 把根目录下的.svn目录删除掉,再checkout,然后就会出现下面的加version的action.   疯吻IT

please execute the cleanup command

解决方法: (1)用dos命令进入项目文件夹,运行svn cleanup:不要直接右键点击找cleanup选项 (2)到上一层目录去cleanup试下,或者到.svn文件夹下(隐藏的)找到所有的lock文件,手动删除再update下,最不行就是checkout另一个,把这个坏了的,除svn文件夹都copy过去  = = = 此法验证可行 (3)按上面的意思是说你只要Clearnup一下就可以再更新了,但是很多时候,当你再点cleanup的时候,它会再提示你已经被锁定,无法更新. 原因有可能是 1

Command Line Skills

Part 1: Command Line Interface(CLI) The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer kn

How to create a Maven web app and deploy to Tomcat - fast

原文地址: http://www.blogjava.net/sealyu/archive/2010/01/08/308706.html Procedure Prerequisites and Assumptions Step One - Prepare the Tomcat Manager application Step Two - Create a New Web App Using Maven Step Three - Define Your Tomcat Server in Maven