Mailで添付ファイルを自動リネーム&自動保存

Mailでメール受信(自動的に5分おきくらいにメールチェック)、

ルールでApplescriptを実行して、

HD上の特定のフォルダに添付ファイルを保存したいのですが、

https://discussionsjapan.apple.com/thread/10052314

(添付ファイルの自動保存と発信メールへのルール適用)

上記の質問で、Y._Kawabe-importedさんが、記述されている、

添付ファイルの自動保存スクリプトの例(下記)をもとに、

さらに、『任意の文字列+現在の日時+元ファイル名』という風に

ファイル名を変更して保存したいのですが、どうしたらいいのでしょうか?


たとえば、2013年2月7日10:00:00に

★★★.pdfという添付ファイル付きのメールが送られてきた場合、

XX-130207-10-00-00-★★★.pdf

と変更して保存したいです。


Applescriptはほとんど知識がないので、具体的にスクリプトを記述していただけるとありがたいです。


-----------(Y._Kawabe-importedさん記載スクリプトより引用)-----------

using terms from application "Mail"

on perform mail action with messages messageList

tell application "Mail"

repeat with eachMessage in messageList

set attachmentList to (get mail attachments of eachMessage)

repeat with eachAttachment in attachmentList

set fileName to name of eachAttachment

save eachAttachment in file ("Macintosh HD:Users:hoge:Desktop:添付ファイル:" & fileName)

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

-----------

iMac, Mac OS X (10.6.8)

投稿日 2013/02/06 23:09

返信
返信: 9

2013/02/07 03:12 K_asterisk への返信

失礼致します。 少しずれますが、Automator (ver.2.3 on Mac OS 10.8)でもできましたよ(下図をクリックして拡大)。『テキストを追加』のアクションは『Finder項目の名前を変更』のアクションになります。  Mac OS X 10.6では『一時停止』のアクションがないかもしれませんが、AppleScriptのdelayコマンドを使えると思います。 日付や時刻の変数を『テキストを追加』アクションの項目へドラッグ&ドロップします。 変数の書式は下の変数一覧のアイコンをクリックしてカスタム形式を選択して編集します。

ユーザがアップロードしたファイル

2013/02/07 03:34 T22T への返信

追記: 上記ワークフローの最初のアクションは『指定されたメール項目を取得』になります(画像に入っていませんでした。すみません)。 Automatorの『カレンダーアラーム』形式のワークフローにするとiCalのイベントに入れることができますよ(OS X 10.6ではプラグイン形式で保存する必要があるかもしれません)。

2013/02/07 07:43 K_asterisk への返信

度々、失礼致します。


上記、Y._Kawabe-importedさんのAppleScriptの『set fileName to name of eachAttachment』の行の下に、


set fileName to "XX-" & date_format() & "-" & time_format() & "-" & fileName


を加え、最後の行の下に、


to date_format() -- Old_date is text, not a date.

set old_date to date string of (current date)

set {year:y, month:m, day:d} to date old_date

tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & text 5 thru 6 & text 7 thru 8

end date_format


to time_format()

set old_time to time string of (current date)

tell old_time as string to text 1 thru 2 & "-" & text 4 thru 5 & "-" & text 7 thru 8

end time_format



を加えて下さい。 AppleScript (ver.2.2.3) on Mac OS 10.8.2で作動確認致しました。 どうでしょうか?


参照リンク: http://macscripter.net/viewtopic.php?id=24737

2013/02/07 15:44 T22T への返信

訂正: to date_format() にある、


1)『tell (y * 10000 + m * 100 + d) asstringtotext 1 thru 4 & text 5 thru 6 & text 7thru 8』 は、


(y * 10000 + m * 100 + d) as string』 の方が簡単でしたね。


2)to time_format()の方も修正が必要でした。すみません。 しばらくお待ち下さい。


2013/02/07 16:11 T22T への返信

訂正: to time_format() は、


to time_format()

set old_time to time string of (current date)

set {hours:h, minutes:m, seconds:s} to date old_time

tell (1000000 + h * 10000 + m * 100 + s) as string to text 2 thru 3 & "-" & text 4 thru 5 & "-" & text 6 thru 7

end time_format


で正しく作動すると思います。 度々、修正して済みません(お恥ずかしい)。


サポートコミュニティーの皆様、もっと良い方法がありましたら、ご指摘下さい。 宜しくお願い申し上げます。

2013/02/07 19:55 K_asterisk への返信

K*さん、度々、失礼致します。 色々と余計な書き込みをしてしまい、混乱なさっているかも知れませんね。 大変に申し訳ございません。 まとめると、以下のAppleScriptでご希望の操作が可能になると考えます。 御確認下さい。


using terms from application "Mail"

on perform mail action with messagesmessageList

tell application "Mail"

repeat with eachMessage in messageList

set attachmentList to (get mail attachments of eachMessage)

repeat with eachAttachment in attachmentList

set fileName to name of eachAttachment

set fileName to "XX" & "-" & my print_date() & "-" & fileName

save eachAttachment in file ("Macintosh HD:Users:hoge:Desktop:添付ファイル:" & fileName)

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from


on print_date()

do shell script "date '+%y%m%d-%H-%M-%S'"

end print_date

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

Mailで添付ファイルを自動リネーム&自動保存

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。