使用applescript自动化trilium的数学公式环境(二)

9.23 ver1

没想到今天很有精神,在玩chatgpt的时候突然想到,为什么不让他帮我写一份代码呢?说干就干。但是,可能是因为我的英语不怎么样,chatgpt生成出来的整个东西实在是菜的抠脚。所以我觉得还是应该自己先想好一个大致的任务流程,然后让chatgpt一步一步把它实现出来。

  1. 从剪贴板获取需要数学公式化的文本
  2. 使用分隔符"$"将该文本分割为若干部分,存储为一个数组
  3. 对数组的元素循环:第偶数个就直接粘贴到trilium里面,第奇数个则用ver0的工作流程插入到公式环境里面。(这里有个小问题,就是可能文件一开始就是公式,但是我们事实上可以先给他前面插一个无意义字符,后面再删掉,保证公式一定是奇数个;总之这个细节我们先不管)

我们分别实现各个部分:

applescript 复制代码
-- Part 1: get the text from clipboard
set paragraphText to the clipboard
tell application "Trilium Notes"
	activate
end tell


-- Part 2: delimiter paragraphText to oldDelimiters
set delimiter to "$"

-- Set the text item delimiters to the delimiter
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter

-- Split the paragraphText into pieces
set shellPieces to text items of paragraphText

-- Restore the original text item delimiters
set AppleScript's text item delimiters to oldDelimiters


-- Part 3 insert ino trilium
-- Loop through the paragraph pieces
set i to 0
repeat with piece in shellPieces
	-- even or odd
	if i is equal to 1 then
		tell application "System Events"
			keystroke "m" using command down
			delay 0.1
			keystroke piece
			delay 0.1
			keystroke return
			delay 0.1
		end tell
		set i to 0
	else
		tell application "System Events"
			keystroke piece
			delay 0.1
		end tell
		set i to 1
	end if
end repeat
问题处理
keystroke反序bug

实际使用的时候这个程序会出一些小bug,最明显的是,apple的keystroke似乎有点bug,第一个输入是反序的,比如keystroke "text"会输出"txet",所以我们这里让keystroke一开始直接输入一个空格来避免这个情况。同时,每次keystroke之间应该delay一会,等待系统反应过来,即在输出之前增加一段:

applescript 复制代码
tell application "System Events"
	keystroke " "
	delay 0.1
end tell
安全性问题

原本使用剪贴板是因为它无需输入,比较方便。但是在实际使用中发现会有一个问题就是我们的剪贴板里面可能不一定是我们想要的东西,所以最终还是决定改成输入模式:

applescript 复制代码
set paragraphText to text returned of (display dialog "Enter the text you want to input in trilium with automated math formula transformation:" default answer "")
提醒我们把输入法切换成英文

还有一个问题是我们使用的是脚本,所以系统输入法会影响我们的输入。我们应该把输入法切换成英文,防止被它调用。

之前提到的奇偶性问题

最开始我们在字符串前面加一个空格,最后再把空格删掉即可。

最终我们的代码是:

applescript 复制代码
-- Part 1: get the text from clipboard
set orgnparagraphText to text returned of (display dialog "Enter the text you want to input in trilium with automated math formula transformation; remember to change the keyboard into English:" default answer "")
set paragraphText to " " & orgnparagraphText
tell application "Trilium Notes"
	activate
end tell


-- Part 2: delimiter paragraphText to oldDelimiters
set delimiter to "$"

-- Set the text item delimiters to the delimiter
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter

-- Split the paragraphText into pieces
set shellPieces to text items of paragraphText

-- Restore the original text item delimiters
set AppleScript's text item delimiters to oldDelimiters


-- Part 3 insert ino trilium
-- Loop through the paragraph pieces
set i to 0
set j to 0
-- 这里插入一段是因为apple的keystroke似乎有点bug,第一个输入是反序的,所以我们这里输入一个空格来避免这个情况。同时,每次keystroke之间应该delay一会,等待系统反应过来。
tell application "System Events"
	keystroke " "
	delay 0.1
end tell
repeat with piece in shellPieces
	-- even or odd
	if length of piece is not equal to 0 then
		if i is equal to 1 then
			tell application "System Events"
				keystroke "m" using command down
				delay 0.1
				keystroke piece
				delay 0.1
				keystroke return
				delay 0.1
			end tell
			set i to 0
			set j to 1
		else
			if j is equal to 0 then
				tell application "System Events"
					-- delete the added space
					keystroke (ASCII character 8)
					delay 0.1
				end tell
			end if
			tell application "System Events"
				keystroke piece
				delay 0.1
			end tell
			set i to 1
			set j to 1
		end if
	end if
