Apple Mail to Evernote Applescript – Japanese Language

Description

Japanese version of the current "Apple Mail to Evernote Applescript". (A new version is coming soon!)

The Code

(*
http://veritrope.com
Apple Mail to Evernote
Version 1.3-JAPANESE
September 16, 2010
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/mail-to-evernote

WITH GREAT THANKS TO SYNKURO AND M.E. HORI FOR THEIR ASSISTANCE!
           
*)

property successCount : 0
--property EVnotebook : ""

(* CHECK FOR GROWL *)
tell application "System Events"
    set processnames to name of every process
end tell
if "GrowlHelperApp" is in processnames then
    tell application "GrowlHelperApp"
        set the allNotificationsList to {"Success Notification", "Failure Notification"}
        set the enabledNotificationsList to {"Success Notification", "Failure Notification"}
        register as application ¬
            "Mail to Evernote" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application "Evernote"
    end tell
end if
(*MAIN PROGRAM *)

set myPath to (path to home folder)

(*TEMP FILES PROCESSED ON THE DESKTOP*)
set ExportFolder to ((path to desktop folder) & "Temp Export From Mail:") as string
set SaveLoc to my f_exists(ExportFolder)

tell application "Mail"
    try
        if (selection is not 0) then
            set defaultTag to "メールメッセージ"
            set myTitle to "メッセージ"
            set EVnotebook to ""
            display dialog "" & ¬
                "タグを入力(コロンかコンマで分割)" with title "Apple Mail から Evernote へ書き出し" default answer defaultTag buttons {"既定のノートブックに作成", "ノートブックを選択", "キャンセル"} default button "既定のノートブックに作成" cancel button ¬
                "キャンセル" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
            set dialogresult to the result
            set userInput to text returned of dialogresult
            set ButtonSel to button returned of dialogresult
            set theDelims to {":", ","}
            set EVTag to my Tag_List(userInput, theDelims)
            set theMessages to selection
            if ButtonSel is "ノートブックを選択" then set EVnotebook to my Notebook_List()
           
            repeat with thisMessage in theMessages
                set myTitle to the subject of thisMessage
                set myHeaders to content of header "content-type" of thisMessage
                set myContent to the content of thisMessage
                set mySource to the source of thisMessage
                set ReplyAddr to the reply to of thisMessage
                set EmailDate to the date received of thisMessage
                set MsgLink to "message://%3c" & thisMessage's message id & "%3e"
                set ex to my extractBetween(ReplyAddr, "<", ">") -- extract the Address
                set myText to "Message from mailto:" & ex & " on " & EmailDate & ":" & return & return & myContent
               
                (*START ATTACHMENT PROCESSING*)
                if thisMessage's mail attachments is not {} then
                    set attCount to 0
                    repeat with theAttachment in thisMessage's mail attachments
                        set theFileName to ExportFolder & theAttachment's name
                        try
                            save theAttachment in theFileName
                        end try
                        set attTitle to "添付ファイル:" & myTitle
                       
                        tell application "Evernote"
                           
                            if EVnotebook is not "" then
                                set n to create note from file theFileName title attTitle tags EVTag notebook EVnotebook
                                set creation date of n to EmailDate
                                set source URL of n to MsgLink
                               
                            else
                                set n to create note from file theFileName title attTitle tags EVTag
                                set creation date of n to EmailDate
                                set source URL of n to MsgLink
                            end if
                           
                        end tell
                       
                        set attCount to attCount + 1
                    end repeat
                end if
               
                tell application "Evernote"
                    try
                        if EVnotebook is not "" then
                           
                            set n to create note with text myText title myTitle notebook EVnotebook tags EVTag
                            set creation date of n to EmailDate
                            set source URL of n to MsgLink
                        else
                            set n to create note with text myText title myTitle tags EVTag
                            set creation date of n to EmailDate
                            set source URL of n to MsgLink
                           
                           
                        end if --EVNOTEBOOK
                    end try
                end tell
                set successCount to successCount + 1
            end repeat
            (*DELETE THE TEMP FILE/FOLDER *)
            tell application "Finder" to delete SaveLoc
           
            (* GROWL *)
            if "GrowlHelperApp" is in processnames then
                tell application "GrowlHelperApp" -- GROWL SUCCESS
                    notify with name ¬
                        "Success Notification" title ¬
                        "書き出し成功" description " " & successCount & ¬
                        " 件のメッセージを Evernote に書き出しました!" application name "Mail to Evernote"
                end tell
                set successCount to 0
            end if
        else if "GrowlHelperApp" is in processnames then
            tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
                notify with name ¬
                    "Failure Notification" title ¬
                    "読み込み失敗" description ¬
                    "ヘッドラインまたはタブが選択されていません!" application name "Mail to Evernote"
            end tell
        else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
            display dialog "ヘッドラインまたはタブが選択されていません!" with icon 0
        end if
        (* ERROR HANDLING *)
    on error errText number errNum
        if "GrowlHelperApp" is in processnames then
            tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
                notify with name ¬
                    "Failure Notification" title ¬
                    "書き出し失敗" description "次のエラーで書き出しに失敗しました:" & return & myTitle & ¬
                    ""  " & return & errText ¬
                    application name "
