程序退出时是否可以运行Automator工作流程?

问题描述

某些macOS程序具有令人讨厌的后台进程,当我退出程序时,该进程不会终止。退出程序后,有什么方法可以运行Automator工作流程来结束该过程?

解决方法

在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:

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

enter image description here

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

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

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

保存该文件,关闭文件和包,应用程序将在看不见的情况下运行。