我有以下代码可以使用Cocoa objective-c应用程序打开TextEdit:
[[NSWorkspace sharedWorkspace] openFile:@"/Users/abs/Documents/my.txt" withApplication:@"TextEdit"];
NSDictionary * currentAppInfo = [[NSWorkspace sharedWorkspace] activeApplication];
int pid = [[currentAppInfo objectForKey: @"NSApplicationProcessIdentifier"] intValue];但是,我正在尝试为刚打开的应用程序获取一个NSWindow对象或类似的对象。因此,我可以设置高度和宽度以及其他各种设置。我该怎么做呢?
发布于 2011-11-08 04:05:43
AppleScript是最好的选择:
set theFile to "/Users/Anne/Desktop/File.txt"
tell application "TextEdit"
open (POSIX file theFile) as alias
set bounds of window 1 to {10, 10, 100, 100}
end tell使用NSAppleScript运行脚本:
NSString *path = @"/Users/Anne/Desktop/File.txt";
int X = 10;
int Y = 10;
int width = 400;
int height = 800;
NSString *theSource = [NSString stringWithFormat:@""
"set theFile to \"%@\"\n"
"tell application \"TextEdit\"\n"
"open (POSIX file theFile) as alias\n"
"set bounds of window 1 to {%d, %d, %d, %d}\n"
"end tell",
path,X,Y,width,height];
NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource];
[theScript executeAndReturnError:nil];发布于 2011-11-08 03:07:07
无法想象这是怎么可能的(如果你打开一个碳应用程序呢?或者应用程序根本没有打开窗口?)。
有时,可访问性API允许您执行这种性质的操作。
https://stackoverflow.com/questions/8041232
复制相似问题