How to relaunch finder application

前端 未结 2 1413
我寻月下人不归
我寻月下人不归 2021-01-07 10:51

I am using following applescript to relaunch finder application.

osascript -e \"tell application \\\"Finder\\\"\" -e \"delay 1\" -e \"try\" -e         


        
2条回答
  •  失恋的感觉
    2021-01-07 11:43

    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.

    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
    

    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'
    

提交回复
热议问题