我有以下使用 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
对象或类似对象。所以我可以设置高度和宽度以及其他各种东西。我怎样才能做到这一点?
最佳答案
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];
关于objective-c - 从 NSWorkspace 获取 NSWindow 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8041232/