2011-11-07 39 views
1

我有打開文本編輯使用可可的Objective-C的應用程序如下:從NSWorkspace獲取NSWindow對象?

[[NSWorkspace sharedWorkspace] openFile:@"/Users/abs/Documents/my.txt" withApplication:@"TextEdit"]; 

NSDictionary * currentAppInfo = [[NSWorkspace sharedWorkspace] activeApplication]; 

int pid = [[currentAppInfo objectForKey: @"NSApplicationProcessIdentifier"] intValue]; 

不過,我試圖讓一個NSWindow對象或爲我剛剛打開應用程序的喜好。所以我可以設置高度和寬度以及其他各種東西。我怎樣才能做到這一點?

回答

3

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]; 
0

無法想象這是怎麼可能的(如果你打開一個Carbon應用程序?或者應用程序根本沒有打開窗口?)。

有時候,Accessibility API可以讓你做這種事情。

+0

我總是會打開文本編輯或具有窗口喜歡。我還必須檢查窗口是否已打開。 – Abs