Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Mac OS X-控制其他窗口_Objective C_Macos - Fatal编程技术网

Objective c Mac OS X-控制其他窗口

Objective c Mac OS X-控制其他窗口,objective-c,macos,Objective C,Macos,可能重复: 我想控制Mac OS X 10.6上其他进程的其他窗口 例如,我想向下滚动Safari 我该怎么做呢?AppleScript可以在Safari中向下滚动页面,选中: 脚本: tell application "System Events" tell application "Safari" to activate delay 0.5 key code 119 end tell 只需使用NSAppleScript运行此脚本: NSAppleScript *scr

可能重复:

我想控制Mac OS X 10.6上其他进程的其他窗口

例如,我想向下滚动Safari


我该怎么做呢?

AppleScript可以在Safari中向下滚动页面,选中:

脚本:

tell application "System Events"
   tell application "Safari" to activate
   delay 0.5
   key code 119
end tell
只需使用NSAppleScript运行此脚本:

NSAppleScript *scrollDownSafari = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\"\ntell application \"Safari\" to activate\ndelay 0.5\nkey code 119\nend tell"];
[scrollDownSafari executeAndReturnError:nil];
编辑

可读性更好的代码:

NSString *script =
@"tell application \"System Events\"\n"
 "tell application \"Safari\" to activate\n"
 "delay 0.5\n"
 "key code 119\n"
 "end tell";

NSAppleScript *scrollDownSafari = [[NSAppleScript alloc] initWithSource:script];
[scrollDownSafari executeAndReturnError:nil];

您可以通过使用字符串连接来提高可读性:
@“ab
”相当于
@“a”@“b”
。我同意:)注意:只有第一行需要
@
,但您可以对每一行使用
@
。是否有其他使用目标C的解决方案?否。AppleScript是与其他应用程序通信的推荐方式,特别是因为它可以防止不兼容问题。例如,AppleScript确保您的应用程序能够与Safari的每个(未来)版本一起工作。如果您实现自己的方法,它可能会在下一次Safari更新时停止。不要害怕使用AppleScrip,它对于特定任务非常有用。