如何重新启动finder应用程序 [英] How to relaunch finder application

查看:34
本文介绍了如何重新启动finder应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下 applescript 重新启动 finder 应用程序.

I am using following applescript to relaunch finder application.

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

但有时此脚本不会重新启动 finder 应用程序(仅退出 finder 应用程序).我在控制台中没有收到任何错误.
http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html
任何人都可以帮我吗?

But sometimes this script is not relaunching finder application(only quiting finder application). i am not getting any error in console.
http://www.cocoabuilder.com/archive/cocoa/113654-nsapplescript-buggy.html
Can anyone please help me out?

推荐答案

这是一个 AppleScript 方式.正如您所见,您不能依赖特定的延迟时间.因此,我们通过检查它是否在正在运行的进程列表中来手动等待 Finder 退出.当它不再在列表中时,我们就知道它已经退出,我们可以再次激活它.

Here's an applescript way. 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. When it is no longer in the list then we know it has quit and we can activate it again.

您还会注意到,由于重复循环,我在脚本中添加了时间检查.以防万一出现问题,我们不希望重复循环永远运行.因此,如果它运行超过 10 秒,我们将自动退出重复循环.

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.

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 版本.

Here's the osascript version of that code.

/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'

这篇关于如何重新启动finder应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