LoginSignup
2
2

More than 5 years have passed since last update.

AppleScriptでEvernoteを操作するときのappendとset HTML contentに関するバグを回避するハンドラ

Last updated at Posted at 2015-12-23
  • AppleScriptでappendset HTML contentを使うとエラーが発生することがある
    • error "この操作により月間アップロード許容量を超過します。" number 1
    • 2014年7月頃から
    • ノートの状態によって使えたり使えなかったり
  • appendに関するバグは以下のとおり
    • 同期してから内容が編集されていないノートを対象にするとエラー
    • appendの前にset HTML contentを使えばエラーを回避できる
    • ノートと全く同じ内容をset HTML contentで上書きしてからappendするハンドラを作成
  • set HTML contentに関するバグは以下のとおり
    • set HTML contentを使った直後に再度set HTML contentでエラー
    • set HTML contentの直前にappendを使えばエラーを回避できる
    • 適当な内容をappendしてからset HTML contentするハンドラを作成
append_and_set_HTML_content.scpt
tell application "Evernote"
    set {aNote} to selection

    --append aNote text "append text"
    --> error "この操作により月間アップロード許容量を超過します。" number 1

    my appendDataToNoteInEvernote(aNote, text, "append text")

    --set HTML content of aNote to "<div>HTML content</div>"
    --> error "この操作により月間アップロード許容量を超過します。" number 1

    my setHTMLContentOfNote(aNote, "<div>HTML content</div>", false)
end tell

on appendDataToNoteInEvernote(aNote, dataClass, dataContent)
    --overlap: append aNote dataClass dataContent
    --dataClass: text/html(HTML)/attachment
    tell application "Evernote"
        tell aNote
            repeat 2 times
                try
                    if dataClass = text then
                        append text (dataContent as text)
                    else if dataClass = HTML then
                        append html (dataContent as text)
                    else if dataClass = attachment then
                        append attachment (dataContent as file)
                    else
                        error "Invalid dataClass: " & dataClass
                    end if
                    exit repeat
                on error number 1
                    my setHTMLContentOfNote(aNote, HTML content, false)
                end try
            end repeat
        end tell
    end tell
end appendDataToNoteInEvernote

on setHTMLContentOfNote(aNote, HTMLContent as text, sameCheck as boolean)
    tell application "Evernote"
        tell aNote
            ignoring white space
                if sameCheck and HTML content = HTMLContent then return
            end ignoring
            repeat while HTMLContent contains "<div id=\"en-note\">"
                set HTMLContent to my extractText(HTMLContent, {text:"<div id=\"en-note\">", forwardSearch:true, containText:false}, {text:"</div>", forwardSearch:false, containText:false})
            end repeat
            try
                append html ""
            on error number 1
            end try
            set HTML content to HTMLContent
        end tell
    end tell
end setHTMLContentOfNote

on extractText(theText, beginText, endText)
    --beginText, endText例: {text:"a", forwardSearch:false, containText:true}
    set beginText to (beginText as record) & {forwardSearch:true, containText:false}
    set endText to (endText as record) & {forwardSearch:true, containText:false}

    if theText does not contain text of beginText or theText does not contain text of endText then error "引数の文字列が含まれません"

    set scraps to my split(theText, text of beginText)
    if forwardSearch of beginText then
        set theText to my join(rest of scraps, text of beginText)
    else
        set {theText} to reverse of scraps
    end if
    set scraps to my split(theText, text of endText)
    if forwardSearch of endText then
        set {theText} to scraps
    else
        set theText to my join(reverse of rest of reverse of scraps, text of endText)
    end if
    if containText of beginText then
        set theText to "" & text of beginText & theText
    end if
    if containText of endText then
        set theText to "" & theText & text of endText
    end if
    return theText
end extractText

on split(str as text, delim)
    set oldDel to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    set aList to text items of str
    set AppleScript's text item delimiters to oldDel
    return aList
end split

on join(textList, term as text)
    set oldDel to AppleScript's text item delimiters
    set AppleScript's text item delimiters to term
    set aText to textList as string
    set AppleScript's text item delimiters to oldDel
    return aText
end join

更新履歴

  • 2015-12-23: スクリプト中で記述していたものをハンドラとしてまとめ
2
2
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
2
2