简体   繁体   English

如何重新启动Finder应用程序

[英]How to relaunch finder application

I am using following applescript to relaunch finder application. 我正在使用以下applescript重新启动finder应用程序。

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

But sometimes this script is not relaunching finder application(only quiting finder application). 但是有时此脚本不会重新启动finder应用程序(仅退出finder应用程序)。 i am not getting any error in console. 我在控制台中没有任何错误。
http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html
Can anyone please help me out? 有人可以帮我吗?

This is the wrong way to go about things if you are using Cocoa. 如果您使用可可粉,这是做事的错误方法。 You should always use native APIs where possible, whereas you are attempting to call a shell script that itself builds and runs an AppleScript. 在可能的情况下,应始终使用本机API,而尝试调用本身可构建并运行AppleScript的Shell脚本。 Your AppleScript waits for one second before attempting a relaunch, which is an arbitrary value. 您的AppleScript等待一秒钟,然后尝试重新启动,这是一个任意值。 You should actually be waiting for Finder to quit. 您实际上应该在等待Finder退出。

Instead, you should use the NSRunningApplication class to manage this, by using Key-Value Observing to monitor the instance's terminated property, so that you can relaunch the app when it terminates: 相反,您应该使用NSRunningApplication类来管理此操作,方法是使用键值观察来监视实例的terminated属性,以便可以在终止该应用程序时重新启动该应用程序:

//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];
    }
}

Here's an applescript way. 这是一个applescript方式。 You cannot rely on a specific delay time as you've seen. 如您所见,您不能依赖特定的延迟时间。 Therefore we manually wait for the Finder to quit by checking if it's in the list of running processes. 因此,我们通过检查它是否在正在运行的进程列表中来手动等待Finder退出。 When it is no longer in the list then we know it has quit and we can activate it again. 当它不再在列表中时,我们知道它已退出,我们可以再次激活它。

You'll also note I put a time check in the script because of the repeat loop. 您还会注意到,由于重复循环,我在脚本中进行了时间检查。 Just in case something goes wrong we do not want the repeat loop to run forever. 万一出现问题,我们不希望重复循环永远运行。 As such if it runs for more than 10 seconds we automatically exit the repeat loop. 因此,如果它运行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

Here's the osascript version of that code. 这是该代码的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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM