0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サーバからのメールで落ちてるサーバを通知センターに表示するAppleScript

0
Last updated at Posted at 2016-09-28

遠隔地のサーバから毎日生きてる通知がくるけど、知りたいのは落ちてるサーバ!
50も通知くるのに確認しきれない! との声が聞こえたのでちょっと書いてみました。

準備

変数 内容
Servers 通知がくるサーバのアドレスを登録してください。
containSubject サーバからのメールと識別できる一意の文字列を登録してください。
moveBox 受信ボックスから移動するメールボックス名を設定してください。
※毎回作るようにしていますが、一度作ってしまえばコメントアウトした方がいいかもしれません。

テスト環境で動作確認をしている際、メールを移動させてもスクリプトからは受信箱に残ったままと認識される謎の挙動に遭遇したのですが、別の環境では大丈夫そう……
OS X 10.9.5にて



set Servers to {"aaa@aaa.aa", "bbb@bbb.bb", "job@xxx.xx"}
set containSubject to "サーバ報告"
set moveBox to "ServerMail"
set errServer to ""

tell application "Mail"
	
	set senderMails to sender of every message in inbox whose subject contains containSubject
	set senderMailsList to senderMails as string
	
	set countMails to count of senderMails
	set countServers to count of Servers
	
	--受信メールが0ではなく、設定サーバ数より少ない
	if countMails is not 0 and countMails < countServers then
		repeat with i from 1 to countServers
			
			set aServer to item i of Servers
			
			if aServer is not in senderMailsList then
				--display dialog "含まない"
				set errServer to errServer & aServer & return
			else
				--display dialog "含む"
			end if
			
		end repeat
		
		--移動用mailBox作成
		my newMailBox(moveBox)
		
		--受信メールを移動
		my moveMsg(containSubject, moveBox)
		
		--異常サーバの通知
		my msg(errServer)
		
	end if
	
end tell


on msg(errServer)
	my notification("サーバエラー", "", "下記のサーバが落ちているようです" & return & errServer)
end msg


on notification(aTitle, aSub, aMessage)
	tell application "System Events"
		tell process "通知センター"
			
			display notification aMessage with title aTitle subtitle aSub
			
		end tell
		
	end tell
end notification


on newMailBox(moveBox)
	tell application "Mail"
		try
			make new mailbox with properties {name:moveBox}
		end try
	end tell
end newMailBox


on moveMsg(containSubject, moveBox)
	tell application "Mail"
		
		set Mails to a reference to (every message in inbox whose subject contains containSubject)
		move Mails to mailbox moveBox
		
	end tell
end moveMsg


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?