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

9.23 ver1

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

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

我们分别实现各个部分:

-- 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 oddif i is equal to 1 thentell application "System Events"keystroke "m" using command downdelay 0.1keystroke piecedelay 0.1keystroke returndelay 0.1end tellset i to 0elsetell application "System Events"keystroke piecedelay 0.1end tellset i to 1end if
end repeat
问题处理
keystroke反序bug

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

tell application "System Events"keystroke " "delay 0.1
end tell
安全性问题

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

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

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

之前提到的奇偶性问题

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

最终我们的代码是:

-- 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 oddif length of piece is not equal to 0 thenif i is equal to 1 thentell application "System Events"keystroke "m" using command downdelay 0.1keystroke piecedelay 0.1keystroke returndelay 0.1end tellset i to 0set j to 1elseif j is equal to 0 thentell application "System Events"-- delete the added spacekeystroke (ASCII character 8)delay 0.1end tellend iftell application "System Events"keystroke piecedelay 0.1end tellset i to 1set j to 1end ifend if
end repeat

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

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

来自9.23凌晨的灵感

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

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

-- 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 oddif length of piece is not equal to 0 thenif i is equal to 1 thentell application "System Events"keystroke "m" using command downdelay 0.1set the clipboard to piecekeystroke "v" using command downdelay 0.1keystroke returndelay 0.1end tellset i to 0set j to 1elseif j is equal to 0 thentell application "System Events"-- delete the added spacekeystroke (ASCII character 8)delay 0.1end tellend iftell application "System Events"set the clipboard to piecekeystroke "v" using command downdelay 0.1end tellset i to 1set j to 1end ifend if
end repeat

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/142745.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

华为OD七日集训第6期 十一特辑 - 按算法分类,由易到难,循序渐进,玩转OD

目录 专栏导读华为OD机试算法题太多了,知识点繁杂,如何刷题更有效率呢? 一、逻辑分析二、数据结构1、线性表① 数组② 双指针 2、map与list3、优先队列4、滑动窗口5、二叉树6、并查集7、栈 三、算法1、基础算法① 贪心算法② 二分查找③ 分治…

大咖共探AGI时代机遇,腾讯云助力大模型规模化应用提速

引言 2023 年,科技圈的“顶流”莫过于大模型。自 ChatGPT 的问世拉开大模型与生成式 AI 产业的发展序幕后,国内大模型快速跟进,已完成从技术到产品、再到商业的阶段跨越,并深入垂直行业领域。 新技术的爆发,催生新的应…

mdobus ASCII转CAN OPEN JAE1939协议网关

Modbus RTU协议转换网关是一种常见的设备,用于将Modbus RTU协议转换为其他通信协议。而CANopen是一种基于CAN总线的通信协议,主要用于工业自动化和控制系统中。本文将介绍Modbus RTU协议转换网关如何支持CANopen协议,以及该功能的应用场景和优…

洗地机哪个牌子好用又实惠?口碑最好的洗地机推荐

智能技术飞速发展的时代,扫地机器人这类智能家电其实也在顺应潮流和用户需求,不断更新迭代。暂且不说市面上现有多少个洗地机品牌,单单一个洗地机品牌旗下,其实每年都会有多个系列的新品亮相,我们面对的选择多了&#…

javaee之黑马乐优商城6

商品品牌的查询 上面就是我们需要根据分类id去找品牌 假设我们现在拿到的是 商品的分类id,我们需要根据分类id查询出对应的品牌即可 下面我们拿到上面的接口,直接撸代码 这个是和品牌相关联的操作,因为先去看一下BrandMapper,这个mapper是…

OpenCV显示10bit Raw数据

参考&#xff1a;10 12 14bit图像存储格式&#xff0c;利用Opencv显示10bit Raw数据,并根据鼠标的移动显示对应位置的灰度值。其他bit位数的Raw数据方法类似。 代码实现&#xff1a; #include<opencv2/opencv.hpp> #include<iostream> #include<opencv/highgu…

Qt扩展-QCustomPlot 简介及配置

QCustomPlot 简介及配置 一、概述二、安装教程三、帮助文档的集成 一、概述 QCustomPlot是一个用于绘图和数据可视化的Qt 控件。它没有进一步的依赖关系&#xff0c;并且有良好的文档记录。这个绘图库专注于制作好看的、发布质量的2D绘图、图形和图表&#xff0c;以及为实时可…

中间相遇法(分治类问题非等大分治的平衡做法)

分治&#xff0c;如果分成两半大小不一样&#xff0c;很容易被卡到 O ( n 2 ) O(n^2) O(n2) 在某些题目中&#xff0c;利用中间相遇法&#xff0c;我们可以优化这个过程 其优化的前提是分治的大头在找分界点 复杂度不用证&#xff0c;很好理解吧 这层找地越久&#xff0c;下…

