2012-04-19 15 views
1

다음은 applescript을 사용하여 파인더 응용 프로그램을 다시 시작합니다.파인더 응용 프로그램을 다시 시작하는 방법

osascript -e "tell application \"Finder\"" -e "delay 1" -e "try" -e "quit" -e "delay 1" -e "activate" -e "end try" -e "end tell" 

가끔이 스크립트는 찾기 응용 프로그램을 다시 시작하지 않습니다 (찾기 응용 프로그램 만 종료). 콘솔에서 오류가 발생하지 않습니다.
http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html
누구든지 나를 도와 줄 수 있습니까?

답변

4

여기 AppleScript로 : 그것은 종료 할 때 응용 프로그램을 다시 시작 할 수 있도록

대신, 인스턴스의 terminated 속성을 모니터하기 위해 관찰 키 - 값을 사용하여이 문제를 관리 할 NSRunningApplication 클래스를 사용해야합니다 방법. 당신은 당신이 본 것처럼 특정 지연 시간에 의지 할 수 없습니다. 따라서 Finder가 실행중인 프로세스 목록에 있는지 검사하여 수동으로 Finder가 종료 될 때까지 기다립니다. 더 이상 목록에 없으면 종료되었음을 알게되고 다시 활성화 할 수 있습니다.

또한 반복 루프로 인해 스크립트에 시간 확인을 기록합니다. 무언가가 잘못되어 간다면 우리는 반복 루프가 영원히 돌아가는 것을 원하지 않습니다. 따라서 10 초 이상 실행하면 자동으로 반복 루프가 종료됩니다.

tell application "Finder" to quit 

set inTime to current date 
repeat 
    tell application "System Events" 
     if "Finder" is not in (get name of processes) then exit repeat 
    end tell 
    if (current date) - inTime is greater than 10 then exit repeat 
    delay 0.2 
end repeat 

tell application "Finder" to activate 

다음은 해당 코드의 osascript 버전입니다.

/usr/bin/osascript -e 'tell application "Finder" to quit' -e 'set inTime to current date' -e 'repeat' -e 'tell application "System Events"' -e 'if "Finder" is not in (get name of processes) then exit repeat' -e 'end tell' -e 'if (current date) - inTime is greater than 10 then exit repeat' -e 'delay 0.2' -e 'end repeat' -e 'tell application "Finder" to activate' 
+0

임의의 "연결이 잘못되었습니다. -609"AppleScript 오류를 방지하기 위해 응용 프로그램 "Finder"에 지시 한 후에 지연이 필요합니까 ?? –

+0

제 경험상 Finder가 너무 빨리 종료되어 다시 시작하려고 시도하면 임의 오류가 발생합니다. 따라서 시스템 이벤트를 사용하여 이러한 문제를 방지하기 위해 시스템 이벤트가 종료 될 때까지 기다립니다. 반복 루프에는 설명 된 내용을 막기위한 지연이 있습니다. 이론이 건실하다고 믿습니다. – regulus6633

+0

종료하기 전에 지연이 필요합니까? –

4

코코아를 사용하는 경우에는 잘못된 방법입니다. 가능하면 네이티브 API를 사용해야하며, 자체적으로 AppleScript를 빌드하고 실행하는 셸 스크립트를 호출하려고합니다. AppleScript는 재시작을 시도하기 전에 1 초 동안 기다립니다. 이는 임의의 값입니다. 실제로 Finder가 종료 될 때까지 기다려야합니다.

//assume "finder" is an ivar of type NSRunningApplication 
//it has to be a strong reference or it will be released before the observation method 
//is called 

- (void)relaunchFinder 
{ 
    NSArray* apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"]; 
    if([apps count]) 
    { 
     finder = [apps objectAtIndex:0]; 
     [finder addObserver:self forKeyPath:@"isTerminated" options:0 context:@"QuitFinder"]; 
     [finder terminate]; 
    } 

} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if (context == @"QuitFinder") 
    { 
     if([keyPath isEqualToString:@"isTerminated"]) 
     { 
      [[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.finder" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:NULL launchIdentifier:NULL]; 
      [object removeObserver:self forKeyPath:@"isTerminated"]; 
     } 
    } else { 
     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 
    } 
} 
+0

응용 프로그램을 설치할 때 스크립트를 사용하고 있습니다. –

+1

그런 다음 질문에 [tag : cocoa]라는 태그와 Cocoa 메일 링리스트 링크가있는 이유는 무엇입니까? –

+0

[Stiphane Sudre] (http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html#113654)도 같은 문제를 겪고 있습니다. 그는 NSAppleScript 클래스를 사용하여 applescript를 실행 중이며 osascript를 사용하고 있습니다. –