在 mac 上阻止应用程序

Block application on mac

有没有办法在打开特定应用程序时运行简单地告诉应用程序退出。

例如,如果我打开 spotify,是否可以触发脚本退出应用程序。

谢谢!

is there a way to run a simple tell application quit, when a specific application is opened.

简短回答:是

更长的答案:

以下是我想到的几种方法...


有一个名为 EventsScripts 的付费 应用程序,它可以在众多 事件 中做出反应此外,一类是 Application Events,其中包含 Application activatedApplication deactivatedApplication will launchApplication launchedApplication quit

EventsScripts 适用于 AppleScript scriptsshell 脚本.

看看 EventScripts. At the time of this post, it's .99 at the US App Store,但可以从开发者网站下载免费演示。

注意:我不属于 EventScripts 的开发人员,只是该产品的满意用户。

示例 AppleScript 代码:

on run eventArgs
    set thisTrigger to (trigger of eventArgs)
    if thisTrigger is "Application launched" then
        set appName to |applicationName| of eventArgs
        if appName is "Spotify" then
            tell application appName to quit
        end if
    end if
end run

一种免费的替代方案是 Hammerspoon,尽管人们可能会发现它不像例如EventsScripts.

这里是代码示例,用于watch目标applicationlaunched 然后使用 AppleScript code 关闭它:

示例 Lua 代码:

function applicationWatcher(appName, eventType)
    if (eventType == hs.application.watcher.launched) then
        if (appName == "Spotify") then
            hs.applescript('tell application "Spotify" to quit')
        end
    end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()

这将被放置在 ~/.hammerspoon/init.lua 文件 中并与 Hammerspoon 运行在后台,当目标applicationlaunched时,被告知退出通过 AppleScript.

注意:我不隶属于 Hammerspoon 的开发人员,只是该产品的满意用户。