Mailで添付ファイルを自動保存

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

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

https://discussionsjapan.apple.com/message/100746770

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

で質問させていただいたものです。


さらによろしくお願いします。


添付ファイルのファイルタイプ(拡張子)で保存先を振り分けるにはどうしたらいいでしょうか?少し自分でやってみたのですが、やはり駄目だったので、再び質問させていただきました。


PDFがほとんどで、時々JPGや他のファイルが届く事になると思うので、


PDFだったら ("MacintoshHD:Users:hoge:Desktop:添付ファイルA:" & fileName)

それ以外は  ("MacintoshHD:Users:hoge:Desktop:添付ファイルB:" & fileName)


という設定にしたいです。



-----------(現状script)-----------

using terms from application "Mail"

on perform mail action with messages messageList

tell application "Mail"

repeat with eachMessage in messageList

set theSubject to subject of eachMessage

set attachmentList to (get mail attachments of eachMessage)

repeat with eachAttachment in attachmentList

set fileName to name of eachAttachment

set fileName to my setPref(theSubject) & "-" & 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


on setPref(theText)

if theText contains "XX" then

set Pref to "XX"

else if theText contains "YY" then

set Pref to "YY"

else if theText contains "ZZ" then

set Pref to "ZZ"

else

set Pref to "Unknown"

end if

return Pref

end setPref

----------------------

iMac, Mac OS X (10.6.8)

投稿日 2013/02/14 21:19

返信
返信: 4

2013/02/15 00:29 K_asterisk への返信

失礼致します。 私の環境(Mail ver. 6.2)ではルールの適用条件の中に、添付ファイルの種類を選択できるようになっておりますが、K*さんの御環境では如何でしょうか?


添付ファイルの拡張子を返すハンドラーの一例としては、


on FileExtension(theText)

if theText contains "." then

set theTet to (the reverse of every character of theText) as string

set x to the offset of "." in theText

set theText to (text (x + 1) thru -1 of theText)

set theTet to (the reverse of every character of theText) as string


-- else


-- set theText to "No_extension"

end if

return theText

end FileExtension


が作動するものと思います。 my FileExtension(fileName) としてスクリプト内で使います。


参照リンク: http://www.macosxautomation.com/applescript/sbrt/index.html



このハンドラーと、


『  save eachAttachment in file ("Macintosh HD:Users:hoge:Desktop:添付ファイル:" & fileName) 』の行を、


if my FileExtension(fileName) = "pdf" then

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

else

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

end if



としてみてはどうでしょう?

2013/02/15 00:52 T22T への返信

度々、済みません。 訂正します。 拡張子を返すハンドラーはこちらになります(不注意で申し訳ございません)。


on FileExtension(theText)

if theText contains "." then

set theText to (the reverse of every character of theText) as string

set x to the offset of "." in theText

set theText to (text 1 thru (x - 1) of theText)

set theText to (the reverse of every character of theText) as string


-- else


-- set theText to "No_extension"

end if

return theText

end FileExtension

2013/02/15 06:38 T22T への返信

度々、失礼致します。 長くなりますが、まとめさせて下さい(↓)。 このルールは5行目で添付ファイルがあるかどうかを確認した上で、添付ファイルがあれば名前を変更する処理とファイルの種類に応じて保存先を分けています。 (ファイル名について、『 & "_" & fileName 』の部分を、『 & "-" & fileName 』に変更する必要があれば、修正して下さい)



using terms from application "Mail"

on perform mail action with messagesmessageList

tell application "Mail"

repeat with eachMessage in messageList

if (mail attachments of eachMessageexists) then # 添付ファイルがあるかどうかを確認

set theSubject to subject of eachMessage

set attachmentList to (get mail attachments of eachMessage)

repeat with eachAttachment in attachmentList

set fileName to name of eachAttachment

set fileName to my setPref(theSubject) & "-" & my print_date() & "_" & fileName

if my FileExtension(fileName) = "pdf" then

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

else

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

end if

end repeat

end if

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


on setPref(theText)

if theText contains "XX" then

set Pref to "XX"

else if theText contains "YY" then

set Pref to "YY"

else if theText contains "ZZ" then

set Pref to "ZZ"

else

set Pref to "Unknown"

end if

return Pref

end setPref


on FileExtension(theText)

if theText contains "." then

set theText to (the reverse of every character of theText) as string

set x to the offset of "." in theText

set theText to (text 1 thru (x - 1) of theText)

set theText to (the reverse of every character of theText) as string


-- else


-- set theText to "No_extension"

end if

return theText

end FileExtension



うまく動きますでしょうか?

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

Mailで添付ファイルを自動保存

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