xcode - 设置在共享工作区 Mac OSX 中启动的应用程序的框架位置

标签 xcode macos cocoa

我正在从我的应用程序中打开系统首选项应用程序。我还想设置应用程序框架,使其与应用程序的左侧对齐。问题是我不知道如何访问应用程序框架属性,或在特定点启动它。

我使用 NSWorkspace 来启动它,然后获取 NSRunningApplication 的一个实例,我希望它可以用来操作窗口。

-(void)openPreferencesAndSetFrame;
{
    @try {
        BOOL success = [[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/Localization.prefPane"];
        if (success == YES)
        {
            NSArray *appArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.systempreferences"];
            if ([appArray count] > 0)
            {
                NSRunningApplication *sysPrefApp = [appArray objectAtIndex:0];
                //set frame here
            }
        }         
    } @catch (NSException *exception) {
        NSLog(@"%@", [exception description]);
    }
}

最佳答案

经过一番研究、尝试和错误,我找到了解决方案。正如前面提到的,AppleScript 能够完成这项工作。

-(void)openPreferencesAndSetFrame;
{
    @try {
        NSAppleEventDescriptor *theResult;
        NSDictionary *errorInfo;
        float posX = 100;
        float posY = 200;

        NSString *sourceStr = [NSString stringWithFormat:@"ignoring application responses\r\n tell application \"System Preferences\"\r\n activate\r\n end tell\r\n tell application \"System Events\"\r\n tell process \"System Preferences\"\r\n set position of window 1 to {%f, %f}\r\n end tell\r\n end tell\r\n end ignoring\r\n", posX, posY];
        NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:sourceStr];

        theResult = [theScript executeAndReturnError:&errorInfo];
        if (nil == theResult ) {        
            NSString *err = [NSString stringWithFormat:
                         @"Error %@ occured call: %@",
                         [errorInfo objectForKey:NSAppleScriptErrorNumber],
                         [errorInfo objectForKey:NSAppleScriptErrorBriefMessage]];

            NSRunAlertPanel(@"AttachAScript Error", err, @"ok", nil, nil);
        }                
    } @catch (NSException *exception) {
        NSLog(@"%@", [exception description]);
    }
}

关于xcode - 设置在共享工作区 Mac OSX 中启动的应用程序的框架位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7580956/

相关文章:

cocoa - Core Data 和 NSArrayController 的行为非常奇怪

ios - 如何从方法中获取挂起的 View Controller ?

objective-c - 如何在 Objective-C 中自定义绘制窗口标题栏?

objective-c - 如何安装 Mac OS X Security Auth 插件?

java - 如何使用 JDK 7 在 Mac OS X 上运行 IDEA IntelliJ?

xcode - 在 Xcode 之外对 Mac 应用程序进行数字签名失败

cocoa - NSTextView 在鼠标经过之前不会显示数据

ios - Xcode 11/iOS 13 记录 UITest 不工作

xcode - Swift:如何查看 Xcode 监 window 口中显示的变量值?

c++ - 在 Xcode 上用 C++ 创建文件