Mail to Evernote"
            end tell
        else if "
GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
            display dialog "
アイテムの読み込みに失敗しました:" & errNum & return & errText with icon 0
        end if
    end try
end tell
(* SUBROUTINES *)
--FOLDER EXISTS
on f_exists(the_path)
    try
        get the_path as alias
        set SaveLoc to the_path
    on error
        tell application "
Finder" to make new folder with properties {name:"Temp Export From Mail"}
    end try
end f_exists

-- CLEAN-UP EMAIL ADDRESS
on extractBetween(SearchText, startText, endText)
    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to startText
    set endItems to text of text item -1 of SearchText
    set AppleScript's text item delimiters to endText
    set beginningToEnd to text of text item 1 of endItems
    set AppleScript's text item delimiters to tid
    return beginningToEnd
end extractBetween
--TAG SELECTION SUBROUTINE
on Tag_List(userInput, theDelims)
    set oldDelims to AppleScript's text item delimiters
    set theList to {userInput}
    repeat with aDelim in theDelims
        set AppleScript's text item delimiters to aDelim
        set newList to {}
        repeat with anItem in theList
            set newList to newList & text items of anItem
        end repeat
        set theList to newList
    end repeat
    return theList
    set AppleScript's text item delimiters to oldDelims
end Tag_List
--EVERNOTE NOTEBOOK SELECTION SUBROUTINE
on Notebook_List()
    tell application "
Evernote"
        activate
        set listOfNotebooks to {} (*PREPARE TO GET EVERNOTE'S LIST OF NOTEBOOKS *)
        set EVNotebooks to every notebook (*GET THE NOTEBOOK LIST *)
        repeat with currentNotebook in EVNotebooks
            set currentNotebookName to (the name of currentNotebook)
            copy currentNotebookName to the end of listOfNotebooks
        end repeat
        set Folders_sorted to my simple_sort(listOfNotebooks) (*SORT THE LIST *)
        set SelNotebook to choose from list of Folders_sorted with title "
Evernote のノートブックを選択" with prompt ¬
            "
現在の Evernote ノートブック" OK button name "OK" cancel button name "新規ノートブック" (*USER SELECTION FROM NOTEBOOK LIST *)
        if (SelNotebook is false) then (*CREATE NEW NOTEBOOK OPTION *)
            set userInput to ¬
                text returned of (display dialog "
新規ノートブック名を入力:" default answer "")
            set EVnotebook to userInput
        else
            set EVnotebook to item 1 of SelNotebook
        end if
    end tell
end Notebook_List
--SORT SUBROUTINE
on simple_sort(my_list)
    set the index_list to {}
    set the sorted_list to {}
    repeat (the number of items in my_list) times
        set the low_item to "
"
        repeat with i from 1 to (number of items in my_list)
            if i is not in the index_list then
                set this_item to item i of my_list as text
                if the low_item is "
" then
                    set the low_item to this_item
                    set the low_item_index to i
                else if this_item comes before the low_item then
                    set the low_item to this_item
                    set the low_item_index to i
                end if
            end if
        end repeat
        set the end of sorted_list to the low_item
        set the end of the index_list to the low_item_index
    end repeat
    return the sorted_list
end simple_sort