objective-c - 从 NSWorkspace 获取 NSWindow 对象?

标签 objective-c cocoa

我有以下使用 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/

相关文章:

macos - Cocoa:通过供应商 ID 检测 USB 设备

ios - CIDetector featuresInImage 返回 0

Objective-C 内存管理 : What is happening when setting a retain property?

iphone - 如何计算 NSButton 的中心?

objective-c - 使用 OCMock 和 Core Data 测试 Controller 方法

objective-c - OS X Lion 中类似邮件的收藏夹栏

ios - 谷歌分析在不同系统机器上崩溃 iOS

ios - 如何创建对角线切割 View 以在配置文件屏幕中使用?

ios - 在didReceiveLocalNotification中收到本地通知后执行后台任务:

swift - 在 Swift 3 中以编程方式创建没有 XIB 的 NSViewController