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


* AppleScriptで`append`と`set 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`するハンドラ__を作成


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