[求助]我想用Applescript写一个将摘录的文本自动转化为标题的功能

各位大佬好!
我想用Applescript写一个将摘录的文本自动转化为标题的功能,虽然有插件可以直接实现,但我想结合keyboard-maestro的面板功能制作一个更适用于自己的工具栏,但现目前写了个代码不能,不知道有没有大佬能帮我看看呢:

tell application "MarginNote 3"
	tell front notebook
		set noteids to note ids
		set weid to item 1 of noteids
		set weidiction to fetch dictionaries weid
		set targetNote to item 1 of weidiction
		
		set title of targetNote to excerpt text of targetNote
		set excerpt text of targetNote to ""
		
		set weidiction to targetNote
	end tell
end tell

目前的思路是通过获取笔记的id,然后对笔记id的字典中的tile和excerpt text进行了替换,存在了targetNote这个变量中,但不知道怎么把targetNote这个变量传回fetch dictionaries weid呢,恳求大佬们帮我优化优化~

这段代码你是从哪获得参考写出来的?

要想通过Applescript实现摘录转标题,有两种情况:

  1. 将整本笔记本中所有卡片的摘录都转换成标题
  2. 将笔记本中指定的某张卡片的摘录转换成标题

前者比较容易实现,后者需要用到 search note in notebook 这个方法,将指定卡片的文本内容给这个方法用来执行搜索,这样才能锁定该卡片的id。

下面是我这边测试通过的代码,:warning:需要注意:warning::这些代码不适用于已经被OhmyMN处理过的卡片,特别是使用OhmyMN处理过摘录和标题的卡片,因为会获取不到摘录字段的内容。

整本笔记本批量执行:摘录转标题

tell application "MarginNote 3"

	# 获取当前查看的笔记本
	set nbk to item 1 of (search notebook "") # 搜索最近新查看的笔记本,即当前打开的笔记本
	set nLst to notes of nbk # 获取笔记本里的全部笔记
	
	# 对笔记本中所有笔记执行循环
	repeat with n in nLst
		set x to excerpt text of n # 使用1个临时变量x来储存笔记的摘录文本
		set title of n to x # 将笔记的标题设置为摘录文本
		set excerpt text of n to "" # 将笔记的摘录文本设置为空
	end repeat
	
end tell

指定单张卡片执行:摘录转标题

tell application "MarginNote 3"

	# 获取当前查看的笔记本
	set nbk to id of item 1 of (search notebook "") # 搜索最近新查看的笔记本,即当前打开的笔记本
	set n to item 1 of (search note in notebook nbk text "For example, when children receive low grades") # 获取笔记本里包含特定内容的指定卡片
	
	set x to excerpt text of n # 使用1个临时变量x储存指定卡片的标题
	set title of n to x # 将指定卡片标题设置为摘录的内容
	set excerpt text of n to "" # 清空指定卡片的摘录文本
	
end tell

:man_technologist: 关于插件和Applescript的选择

只是一个建议:

  • :robot: 如果有编程基础,喜欢大批量处理,或者自己有一些特殊需求就使用Applescript;
  • :memo: 如果是轻量场景,比如边读边自动转换,使用插件也可以很好胜任日常学习。
2 个赞

实在太棒了!我的那段代码就是参考两年前您发那篇《Test out AppleScript on MarginNote》写的哈哈,献丑了!十分十分感谢!