end repeat

我们同样在系统设置中为这个功能添加一个快捷键。由于我的命名是"trilium日志快速导入",因此我们设置快捷键为ctrl+shift+l(log)

现在最后遗留的一个问题就是,我们的字符是用自动输入输入的,假如遇到中文字符可就原形毕露了。所以后面我们需要解决的问题是把这玩意弄成支持中文输入的。

来自9.23凌晨的灵感

卧槽,我突然悟了,只要像原来一样,把要输入的东西弄到剪贴板里面,然后粘贴到程序里面就行了。

询问chatgpt得知,只要用代码"set the clipboard to target string"就能实现这个功能!

applescript 复制代码
-- Part 1: get the text from clipboard
set orgnparagraphText to text returned of (display dialog "Enter the text you want to input in trilium with automated math formula transformation; remember to change the keyboard into English:" default answer "")
set paragraphText to " " & orgnparagraphText
tell application "Trilium Notes"
	activate
end tell


-- Part 2: delimiter paragraphText to oldDelimiters
set delimiter to "$"

-- Set the text item delimiters to the delimiter
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter

-- Split the paragraphText into pieces
set shellPieces to text items of paragraphText

-- Restore the original text item delimiters
set AppleScript's text item delimiters to oldDelimiters


-- Part 3 insert ino trilium
-- Loop through the paragraph pieces
set i to 0
set j to 0
-- 这里插入一段是因为apple的keystroke似乎有点bug,第一个输入是反序的,所以我们这里输入一个空格来避免这个情况。同时,每次keystroke之间应该delay一会,等待系统反应过来。
tell application "System Events"
	keystroke " "
	delay 0.1
end tell
repeat with piece in shellPieces
	-- even or odd
	if length of piece is not equal to 0 then
		if i is equal to 1 then
			tell application "System Events"
				keystroke "m" using command down
				delay 0.1
				set the clipboard to piece
				keystroke "v" using command down
				delay 0.1
				keystroke return
				delay 0.1
			end tell
			set i to 0
			set j to 1
		else
			if j is equal to 0 then
				tell application "System Events"
					-- delete the added space
					keystroke (ASCII character 8)
					delay 0.1
				end tell
			end if
			tell application "System Events"
				set the clipboard to piece
				keystroke "v" using command down
				delay 0.1
			end tell
			set i to 1
			set j to 1
		end if
	end if
end repeat

那么,这个工具的设计就大功告成了,完结撒花!

相关推荐
0思必得01 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
芷栀夏1 小时前
CANN开源实战:基于DrissionPage构建企业级网页自动化与数据采集系统
运维·人工智能·开源·自动化·cann
鸽芷咕2 小时前
DrissionPage 成 CANN 仓库爆款自动化工具:背后原因何在?
运维·python·自动化·cann
池央2 小时前
CANN GE 深度解析:图编译器的核心优化策略、执行流调度与模型下沉技术原理
人工智能·ci/cd·自动化
深圳安锐科技有限公司3 小时前
斜拉桥、铁塔 4G 一体化索力计 工地快速加装方案怎么实施?
自动化·实时监测·自动化监测·桥梁监测·结构健康监测·索力计·索力监测仪
北京耐用通信4 小时前
破解AGV多协议互联难题:耐达讯自动化Profinet转Devicenet网关如何实现高效协同
人工智能·科技·物联网·网络协议·自动化·信息与通信
梦帮科技5 小时前
OpenClaw 桥接调用 Windows MCP:打造你的 AI 桌面自动化助手
人工智能·windows·自动化
feasibility.5 小时前
AI 编程助手进阶指南:从 Claude Code 到 OpenCode 的工程化经验总结
人工智能·经验分享·设计模式·自动化·agi·skills·opencode
xiaobaibai1536 小时前
营销自动化终极形态:AdAgent 自主闭环工作流全解析
大数据·人工智能·自动化
池央8 小时前
CANN 诊断工具链深度解析:oam-tools 的自动化故障信息收集、软硬件状态快照与 AI Core 错误溯源机制
运维·人工智能·自动化