首页 > 解决方案 > 程序退出时是否可以运行 Automator 工作流程?

问题描述

macOS 的某些程序有烦人的后台进程,当我退出程序时这些进程不会终止。有什么方法可以运行 Automator 工作流程,一旦我退出程序就结束该过程?

编辑:Adobe 为 AGMService 提供了一个卸载程序来永久删除它,绕过了这个问题的一部分。

标签: macosautomator

解决方案


跟进 red_menace 的评论,您可以创建一个后台应用程序,该应用程序将等待 NSWorkspace 通知 Photoshop 或 Illustrator 已退出,然后退出 AGMService。将以下代码复制到脚本编辑器中(仔细检查 Illustrator 和 Photoshop 的本地化名称,尽管我认为它们是正确的):

use AppleScript version "2.4"
use framework "AppKit"
use scripting additions

property NSWorkspace : class "NSWorkspace"

on run
    set workSp to NSWorkspace's sharedWorkspace()
    set notifCent to workSp's notificationCenter()
    tell notifCent to addObserver:me selector:"someAppHasTerminated:" |name|:"NSWorkspaceDidTerminateApplicationNotification" object:(missing value)
end run

on idle
    -- we don't use the idle loop, so tell the system let the app sleep. this comes out of idle once an hour
    return 3600
end idle

on someAppHasTerminated:notif
    set termedApp to (notif's userInfo's valueForKey:"NSWorkspaceApplicationKey")
    set termedAppName to (termedApp's localizedName) as text
    -- I'm guessing at the localized names for Photoshop and Illustrator. you may need to alter these
    if termedAppName is "Adobe Photoshop" or termedAppName is "Adobe Illustrator" then
        -- close the service here
        tell application "System Events"
            tell process "AGMService"
                if its accepts high level events is true then
                    tell application "AGMService" to quit
                else
                    do shell script "killall AGMService"
                end if
            end tell
        end tell
    end if
end someAppHasTerminated:

将其保存为应用程序,并选中“运行处理程序后保持打开”框:

在此处输入图像描述

然后将它添加到您的启动项中,以便在您登录时运行。然后,每当您退出 Illustrator 或 photoshop 时,它也应该强制 AGMService 退出。(我不知道如果您在另一个仍处于打开状态时退出一个是否会导致问题)。

如果你想让它成为一个隐藏的后台应用程序——一个不显示在 Dock 或可见程序列表中的应用程序——右键单击小程序的图标,选择“显示包内容”,然后向下导航直到找到“ info.plist 文件。在纯文本编辑器(如 TextWrangler 或纯文本模式下的 TextEdit)中打开该文件,并在以下键值对中进行编辑:

<key>LSUIElement</key>
<true/>

保存,关闭文件和包,应用程序将不可见地运行。


推荐阅读