2010-07-02 67 views
1

我有一个函数发送字符串“theData”。我想在此代码中插入该字符串。请有人告诉我这个正确的语法?事情变得有点多毛了。谢谢!如何将一些数据插入到AppleScript字符串中

NSTask *task; 
task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/usr/bin/osascript"]; 
[task setArguments:[NSArray arrayWithObjects:@"-e", @"tell application \"System Events\"\n", 
              @"-e", @" keystroke \"" + theData + "\"", 
              @"-e", @"end tell", nil]]; 
[task launch]; 
+0

可能有更好的方法来模拟Objective-C的击键... – icktoofay 2010-07-02 02:53:13

+3

而事实上,它看起来像是存在的。 (http://stackoverflow.com/questions/2379867/) – icktoofay 2010-07-02 02:56:30

+0

哇。多谢,伙计。 – 2010-07-02 02:58:19

回答

1

icktoofay已经给更多的正确的答案,但我只想展示如何在字符串中插入一个字符串:

 NSString* toBeInserted = @"for"; 
     NSString* result = [NSString stringWithFormat:@"in%@mation",toBeInserted]; 
     NSLog(@"%@",result); 

这给information。欲了解更多详情,请阅读Apple's doc

我的意思是,苹果的doc实际上相当不错。在SO提出问题之前阅读它。

顺便说一句,您不必启动osascript来执行AppleScript。您可以使用NSAppleScript作为

NSAppleScript* script=[[NSAppleScript alloc] initWithSource:@"tell app \"Finder\" to activate "]; 
NSDictionary*error; 
[script executeAndReturnError:&error]; 
[script release]; 

好,NSAppleScript是需要NSDictionary,而不是一个NSError,报告错误的古怪......

或者,你可以使用Scripting Bridge映射的AppleScript对象目标-C对象。

0

我看你有没有这样做的另一种方式,但可以使用格式字符串来完成这项

[NSString stringWIthFormat: @"part one %@ part 2", theData]; 

假设数据是包含“你好”一个NSString,这会给你:

@"part one hello part 2" 
相关问题