如何解决 AppleScript 中的这些 grep 错误?

How do I resolve these grep errors in an AppleScript?

我在将有效的 shell 脚本正则表达式转换为 AppleScript 时遇到问题。我需要在 AppleScript 方面进行哪些更改? (感谢那些在 https://macscripter.net/viewtopic.php?pid=205905#p205905 中提供帮助的人)

这是有效的 shell 脚本正则表达式

john@mac ~ % ExtractedNoteContents="Test Project                 
- [ ] Potential Task 1
- [ ] <0001f7e2> Potential Task 2 (with some comment; additions)

<0001f7e2> Potential Task 3
More text"
john@mac ~ % echo $ExtractedNoteContents | grep -o '<0001f7e2>.*'
 Potential Task 2 (with some comment; additions)
 Potential Task 3

这是失败的 AppleScript 正则表达式

set ExtractedNoteContents to "Test Project
- [ ] Potential Task 1
- [ ]  Potential Task 2 (with some comment; additions)

 Potential Task 3
More text"
do shell script "echo " & ExtractedNoteContents & " | grep -o '<0001f7e2>.*'"

这是我得到的错误:

sh: line 1: -: command not found
sh: -c: line 2: syntax error near unexpected token `('
sh: -c: line 2: `- [ ]  Potential Task 2 (with some comment; additions)'

变量传递给do shell script 命令时需要使用quoted form

变化:

do shell script "echo " & ExtractedNoteContents & " | grep -o '<0001f7e2>.*'"

收件人:

do shell script "echo " & quoted form of ExtractedNoteContents & " | grep -o '<0001f7e2>.*'"

或者:

do shell script "echo " & ExtractedNoteContents's quoted form & " | grep -o '<0001f7e2>.*'"

我的首选方法是,例如:

do shell script "grep -o '<0001f7e2>.*'<<<" & ExtractedNoteContents's quoted form

我更喜欢使用 Here strings 而不是 echo | 我个人认为它更具可读性。


注意:示例 AppleScript code 就是这样,没有任何包含的 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。看看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors