Evernoteでも簡単にノートリンクを追加したい
テキスト解析は難しいので、ダイアログでテキストを入力して、その検索結果からノートを選び、そのノートのリンクを追加する、という形を模索する。
Evernoteの場合、同名のタイトルのノートが存在できるので、テキストだけでは一意に定まらない、という問題もある。
まずダイアログの実装。
code:scpt.scpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
end tell
これで入力したテキストが取得できた
タグの指定などを#とかで行って解析してもいい。
この変数を使って、Evernoteを検索する。
code:scpt.scpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
set resultNotes to find notes query
end tell
resultNotesに検索結果のノートリストが入っている(複数個)
この段階でむっちゃ時間がかかる。
検索ワードが空っぽだった場合の処理が必要
code:scpt.scpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
if query is not "" then
set resultNotes to find notes query
end if
end tell
空っぽでなかったときだけ検索を実行することにした
よくよく考えたら、変数がリストになっていなかった。
code:scpt.scpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
if query is not "" then
set resultNotes to find notes query
--ノート名を表示させるリストを作る
set noteTitleList to {}
repeat with i in resultNotes
set end of noteTitleList to title of i
end repeat
choose from list noteTitleList
set userSelect to result
end if
end tell
https://gyazo.com/b0ebe75f28d19436e1f7533f8a3ccfd4
かなり野暮ったい方法だが、ユーザーが選択したテキストから必要なノートのノートリンクを探す
code:scpt.scpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
if query is not "" then
set resultNotes to find notes query
--ノート名を表示させるリストを作る
set noteTitleList to {}
repeat with i in resultNotes
set end of noteTitleList to title of i
end repeat
choose from list noteTitleList
set userSelect to result
--該当するノートタイトルのノートリンクを探す
repeat with x in resultNotes
if title of x contains userSelect then
set userNotelink to note link of x
exit repeat
end if
end repeat
end if
end tell
choose from listで何番目の項目が選択されたかがわかると、もう少し楽?
テキストしか返ってこないっぽい?
この中身をクリップボードに移すのが一番簡単。
code:scrpt
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
if query is not "" then
set resultNotes to find notes query
--ノート名を表示させるリストを作る
set noteTitleList to {}
repeat with i in resultNotes
set end of noteTitleList to title of i
end repeat
choose from list noteTitleList
set userSelect to result
--該当するノートタイトルのノートリンクを探す
repeat with x in resultNotes
if title of x contains userSelect then
set userNotelink to note link of x
exit repeat
end if
end repeat
--ノートタイトルをクリップボード経由でペースト
set tempClipboard to get the clipboard
set the clipboard to userNotelink
tell application "System Events"
keystroke "v" using {command down}
end tell
delay 1
set the clipboard to tempClipboard
end if
end tell
できたのは、できたが、フォントが悲惨だし、ノートリンクが直になっている。
https://gyazo.com/1e86167cd279fb63ecc8469b9c7482e2
師匠のコードから、HTMLにしてクリップボードにコピーできるサブルーチンを拝借
code:scirp
use scripting additions
use framework "AppKit"
use framework "Foundation"
tell application "Evernote"
set userInput to display dialog "検索ワードを入力してください" default answer ""
set query to text returned of userInput
if query is not "" then
set resultNotes to find notes query
--ノート名を表示させるリストを作る
set noteTitleList to {}
repeat with i in resultNotes
set end of noteTitleList to title of i
end repeat
choose from list noteTitleList
set userSelect to result
--該当するノートタイトルのノートリンクを探す
repeat with x in resultNotes
if title of x contains userSelect then
set userNotelink to note link of x
exit repeat
end if
end repeat
--ノートタイトルをクリップボード経由でペースト
set tempClipboard to get the clipboard
my copyHTML("<a href=\"" & userNotelink & "\">" & userSelect & "</a>")
tell application "System Events"
keystroke "v" using {command down}
end tell
delay 1
set the clipboard to tempClipboard
end if
end tell
--以下サブルーチン
on copyHTML(HTML)
--require framework: AppKit, Foundation
set pasteboard to current application's NSPasteboard's generalPasteboard()
pasteboard's clearContents()
pasteboard's declareTypes:{current application's NSPasteboardTypeHTML, current application's NSPasteboardTypeString, current application's NSPasteboardTypeRTF} owner:me
pasteboard's setString:HTML forType:(current application's NSPasteboardTypeHTML)
set attributedString to my attributedStringOfHTMLText(HTML)
pasteboard's setString:(attributedString's |string|()) forType:(current application's NSPasteboardTypeString)
set attributes to current application's NSDictionary's dictionaryWithObject:(current application's NSHTMLTextDocumentType) forKey:(current application's NSDocumentTypeDocumentAttribute)
set HTMLData to attributedString's RTFFromRange:{location:0, |length|:attributedString's |length|()} documentAttributes:attributes
pasteboard's setData:HTMLData forType:(current application's NSPasteboardTypeRTF)
end copyHTML
on attributedStringOfHTMLText(HTML as text)
--require framework: AppKit, Foundation
set HTMLString to current application's NSString's stringWithString:HTML
set HTMLData to HTMLString's dataUsingEncoding:(current application's NSUnicodeStringEncoding)
return current application's NSAttributedString's alloc()'s initWithHTML:HTMLData documentAttributes:(missing value)
end attributedStringOfHTMLText
できた。
https://gyazo.com/1def1560b0bd46180654f622be5d78bb
こういう感じ。
https://gyazo.com/a1ff6c3484772f6ceb7400d370066692
https://gyazo.com/2d4b1a9bfdbba7bea2876205030b4938
ただし、検索ワードを入力してから、リストが表示される間に、キッチンに行って水を飲んで帰ってこれるくらいの待ち時間がかかる
毎回検索するのではなく、キャッシュにしたい。
あるいは検索対象を絞り込むか?
inboxだけにする?
このようにして放り込みたいノートにはどんな種類があるか?
本
プロジェクト・タスク
それくらい?
限定できるなら、そのタグだけを対象にすればいい。
タグの指定ができるようにする?
しかし、正確に覚えているとは限らない
結論
Scrapboxはむちゃ便利