前回は、コードの不自然な部分やバグを修正したり、ファイル分割して分かりやすくする作業を実施しました。
今回は、PDF以外にも対応するため、関連部分のコードを一つずつ見ていきます。 [asin:B07ZWCDYZG:detail] evernote.com trello.com
PDF以外にも対応する
前回ではPDFとTEXTに対応するためコードを統合してみましたが、機械的に作業してしまったため、期待通りの動作をしなくなってしまいました。
そこで各部分を一つずつ丁寧に見ていくことにします。
docsend_check_from
まずは、docsend_check_from変数です。
この変数の元々の意味は、「送信元が送り込み処理を実施する対象なのか?」についての情報を持っているのでした。同じ送信元であっても、PDFとemailでは処理をする/しないは異なりますので、それぞれ別々に情報を持たなくてはいけません。
コードで示すと、
# docsend_check_from (一部抜粋) if header :contains "From" "example1.co.jp" { set "pdfsend_check_from" "true"; set "textsend_check_from" "true";
といった形になります。
が、このパターンですぐに分かるのは「処理する対象(pdf/email/その他)が増えていくとその数だけ増殖」していくことです。それではさすがに面倒ですよね。
いくつか解決法を思いつくのですが、今回は、docsend_check_from に「処理する対象(pdf/email/その他)」の処理情報を保存しておいて、もし異なる場合に個別(pdfsend_chefk_from等)に設定することにしてみます。
もう少し詳しく説明すると、
docsend_check_from = true | false | 未定義 | |
---|---|---|---|
pdfsend_check_from = true | ◯ | ◯ | ◯ |
false | ✕ | ✕ | ✕ |
未定義 | ◯ | ✕ | ✕ |
この表のような挙動をしてくれれば良いわけです。
色々な解釈ができますが、今回は実行条件として、「(1)pdfsend_check_from が true の場合、もしくは、(2)docsend_check_from が true かつ pdfsend_check_from が false でない場合」ということにします。
#test5-1-1 (一部抜粋) include "docsend_check_from"; if anyof ( string :matches "${pdfsend_check_from}" "true", allof ( string :matches "${docsend_check_from}" "true", not string :matches "${pdfsend_check_from}" "false" ) )
このような感じになります。pdfだけでなくtextも同様の挙動をさせることとし、変数名をpdfsend_check_from から docsend_check_from_pdf 等へ変更すると、該当する部分のコードは次のようになります。
#test5-1-2 require ["include", "variables", "mime", "foreverypart"]; global "subject"; global "docsend_check_from"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; # 元メールのSubject:を保存 if header :matches "Subject" "*" { set "subject" "${1}"; } include "docsend_check_from"; if anyof ( string :matches "${docsend_check_from_text}" "true", allof ( string :matches "${docsend_check_from}" "true", not string :matches "${docsend_check_from_text}" "false" ) ) { include "docsend_run"; include "trash"; } if anyof ( string :matches "${docsend_check_from_pdf}" "true", allof ( string :matches "${docsend_check_from}" "true", not string :matches "${docsend_check_from_pdf}" "false" ) ) { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_run"; break; } } include "trash"; } discard;
各処理を実行するかどうかの条件式が複雑になりましたね。そこで、条件式に名前をつけて別ファイルに追い出してみましょう。
とりあえず条件式の名前を "docsend_check_from_bool" に決めたとすると、text側の処理は次のようになります。
#test5-1-3 (抜粋) include "docsend_check_from"; include "docsend_check_from_bool"; if string :matches "${docsend_check_from_bool}" "true" { include "docsend_run"; include "trash"; }
# docsend_check_from_bool require ["include", "variables"]; global "docsend_check_from_bool"; global "docsend_check_from"; global "docsend_check_from_text"; if anyof ( string :matches "${docsend_check_from_text}" "true", allof ( string :matches "${docsend_check_from}" "true", not string :matches "${docsend_check_from_text}" "false" ) ) { set "docsend_check_from_bool" "true"; } else { set "docsend_check_from_bool" "false"; }
今作った、docsend_check_from_boolに一工夫を加えてみると、もう少し何をやっているのか分かりやすくなります。
#test5-1-4 (抜粋) include "docsend_check_from"; set "docsend_running" "text"; include "docsend_check_from_bool"; if string :matches "${docsend_check_from_bool}" "true" { include "docsend_run"; include "trash"; }
# docsend_check_from_bool require ["include", "variables"]; global "docsend_check_from_bool"; global "docsend_check_from_running"; global "docsend_check_from"; global "docsend_check_from_text"; if string :matches "${docsend_running}" "text" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_text}"; } if anyof ( string :matches "${custom}" "true", allof ( string :matches "${default}" "true", not string :matches "${custom}" "false" ) ) { set "docsend_check_from_bool" "true"; } else { set "docsend_check_from_bool" "false"; }
ここまで変形した状態で、pdf側にも対応させてみましょう。するとコードは次のようになります。
#test5-1-5 require ["include", "variables", "mime", "foreverypart"]; global "subject"; global "docsend_check_from_bool"; global "docsend_running"; # 元メールのSubject:を保存 if header :matches "Subject" "*" { set "subject" "${1}"; } include "docsend_check_from"; set "docsend_running" "text"; include "docsend_check_from_bool"; if string :matches "${docsend_check_from_bool}" "true" { include "docsend_run"; include "trash"; } set "docsend_running" "pdf"; include "docsend_check_from_bool"; if string :matches "${docsend_check_from_bool}" "true" { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_run"; break; } } include "trash"; } discard;
# docsend_check_from_bool require ["include", "variables"]; global "docsend_check_from_bool"; global "docsend_running"; global "docsend_check_from"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; if string :matches "${docsend_running}" "text" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_pdf}"; } if anyof ( string :matches "${custom}" "true", allof ( string :matches "${default}" "true", not string :matches "${custom}" "false" ) ) { set "docsend_check_from_bool" "true"; } else { set "docsend_check_from_bool" "false"; }
docsend_check_from に関してはこんなところです。
docsend_*_run
次に取り掛かるのは、docsend_*_run 変数です。送信元が処理対象となった時に、実際にどういったツール(kindle/evernote/treelo)に対して送り込むのか、といった情報を扱っています。
docsend_check_fromと同様の考えを摘要するとして、まずはdocsend_kindle_runについてコードに当てはめてみると、次のようになります。
# docsend_run require ["include", "variables"]; global "docsend_kindle_run_bool"; global "docsend_kindle_run"; global "docsend_evernote_run"; global "docsend_trello_run"; include "docsend_kindle_run_bool"; if string :matches "${docsend_kindle_run_bool}" "true" { include "send-kindle"; } if string :matches "${docsend_evernote_run}" "true" { include "send-evernote"; } if string :matches "${docsend_trello_run}" "true" { include "send-trello"; }
# docsend_kindle_run_bool require ["include", "variables"]; global "docsend_kindle_run_bool"; global "docsend_running"; global "docsend_kindle_run"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; if string :matches "${docsend_running}" "text" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_pdf}"; } if anyof ( string :matches "${custom}" "true", allof ( string :matches "${default}" "true", not string :matches "${custom}" "false" ) ) { set "docsend_kindle_run_bool" "true"; } else { set "docsend_kindle_run_bool" "false"; }
あれあれ~?どこかで見たようなコードができましたね。
そうです。docsend_check_from_boolと全く同じ形をしています。先程「同様の考えを摘要」って書いたばかりですので、当然コードも同じようになります。それではこの「同様の考え」に名前をつけて、統合することにしましょう。
コードの後半部分を見ると、やりたいことの本質が見えてみます。そうです、「デフォルトの場合」と、「特に実行するかしないかを指定した場合」で実行するかどうかを決めるのでした。今回は "docsend_default_custom_bool" と名付け、コードを整理すると、次のようになります。
#test5-2-1 (抜粋) include "docsend_check_from"; set "docsend_running" "text"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "docsend_run"; include "trash"; } set "docsend_running" "pdf"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { foreverypart
# docsend_run require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_evernote_run"; global "docsend_trello_run"; set "docsend_default_custom" "kindle_run"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "send-kindle"; } if string :matches "${docsend_evernote_run}" "true" { include "send-evernote"; } if string :matches "${docsend_trello_run}" "true" { include "send-trello"; }
# docsend_default_custom_bool require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; global "docsend_check_from"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; if string :matches "${docsend_default_custom}" "check_from" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_pdf}"; } } elsif string :matches "${docsend_default_custom}" "kindle_run" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_pdf}"; } } if anyof ( string :matches "${custom}" "true", allof ( string :matches "${default}" "true", not string :matches "${custom}" "false" ) ) { set "docsend_default_custom_bool" "true"; } else { set "docsend_default_custom_bool" "false"; }
少し長くなっていますが、やっていることは単純ですので理解するには問題ないでしょう。それでは、docsend_runの残り部分も統合してしまいましょう。
#test5-2-2 require ["include", "variables", "mime", "foreverypart"]; global "subject"; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; # 元メールのSubject:を保存 if header :matches "Subject" "*" { set "subject" "${1}"; } include "docsend_check_from"; set "docsend_running" "text"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "docsend_run"; include "trash"; } set "docsend_running" "pdf"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_run"; break; } } include "trash"; } discard;
# docsend_run require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; set "docsend_default_custom" "kindle_run"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "send-kindle"; } set "docsend_default_custom" "evernote_run"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "send-evernote"; } set "docsend_default_custom" "trello_run"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "send-trello"; }
# docsend_default_custom_bool require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; global "docsend_check_from"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; if string :matches "${docsend_default_custom}" "check_from" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_check_from}"; set "custom" "${docsend_check_from_pdf}"; } } elsif string :matches "${docsend_default_custom}" "kindle_run" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_kindle_run}"; set "custom" "${docsend_kindle_run_pdf}"; } } elsif string :matches "${docsend_default_custom}" "evernote_run" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_evernote_run}"; set "custom" "${docsend_evernote_run_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_evernote_run}"; set "custom" "${docsend_evernote_run_pdf}"; } } elsif string :matches "${docsend_default_custom}" "trello_run" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_trello_run}"; set "custom" "${docsend_trello_run_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_trello_run}"; set "custom" "${docsend_trello_run_pdf}"; } } if anyof ( string :matches "${custom}" "true", allof ( string :matches "${default}" "true", not string :matches "${custom}" "false" ) ) { set "docsend_default_custom_bool" "true"; } else { set "docsend_default_custom_bool" "false"; }
docsend_check_fromを振り返る
一旦ここでdocsend_check_fromを振り返ってみましょう。
# docsend_check_from require ["include", "variables"]; global "docsend_check_from"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; global "kindle_convert"; global "evernote_notebook"; global "trello_assign"; if header :contains "From" "example1.co.jp" { set "docsend_check_from" "true"; set "docsend_kindle_run" "true"; set "kindle_convert" "true"; set "docsend_evernote_run" "true"; set "evernote_notebook" "テスト1"; set "docsend_trello_run" "true"; set "trello_assign" "exampleuser1"; } elsif header :contains "From" "example2.co.jp" { set "docsend_check_from" "true"; set "docsend_kindle_run" "true"; set "docsend_evernote_run" "true"; set "evernote_notebook" "テスト2"; set "docsend_trello_run" "true"; set "trello_assign" "exampleuser2"; }
コード全体を良く眺めてみると、docsend_*系の変数は docsend_default_custom_bool 内のみで使われていることが見て取れます。ですので、docsend_check_fromから移動させてみましょう。
まずは、既存のdocsend_check_fromを分かりやすく変形して、移動させます。
# docsend_check_from (一部抜粋)(移動途中) if header :contains "From" "example1.co.jp" { set "docsend_check_from" "true"; set "docsend_kindle_run" "true"; set "docsend_evernote_run" "true"; set "docsend_trello_run" "true"; } elsif header :contains "From" "example2.co.jp" { set "docsend_check_from" "true"; set "docsend_kindle_run" "true"; set "docsend_evernote_run" "true"; set "docsend_trello_run" "true"; } if header :contains "From" "example1.co.jp" { set "kindle_convert" "true"; set "evernote_notebook" "テスト1"; set "trello_assign" "exampleuser1"; } elsif header :contains "From" "example2.co.jp" { set "evernote_notebook" "テスト2"; set "trello_assign" "exampleuser2"; }
次に、変数名を少し変えてみます。デフォルト時の挙動状態を示しているのをわかりやすくするため、該当する変数名に "_default" を追加します。
# docsend_default_suctom_bool (一部抜粋) if header :contains "From" "example1.co.jp" { set "docsend_check_from_default" "true"; set "docsend_kindle_run_default" "true"; set "docsend_evernote_run_default" "true"; set "docsend_trello_run_default" "true"; } elsif header :contains "From" "example2.co.jp" { set "docsend_check_from_default" "true"; set "docsend_kindle_run_default" "true"; set "docsend_evernote_run_default" "true"; set "docsend_trello_run_default" "true"; } if string :matches "${docsend_default_custom}" "check_from" { if string :matches "${docsend_running}" "text" { set "default" "${docsend_check_from_default}"; set "custom" "${docsend_check_from_text}"; } elsif string :matches "${docsend_running}" "pdf" { set "default" "${docsend_check_from_default}"; set "custom" "${docsend_check_from_pdf}"; } }
さて、ここで少し振り返ると「動作中の状態を渡して、各処理を実行するかどうかについて返す」仕組みになっていますが、そもそも各処理を実行するかどうかについては、送信元に対してあらかじめ決まっていますので、(1)最初にデフォルトの設定を読む、(2)次に個別の設定をチェック、個別の設定が存在すれば設定を上書き、で各動作を最初から決定しておくことができます。
コードにすると以下のようになります。
# docsend_default_custom_bool (一部抜粋) set "docsend_check_from_text_bool" "${docsend_check_from_default}"; if not string :matches "${docsend_check_from_text}" "" { set "docsend_check_from_text_bool" "${docsend_check_from_text}"; }
最初に送信元の動作を決めると同時に各動作を決定するため、docsend_check_from_init を作成しそちらへ移動しておきます。
# docsend_check_from require ["include", "variables"]; # 以下の変数の値を設定(true/false)することができます global "docsend_check_from_default"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run_default"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run_default"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run_default"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; global "kindle_convert"; global "evernote_notebook"; global "trello_assign"; # default動作の設定 set "docsend_check_from_default" "false"; set "docsend_kindle_run_default" "true"; set "docsend_evernote_run_default" "true"; set "docsend_trello_run_default" "true"; # 送信元別の設定 if header :contains "From" "example1.co.jp" { set "docsend_check_from_default" "true"; set "kindle_convert" "true"; set "evernote_notebook" "テスト1"; set "trello_assign" "exampleuser1"; } elsif header :contains "From" "example2.co.jp" { set "docsend_check_from_default" "true"; set "evernote_notebook" "テスト2"; set "trello_assign" "exampleuser2"; } include "docsend_check_from_init";
# docsend_check_from_init require ["include", "variables"]; global "docsend_check_from_text_bool"; global "docsend_check_from_pdf_bool"; global "docsend_kindle_run_text_bool"; global "docsend_kindle_run_pdf_bool"; global "docsend_evernote_run_text_bool"; global "docsend_evernote_run_pdf_bool"; global "docsend_trello_run_text_bool"; global "docsend_trello_run_pdf_bool"; global "docsend_check_from_default"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run_default"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run_default"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run_default"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; set "docsend_check_from_text_bool" "${docsend_check_from_default}"; if not string :matches "${docsend_check_from_text}" "" { set "docsend_check_from_text_bool" "${docsend_check_from_text}"; } set "docsend_check_from_pdf_bool" "${docsend_check_from_default}"; if not string :matches "${docsend_check_from_pdf}" "" { set "docsend_check_from_pdf_bool" "${docsend_check_from_pdf}"; } set "docsend_kindle_run_text_bool" "${docsend_kindle_run_default}"; if not string :matches "${docsend_kindle_run_text}" "" { set "docsend_kindle_run_text_bool" "${docsend_kindle_run_text}"; } set "docsend_kindle_run_pdf_bool" "${docsend_kindle_run_default}"; if not string :matches "${docsend_kindle_run_pdf}" "" { set "docsend_kindle_run_pdf_bool" "${docsend_kindle_run_pdf}"; } set "docsend_evernote_run_text_bool" "${docsend_evernote_run_default}"; if not string :matches "${docsend_evernote_run_text}" "" { set "docsend_evernote_run_text_bool" "${docsend_evernote_run_text}"; } set "docsend_evernote_run_pdf_bool" "${docsend_evernote_run_default}"; if not string :matches "${docsend_evernote_run_pdf}" "" { set "docsend_evernote_run_pdf_bool" "${docsend_evernote_run_pdf}"; } set "docsend_trello_run_text_bool" "${docsend_trello_run_default}"; if not string :matches "${docsend_trekki_run_text}" "" { set "docsend_trello_run_text_bool" "${docsend_trello_run_text}"; } set "docsend_trello_run_pdf_bool" "${docsend_trello_run_default}"; if not string :matches "${docsend_trello_run_pdf}" "" { set "docsend_trello_run_pdf_bool" "${docsend_trello_run_pdf}"; }
# docsend_default_custom_bool require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; global "docsend_check_from_text_bool"; global "docsend_check_from_pdf_bool"; global "docsend_kindle_run_text_bool"; global "docsend_kindle_run_pdf_bool"; global "docsend_evernote_run_text_bool"; global "docsend_evernote_run_pdf_bool"; global "docsend_trello_run_text_bool"; global "docsend_trello_run_pdf_bool"; if string :matches "${docsend_default_custom}" "check_from" { if string :matches "${docsend_running}" "text" { set "docsend_default_custom_bool" "${docsend_check_from_text_bool}"; } elsif string :matches "${docsend_running}" "pdf" { set "docsend_default_custom_bool" "${docsend_check_from_pdf_bool}"; } } elsif string :matches "${docsend_default_custom}" "kindle_run" { if string :matches "${docsend_running}" "text" { set "docsend_default_custom_bool" "${docsend_kindle_run_text_bool}"; } elsif string :matches "${docsend_running}" "pdf" { set "docsend_default_custom_bool" "${docsend_kindle_run_pdf_bool}"; } } elsif string :matches "${docsend_default_custom}" "evernote_run" { if string :matches "${docsend_running}" "text" { set "docsend_default_custom_bool" "${docsend_evernote_run_text_bool}"; } elsif string :matches "${docsend_running}" "pdf" { set "docsend_default_custom_bool" "${docsend_evernote_run_pdf_bool}"; } } elsif string :matches "${docsend_default_custom}" "trello_run" { if string :matches "${docsend_running}" "text" { set "docsend_default_custom_bool" "${docsend_trello_run_text_bool}"; } elsif string :matches "${docsend_running}" "pdf" { set "docsend_default_custom_bool" "${docsend_trello_run_pdf_bool}"; } }
これで、実際に動作を記述する "docsend_check_from" が随分とすっきりしました。それ以外の部分は、動かしてみて不具合が起きない限りほとんど見なくても良いのがありがたいですね。
まとめ
今回は、各部分を一つずつ見ていってPDFとTEXTを送り込むための統合作業を行いました。
次回も引き続き統合作業を行っていきます。お楽しみに。
[asin:B07ZWCDYZG:detail]
evernote.com
trello.com
前回までの記事はこちらです。 tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp