(* QuitHideApp version 0.0.1

1分ごとに「隠す」状態になっているアプリを監視して、新たに「隠す」状態になった
アプリを、ダイアログで確認してから終了させる
Spirited Away」と併用、常時起動するタイプのスクリプト

このスクリプトを「アプリケーションバンドル」として保存する。
保存時に「実行後、自動的に終了しない」にのみチェックを入れる。

保存したものをFinderのコンテキストメニューで「パッケージの内容を表示」で
QuitHideApp.app/Contents/Info.plist
Property List Editor (要Developer Toolをインストール)で開く
Information Property List」を選択して、ツールバーの「Add item」をクリックして
Application is agent (UIElement)」を追加、value欄のチェックを入れて
上書き保存すると、バックグラウンドアプリケーションとなる

終了させるときは「アクティビティモニタ」などで終了させる
*)

property interval_seconds : 60 -- 監視時間のインターバル(秒)
-- 自分の好きな時間間隔にする

property excludeApp_list : {"Finder", "Safari", "firefox-bin"}
-- 除外アプリ。追加したい場合は "アプリ名" を「,」で区切って追加記入にして上書き保存する
-- 例として「Finder」「Safari」「Firefox」に設定。「Finder」は消さないように。

property hideApp_list : {}
global button_result

on run
set hideApp_list to my hideApp()
end run

on idle
delay (interval_seconds - 5)
set hideApp_list_now to my hideApp()
repeat with eachApp in hideApp_list_now
if (hideApp_list does not contain eachApp) and (excludeApp_list does not contain eachApp) then
my show_alert(eachApp & " を終了しますか?", "", critical, {"Cancel", "Quit"}, 2)
if button_result is "Quit" then my quitApp(eachApp)
end if
end repeat

delay 5
set hideApp_list to my hideApp()
end idle


on hideApp()
tell application "System Events"
set processList to name of every application process whose background only is false and visible is false
end tell
return processList
end hideApp


on quitApp(appName)
try
tell application appName to quit
end try
end quitApp


on show_alert(alert_text, message_text, alert_icon, button_list, default_button)
-- beep 1
tell me
activate
display alert (alert_text as Unicode text) message (message_text as Unicode text) as alert_icon buttons button_list default button default_button
set button_result to button returned of result
end tell
end show_alert