文章目录
NSTask 简述
NSTask类只有Mac OS程序开发才有,使用NSTask类,你的程序可以执行另一个程序并获取程序运行的结果。
注意,NSTask创建的是一个独立运行的进程,不会与主程序共享存储空间。
一、打开指定App
1、核心代码
只要传入 app的执行路径即可打开 App,比如打开Pages
- (void)openAppWithExecPath:(NSString *)execPath{
NSTask *task = [[NSTask alloc]init];
[task setLaunchPath:execPath];
[task launch];
}
- (void)openPages{
NSString *execPath = @"/Applications/Pages.app/Contents/MacOS/Pages";
NSLog(@"execPath:%@",execPath);
[self openAppWithExecPath:execPath];
}
2、获取 App 执行地址
2.0 fullPathForApplication
不能获取执行地址
注意:执行地址,并非使用 NSWorkspace 获取的 fullPathForApplication,所以使用下面方法获取的地址无效:
[[NSWorkspace sharedWorkspace] fullPathForApplication:@"Pages"];
2.1 App 执行地址文件结构
我们打开上面 pages的地址看看,会发现文件结构是这样的:
2.2 获取 App 执行地址
同理,如果我们想获得 numbers 的执行地址,可以如下操作
1)打开应用程序文件夹,右击 numbers;
2)选择获取包内容;
3)进入 MacOS 文件夹,即可获得这个 exec 文件。
4)快捷键 command + i 接口得到描述信息,赋值地址即可。
3、APP 多开
参考博文:http://www.jianshu.com/p/0096fc7cbde9
大家使用 Mac QQ 的时候会发现,登陆一个 qq 后,点击 command + N 后,可以再开一个。
其实用法和上面相同,只是获取地址方式改变了
3.1、核心代码
- (void)appNewAction{
NSString *execPath = [[NSBundle mainBundle] executablePath];
NSLog(@"execPath:%@",execPath);
NSTask *task = [[NSTask alloc]init];
[task setLaunchPath:execPath];
NSArray *args = @[@"para0",@"para1",@"para2"];
task.arguments = args;
[task launch];
}
3.2、获取传递参数 args
以上 args 可以不传,如果传了的话,是在 main.c 中获取。获取 args 代码:
int main(int argc, const char * argv[]) {
for (int i = 0; i < argc; i++) {
NSString *arg = [[NSString alloc]initWithCString:argv[i] encoding:NSUTF8StringEncoding];
NSLog(@"arg : %@",arg);
}
return NSApplicationMain(argc, argv);
}
3.3 常用操作:绑定菜单栏 menu
将 menu 上的 item 拉线过来,并在拉线的方法中实现上述方法即可。
大家可以注意到,如果 menu 上的 Item没有绑定方法时,运行起来,显示颜色为灰色。具体关于 menu 的使用,可参考博文:http://blog.csdn.net/lovechris00/article/details/78002161
二、调用命令行查询开机记录,并获取记录
1、引言
在命令行中,可以输入下面语句,获取开关机记录:
-
查看开机时间记录
last | grep reboot
-
查看关机时间记录
last | grep shutdown
不使用命令行,用app 调用命令行,则需要使用到 NSTask。方法如下:
2、核心代码
- (void)addTask{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];
NSString *commandStr = @"last | grep reboot";
NSArray *arguments = [NSArray arrayWithObjects:@"-c",commandStr,nil];
NSLog(@"arguments : %@",arguments);
[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 (@"got\n %@", string);
}
3、其他命令行的实现
3.1 执行查询文件的设置
[task setLaunchPath:@"/bin/ls"];
//command arguments ls -la [path]
NSArray *lsArgs = [NSArray arrayWithObjects: @"-l", @"-a", [pathToList stringValue], nil];
[task setArguments:lsArgs];
待续…
4、分析小结
setLaunchPath
是为了设置 shell 脚本参数。可参考:http://blog.csdn.net/lovechris00/article/details/78145847arguments
中包含的是shell 语句。
参考资料
-
LeungKinKeung:NSTask:Mac开发App多开
http://www.jianshu.com/u/4ebe1f9bc2f5 -
newHung:用NSTask执行外部命令并获取输出结果的方法
http://blog.csdn.net/yuanya/article/details/13393671 -
newHung:关于NSTask使用的一些心得体会
http://blog.csdn.net/yuanya/article/details/40002103 -
nit ghost:使用NSTask 挂载dmg 文件
http://blog.csdn.net/nitghost/article/details/3872780 -
Mac OS编程-NSTask的用法(调用另一个程序)
https://jingyan.baidu.com/article/f7ff0bfc3d840d2e27bb137b.html