告诉应用程序打开然后退出并不能正常使用applescript

时间:2014-09-24 08:06:43

标签: applescript tell

此脚本用于打开微软应用程序,然后在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

但是当我运行脚本时,它会在此之后停止。 (编辑)它似乎关闭除微软应用程序之外的每个应用程序

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 个答案:

答案 0 :(得分:0)

您遇到的问题是告诉Finder打开应用程序在脚本中是不可靠的。这意味着,在继续之前,应用程序加载之前不会完成。你也永远不会用延迟命令把它弄好。我建议使用'告诉app 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")))