如何删除Evernote中笔记的最后一行?

时间:2019-02-01 18:55:29

标签: applescript evernote

我想删除Evernote中每个音符的最后一行。 使用Shell脚本很容易,例如使用wc获取注释的长度,然后使用head打印除最后一行或sed以外的所有行。 但是我找不到将注释内容流式传输到外壳的方法。 我已经尝试过这个(还有很多其他)

do shell script "echo <<<" & noteText

有什么想法吗?

这是整个脚本

tell application "Evernote"
set noteList to find notes ""
repeat with i from 1 to number of items in noteList
    set this_note to item i of noteList
    set this_note_text to ENML content of this_note
    my removeLastLine(this_note_text)
end repeat
end tell

on removeLastLine(note)
set countCmd to "wc <<< " & note
(* do shell script countCmd *)
set noteText to note as string
log noteText
do shell script "echo <<<" & noteText
(* set newNote to do shell script "head -(size - 1)" *)
end removeLastLine

2 个答案:

答案 0 :(得分:1)

好的,这是我使用Evernote Note的HTML解决方案
注意:“注释”中的任何图像或附件都可能丢失。

将其作为注释的HTML:

<div id="en-note"><div>2019-02-02  21:47 GMT-6</div><div><br/></div><div>REF:  <a href="https://stackoverflow.com/questions/54485617/how-can-i-remove-the-last-line-of-a-note-in-evernote">How can I remove the last line of a note in evernote?</a></div><div><br/></div><div>This is a test.</div><div><br/></div><ol><li>One</li><li>two</li><li>three</li></ol><div><br/></div><div>This is the last line where X&gt;Y, A&lt;B, and a path of /dir1/dir2/file.text.<br/></div></div>

此AppleScript将删除最后一个用户行。
注意:我仍在为此测试RegEx,因此应将其视为BETA / DRAFT版本。但这应该足以让您入门。

请告诉我们这是否适合您,或者您有任何疑问/问题。

property ptyScriptName : "Remove Last User Text Line from Evernote Note"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2019-02-03"
property ptyScriptAuthor : "JMichaelTX"

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use scripting additions

--- RegEx to Find Last User Text Line ---
## (<div>(?!.+?<div>).+?<\/div>)(<\/div>)$
set reFind to "(<div>(?!.+?<div>).+?<\\/div>)(<\\/div>)$"


tell application "Evernote"
  set noteList to selection
  set oNote to item 1 of noteList
  set bodyHTML to HTML content of oNote

  set bodyNewHTML to my regexChange(reFind, "", bodyHTML)
  set HTML content of oNote to bodyNewHTML
end tell


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on regexChange(pFindStr, pReplaceStr, pSourceStr) -- @RegEX @Change @Replace @Strings @ASObjC
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  (*  VER: 2.0    2018-07-06
    PURPOSE:  Change ALL Occurances of pFindStr into pReplaceStr in pSourceStr
    METHOD:    Uses ASObjC RegEx (which is based on ICU Regex)
    PARAMETERS:
      • pFindStr    | text |  RegEx Pattern to Find
      • pReplaceStr | text |  Replace string which
          • May contain these RegEx MetaChars:  
                \\n, \\r, \\t for linefeed, return, and tab
                $<CG>, where <CG> is Capture Group number
      • pSourceStr   | text |  Source String to be searched

    RETURNS:  Revised String with all changes that were found.

    AUTHOR:  JMichaelTX -- with help from NigelGarvey
    ## REQUIRES:  use framework "Foundation"
    REF:  
      1.  2018-07-06, NigelGarvey, Late Night Software Ltd.
          ASObjC RegEx Change Handler
          http://forum.latenightsw.com/t/asobjc-regex-change-handler/1395/10
      2.  ICU RegEx Users Guide
          http://userguide.icu-project.org/strings/regexp
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  *)
  set nsCurApp to current application

  -- Replace any backslash-n sequences in the replace string with actual character --

  set nsReplaceStr to nsCurApp's NSString's stringWithString:pReplaceStr
  set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\n" withString:(linefeed)
  set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\r" withString:(return)
  set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\t" withString:(tab)

  set nsSourceStr to nsCurApp's NSString's stringWithString:pSourceStr

  set nsSourceStr to (nsSourceStr's stringByReplacingOccurrencesOfString:pFindStr withString:nsReplaceStr options:(nsCurApp's NSRegularExpressionSearch) range:{0, nsSourceStr's |length|()})

  return nsSourceStr as text

end regexChange
--~~~~~~~~~~~~~~~~~~~~ END of Handler ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

答案 1 :(得分:0)

这不是解决方案。指导/建议与注释行不太吻合。

您的任务要比看起来要复杂得多。您要克服两个主要挑战:
(1)删除用户文本的最后一行(与ENML文本的最后一行相对)
(2)更新现有的注释ENML。

#1可以克服。 ENML文本的最后一行包含您不想删除的XML标记。它与您在Evernote应用程序中查看的用户文本的最后一行不同。这是一个示例ENML文本,其中包含3行文本。最后一个用户行嵌入在最后一个ENML行中,该行具有多个标签。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div>2019-02-02  21:47 GMT-6</div><div><br/></div><div>This is a test.</div><div><br/></div><div><ol><li>One</li><li>two</li><li>three</li></ol><div><br/></div></div><div>this is the last line.</div><div><br/></div></en-note>

#2是一个巨大的挑战。您可以通过脚本更改 Note HTML,但不能更改Note ENML。而且HTML将不包含对嵌入式图像和附件的任何引用。我看到的唯一解决方法是编辑~/Library/Application Support/com.evernote.Evernote文件夹(称为“ Evernote数据库文件夹”)中Evernote数据文件夹中埋藏的实际文件。 OTOH,如果要使用修改后的ENML文本创建新的注释,则可以轻松完成。从脚本编辑器中打开Evernote脚本字典,以查看可用的内容。

如果您只想创建一个新注释,请更新您的问题以表明这一点。

如果明天我有更多时间,我会进一步检查。


更新2019-02-03 18:17 GMT-6

因此,现在OP声明他只能使用HTML。因此,这是EN注释中的HTML示例

<div id="en-note"><div>2019-02-02  21:47 GMT-6</div><div><br/></div><div>REF:  <a href="https://stackoverflow.com/questions/54485617/how-can-i-remove-the-last-line-of-a-note-in-evernote">How can I remove the last line of a note in evernote?</a></div><div><br/></div><div>This is a test.</div><div><br/></div><ol><li>One</li><li>two</li><li>three</li></ol><div><br/></div><div>this is the last line.</div><div><br/></div></div>

最后一个USER行是:
This is a test.

挑战在于,在最后</div>

之前的这一行之后还会有其他非文本内容

我正在研究RegEx。如果有任何RegEx专家那里有解决方案,请跳进去。太遗憾了,我们可以为此使用Web页面DOM。这将比RegEx容易得多。