PseudoTTY 以编程方式执行命令
有一个用 Objective-c 编写的名为“PseudoTTY”的开源 Xcode 项目,我试图找出如何以编程方式执行命令。当我编译应用程序时,我得到一个漂亮的小终端窗口,它完全符合我的要求;除非我必须手动输入命令。
我想做的是在终端中以编程方式执行命令,并能够用我的程序解析结果。
感兴趣的地方是:
- (void)keyDown:(NSEvent *)event
{
const char * typein = [[event characters] UTF8String];
[[pty_ masterFileHandle]
writeData:[NSData dataWithBytes:typein length:strlen(typein)]];
}
和
-(void) didRead: (NSNotification *)noty
{
NSData * data = [[noty userInfo] objectForKey:NSFileHandleNotificationDataItem];
if ([data length] == 0)
return; // end of file
NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self insertText:str];
There is an open source Xcode project called "PseudoTTY" written in objective-c and I'm trying to find out how to execute commands programmatically. When I compile the application, I get a nice little terminal window that does exactly what I want; except I have to manually type the command in.
What I want to do is programatically execute a command in the terminal, and be able to parse the results with my program.
Spots of interest are:
- (void)keyDown:(NSEvent *)event
{
const char * typein = [[event characters] UTF8String];
[[pty_ masterFileHandle]
writeData:[NSData dataWithBytes:typein length:strlen(typein)]];
}
and
-(void) didRead: (NSNotification *)noty
{
NSData * data = [[noty userInfo] objectForKey:NSFileHandleNotificationDataItem];
if ([data length] == 0)
return; // end of file
NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self insertText:str];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做的就是将
typein
替换为您选择的字符串,它就会执行您想要的操作。输出将位于didRead:
方法的str
中。All you need to do is replace
typein
with a string of your choice and it will do what you want. The output will be instr
in thedidRead:
method.