一维卷积神经网络

假设输入数据维度为8&#xff0c;filter维度为5&#xff1b; 不加padding时&#xff0c;输出维度为4&#xff0c;如果filter的数量为16&#xff0c;那么输出数据的shape就是4*16. 一维卷积不代表卷积核只有一维&#xff0c;也不代表被卷积的feature也是一维。一维的意思是说卷…

regexp 应用

今天同事拿出个小栗子 1 如果用like的话 1,22 的情况会被字符串2匹配到这样会有问题 这里需要用concat将uids处理下 比如第一条处理成&#xff0c;1,2&#xff0c;3&#xff0c; 的形式 去模糊匹配 ‘%,1,%’ 当然like这种模糊匹配不太建议使用 2 regexp 用法 单个值 &#x…

MySQL作业1

目录 一.创建一张表&#xff0c;包含以下所有数据类型 建表&#xff1a;​编辑 二.使用以下六种约束 1.非空约束 2.唯一约束 3.主键约束 4.外键约束 5.检查约束 6.默认值约束 一.创建一张表&#xff0c;包含以下所有数据类型 Text 类型&#xff1a; Number 类型&#…

2023-9-26 JZ 复杂链表的复制

题目链接&#xff1a;复杂链表的复制 import java.util.*; /* public class RandomListNode {int label;RandomListNode next null;RandomListNode random null;RandomListNode(int label) {this.label label;} } */ public class Solution {public RandomListNode Clone(Ra…

【ComfyUI】Pytorch预训练模型(torch.hub)缓存地址修改

序言 最近玩ComfyUI时&#xff0c;每次生成图片&#xff0c;总是会下载一些东西&#xff0c;时间长了&#xff0c;C盘就不够用了&#xff0c;今天清理C盘发现&#xff0c;总是会在C:\Users\yutao\.cache\torch\hub\checkpoints这个路径下&#xff0c;下载大模型文件&#xff0…

适合零基础小白学的 Python 教程,视频或者书籍都可以?

Python 有很多衍生方向&#xff0c;比如 web 开发、网络爬虫、数据分析、数据挖掘、机器学习、人工智能等等&#xff0c;就业范围是很广的&#xff0c;Python 相较于别的编程语言对小白入门还是很友好的&#xff0c;Python 入门推荐这份书籍&#xff1a;PYTHON全案例实践 【PD…

6.wifi开发【智能家居:下】,正式开发:智能开关灯,智能采集温湿度,智能调彩灯

一。WEB Server开发 1.需求分析 用户通过页面操作插座彩灯温湿度 【开发前端1】&#xff1a;智能插座网页设计 智能插座网页设计需求 1.通过浏览器访问ESP8266 webserver 2.显示“创客学院-WiFi-智能家居” 3.显示“智能插座” 4.显示当前插座工作状态 5.按键触发插座动作 2.…

新手必看:Android studio 侧边栏实现,带源码

文章目录 前言效果图正文toolbar 用于定义应用程序的导航栏app_bardrawer_layout 用于创建侧边栏导航nav_header_draw app:menu"menu/activity_main_drawer" 前言 本篇内容主要是自己实现侧边栏后的一些总结&#xff0c;部分理论来着网络和ai助手&#xff0c;如有错…

根据文章段落内容自动插入图片php版

每篇内容根据段落判断插入图片代码附上&#xff1a; $chatd"<table>";if(stripos($content,$chatd)0){//随机输出三张图功能if($moduleid!37 &&$thumb){//判断是否存在图$idrand(1,999999);$midrand(1,9999999);$getimg"http://www.nongpin88.co…

uniapp项目实践总结(二十三)网页和小程序应用打包教程

导语&#xff1a;当你的应用程序开发完成后&#xff0c;在发布到互联网之前&#xff0c;需要进行打包操作&#xff0c;包括网页端、小程序端的打包。 目录 准备工作网页打包小程序打包 准备工作 在打包之前&#xff0c;请保证你的 uniapp 应用程序编译到网页、小程序是可以正…

2023国庆后前端面试应该准备什么?

前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;前端面试题库 表妹一键制作自己的五星红旗国庆头像&#xff0c;超好看 本篇文章会持续更新&#xff0c;也会同步到公众号前端面试官&#xff0c;方便大家随时随地学习…

聊聊并发编程——多线程之synchronized

目录 一.多线程下数据不一致问题 二.锁和synchronized 2.1 并发编程三大特性 2.2引入锁概念 三.synchronized的锁实现原理 3.1 monitorenter和monitorexit 3.2synchronized 锁的升级 3.2.1偏向锁的获取和撤销 3.2.2轻量级锁的加锁和解锁 自适应自旋锁 轻量级锁的解锁…