Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.
  • 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: スクリプト中で記述していたものをハンドラとしてまとめ
szk-3
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away