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

时间:2011-09-28 09:18:48

标签: 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]);
    }
}

2 个答案:

答案 0 :(得分:1)

经过一些研究,尝试并错误我找到了解决方案。如上所述,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]);
    }
}

答案 1 :(得分:0)

应用程序没有frame属性。如果你想设置主窗口的框架,你应该能够使用AppleScript,通过NSAppleScript或脚本桥。