由來
上午想在Evernote中對一篇文章的子標題進行加紅操作,沒有格式刷只能一遍又一遍地選中文字,再打開調色板,選中顏色,關閉調色板,真是無以吐槽。
搜索了一下相關增強Evernote編輯能力的文章,發(fā)現(xiàn)了知乎上的一篇分享,以及v2ex上的這個帖子,都是Windows下借助Autohotkey來實現(xiàn)增加Evernote編輯能力的。想起以前在Windows下用Autohotkey那可真是得心應手啊,可惜Mac下沒有。
想起了之前用AppleScript來實現(xiàn)一些快捷操作,在Mac下是否能用AppleScript腳本來簡化上面繁瑣的加紅操作呢?
簡單看了一下上面的Autohotkey腳本代碼,發(fā)現(xiàn)只需要把選中文字轉換成html格式然后粘貼就行。然而AppleScript并沒有像Autohotkey那樣簡單的SetHTML命令。
突破
幾經折騰,終于折騰出了下面的可行代碼:
set appName to "Evernote"
-- if application appName is running then
tell application id (id of application appName)
activate
tell application "System Events"
keystroke "c" using command down
end tell
set selectedText to the clipboard as ?class utf8?
-- the clipboard as "HTML"
set the clipboard to "<span style=\"color:red;\">" & selectedText & "</span>"
set theHEX to do shell script "LC_ALL=en_US.UTF-8 pbpaste | hexdump -ve '1/1 \"%.2x\"'"
if theHEX is "" then
beep
else
run script "set the clipboard to ?data HTML" & theHEX & "?"
tell application "System Events"
keystroke "v" using {command down}
end tell
end if
end tell
-- end if
原理就是復制選中的文字,加上html顏色的標簽,然后變成html格式內容,粘貼即可。
其中的難點在于把html源碼變成剪貼板認可的html富文本格式內容,這個借助了shell中的hexdump命令以及用剪貼板作為中介。突破了這個難點之后,你就可以任意自定義html格式了。
Automator自動化
要與Evernote集成,就要借助于Automator工具了。 Automator 是 Mac 自帶的神奇小機器人,這次我們就要用它的 Workflow 功能來將腳本綁定到系統(tǒng)快捷鍵上。 先來設置 Automator,Automator 可以在 Spotlight 里快速啟動。
1、菜單里選”新建“,選取文稿類型“服務”;
2、在出來的窗口右側頂部設置“服務”收到選定的“文本”,位于”Evernote“,不要勾選”用輸出內容替換選中文本“
3、在左側選取“運行AppleScript”,雙擊或直接拖到右側區(qū)域內;
4、在出現(xiàn)的 AppleScript 編輯窗口里輸入代碼,之后保存為你喜歡的名字;
5、此時在系統(tǒng)左上角 Automator 的下拉菜單里“服務”一欄就已經有剛才你保存的服務啦!現(xiàn)在點擊剛剛保存的服務的名稱運行一次看看~~(一定要運行一次哦)
在Automator中的腳本可以更簡單:
on run {input, parameters}
-- the clipboard as "HTML"
set the clipboard to "<span style=\"color:red;\">" & input & "</span>"
set theHEX to do shell script "LC_ALL=en_US.UTF-8 pbpaste | hexdump -ve '1/1 \"%.2x\"'"
if theHEX is "" then
beep
else
run script "set the clipboard to ?data HTML" & theHEX & "?"
-- set input to the clipboard
tell application "System Events"
keystroke "v" using {command down}
end tell
end if
--display dialog (the clipboard)
-- return input
end run
保存的位置在/Users/yourusername/Library/Services/
現(xiàn)在開始設置快捷鍵。
1、點擊 Automator 下拉菜單中“服務->服務偏好設置”;
2、在彈出的服務偏好設置窗口右側,從“通用”中找到剛保存的服務名稱,選中該服務,右側可以看到“添加快捷鍵”的按鈕;
3、點擊“添加快捷鍵”按鈕,在鍵盤上按下需要設置的快捷鍵。
我給自己自定義的快捷鍵是cmd+shift+1。以后要在Evernote中對選中的文字進行紅色高亮時,按下cmd+shift+1就可以搞定。
是不是很簡單? 這樣,你就可以自定義一些自己常用的文本格式,綁定快捷鍵,然后就像Word中的樣式表一樣,一鍵就可以更換自己需要的格式。
這只是一個簡單的例子,相信實現(xiàn)其它的文本格式也不是難事,權當拋磚引玉,大家應該能做出更高級的功能。
一些額外的發(fā)現(xiàn):
將文本轉換成完整html源碼的AppleScript腳本:
set customHTML to "123"
set theMsg to do shell script "echo " & quoted form of customHTML & space & "| textutil -stdin -stdout -format html -convert html -encoding UTF-8"
set the clipboard to the theMsg
在terminal里用于查看剛剛復制的html內容的html源碼的命令:
osascript -e 'the clipboard as "HTML"'|perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'
通過 AppleScript 連接印象筆記
https://dev.yinxiang.com/doc/articles/applescript.php
查看Evernote api for AppleScript的方法:
在Evernote的菜單中,選擇 "文件 > 打開字典..." 然后在顯示的應用列表中選擇Evernote。
后記:
后來發(fā)現(xiàn)Evernote還是有樣式高亮操作的,菜單路徑是格式——樣子——高亮顯示,快捷鍵是Ctrl+Command+H