gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:59:01 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com