Указание приложениям открываться и затем выходить не работает должным образом на applecript

Этот скрипт должен открыть приложения Microsoft и затем выйти через 3 секунды

tell application "Finder"
set myFolder to ((startup disk as text) & "Applications:Microsoft Office 2011") as alias
set myFiles to (every item of myFolder) as alias list
open myFiles
end tell

delay 3

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try
    tell application "Finder"
        set process_list to the name of every process whose visible is true
    end tell
    repeat with i from 1 to (number of items in process_list)
        set this_process to item i of the process_list
        if this_process is not in white_list then
            tell application this_process
                quit
            end tell
        end if
    end repeat
on error
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
end tell

но когда я запускаю скрипт, он останавливается после этого. (РЕДАКТИРОВАТЬ), кажется, закрыть все приложения, кроме приложений Microsoft

tell application "Finder"
set myFolder to ((startup disk as text) & "Applications:Microsoft Office 2011") as alias
set myFiles to (every item of myFolder) as alias list
open myFiles

Как открытие, так и закрытие сценариев приложений прекрасно работают по отдельности, но я не знаю, как к ним присоединиться. Если кто-нибудь знает, почему это происходит, это было бы здорово. Спасибо

1 ответ

Решение

Проблема в том, что указание Finder открыть приложение является асинхронным в сценарии. Это означает, что он не закончится, пока приложение не будет загружено, прежде чем двигаться дальше. Вы никогда не поймете это правильно с помощью команды задержки. Я бы предложил использовать "скажите приложение xxx для активации", которое ждет, прежде чем продолжить. И это делает ваш сценарий намного чище.

property myApps : {"Microsoft Word", "Microsoft Excel"}

repeat with thisApp in myApps
    try
        tell application thisApp to activate
    end try
end repeat

-- do whatever you need after all are open

repeat with thisApp in myApps
    try
        tell application thisApp to quit
    end try
end repeat

Если вы хотите закрыть все видимые приложения, кроме Finder, вы также можете добавить

tell application "Finder" to set myApps to name of (every process whose ((visible is true) and (name is not "Finder")))
Другие вопросы по тегам