使用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.mzph.cn/news/88564.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javaee之黑马乐优商城6

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

Vue3 动态组件 component:is= 失效

错误代码 用Vue3&#xff0c;组件无需注册&#xff0c;所以就会提示“注册了不不使用”的报错&#xff0c; 于是用了异步注册&#xff0c;甚至直接为了不报错就在下面使用3个组件&#xff0c;有异步加载&#xff0c;但还是实现不了预期效果 <script setup> import { re…

Linux内核启动流程-第二阶段rest_init函数

一. Linux内核启动 上一篇文章简单了解了 Linux内核启动第二阶段&#xff0c;涉及的 start_kernel函数。start_kernel 函数最后调用了 rest_init 函数&#xff0c;接下来简单看一下 rest_init 函数。 本文续上一篇文章的学习&#xff0c;地址如下&#xff1a; Linux内核启…

距离矢量路由协议RIP(含Cisco模拟器实验命令配置)

距离矢量路由协议RIP(含Cisco模拟器实验命令配置) 简介 距离矢量路由协议&#xff08;Routing Information Protocol, RIP&#xff09;是一种内部网关协议&#xff0c;它位于应用层&#xff0c;使用520 UDP端口。RIP基于距离矢量算法&#xff08;Bellham-Ford&#xff09;根据…

Ubuntu Kafka开机自启动服务

1、创建service文件 在/lib/systemd/system目录下创建kafka.service文件 [Unit] DescriptionApache Kafka Server Documentationhttp://kafka.apache.org/documentation.html Requireszookeeper.service[Service] Typesimple Environment"JAVA_HOME/usr/local/programs/j…

MSF的安装与使用教程,超详细,附安装包和密钥

MSF简介 Metasploit&#xff08;MSF&#xff09;是一个免费的、可下载的框架 它本身附带数百个已知软件漏洞&#xff0c;是一款专业级漏洞攻击工具。 当H.D. Moore在2003年发布Metasploit时&#xff0c;计算机安全状况也被永久性地改变了&#xff0c;仿佛一夜之间&#xff0…

MySQL进阶篇4——锁+InnoDB引擎+MySQL管理

锁 概述 保证并发访问数据库数据的一致性和有效性等。 全局锁-库锁 ​ 加锁后&#xff0c;整个数据库实例就处于只读状态&#xff0c;后续的DML语句&#xff0c;DDL语句&#xff0c;以及更新操作的事务提交语句都将会被阻塞。 典型使用场景&#xff1a; ​ 对全库做逻辑备…

PY32F003F18之DMA串口

PY32F003F18使用DMA串口&#xff0c;官方程序省FLASH&#xff0c;但不省内存。单片机内存够大&#xff0c;节省没意义&#xff0c;故做了修改&#xff0c;少用HAL库中的发送和接收&#xff0c;从里面抠出有用的部分&#xff0c;修修改改就可以了。 一、DMA串口初始化流程&…

人生第一个java项目 学生管理系统

开始编程 建类 开始主要部分 main()部分 方法部分

RocketMQ —消费进度管理

Apache RocketMQ 通过消费位点管理消费进度&#xff0c;本文为您介绍 Apache RocketMQ 的消费进度管理机制。 背景信息​ Apache RocketMQ 的生产者和消费者在进行消息收发时&#xff0c;必然会涉及以下场景&#xff0c;消息先生产后订阅或先订阅后生产。这两种场景下&#x…

【单调栈】496. 下一个更大元素 I

496. 下一个更大元素 I 解题思路 首先计算nums2的每一个元素的下一个比他大的元素&#xff0c;使用单调栈将上面的结果和nums2中的每一个元素组成映射map针对每一个Nums1的元素 查询map 记录map 的value class Solution {public int[] nextGreaterElement(int[] nums1, int[…

算法 N皇后问题-(递归回溯)

牛客网 BM59. 解题思路&#xff1a; 行列、斜叉不在一条直线上。 命令行为 row, 列为col, row 从0开始递归直到最后一行&#xff0c;列从0开始遍历&#xff0c;直到最后一列&#xff0c;中间每一步记录或清除位置状态&#xff0c;状态分为 m1[col] 1, m2[row-col] 1, m3[r…

R语言柱状图直方图 histogram

柱状图简介 柱状图也叫直方图&#xff0c;是展示连续性数值的分布状况。在x轴上将连续型数值分为一定数量的组&#xff0c;y轴显示对应值的频数。 R基本的柱状图 hist 我们用R自带的Orange数据来画图。 > head(Orange)Tree age circumference(圆周长) 1 1 118 …

国家开放大学的训练试题

仓储与配送管理 参考 试题 一、单项选择题&#xff08;每小题2分&#xff0c;共20分&#xff0c;将正确答案选项的字母填入题目的括号内&#xff09; 1.( )是保管人在接受仓储物后签发的表明一定数量的保管物已经交付仓储保管的法律文书。 A.保单 B.仓单 C.提单 D.发票 …

132.【MySQL_进阶篇】

MySQL_进阶 (一)、存储引擎1.MySQL体系结构(1).连接层(2).服务层(3).引擎层(4).存储层 2.存储引擎简介(1).查看某张表的数据引擎(2).展示此版本支持的所有存储引擎(3).创建表my_myisam,并指定MyIASM存储引擎(4).存储引擎示列 3.存储引擎 _ Innodb(1).Innodb 介绍(2).Innodb 特点…

Linux学习第20天:Linux按键输入驱动开发: 大道至简 量入为出

Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 中国文化博大精深&#xff0c;太极八卦&#xff0c;阴阳交合&#xff0c;变化无穷。在程序的开发中也是这样&#xff0c;数字0和1也是同样的道理。就本节来说&am…

vue3 无法使用pnpm安装依赖 或 Cannot find module preinstall.js

创建.npmrc文件在根目录 shamefully-hoisttrue auto-install-peerstrue strict-peer-dependenciesfalse删除 node_modules 和 pnpm-lock.yaml 文件 重新 pnpm i 就可以啦

【Pm4py第七讲】关于visualization

本节用于介绍pm4py中的可视化函数&#xff0c;包括可视化bpmn、petri、性能图谱、变迁系统等。 1.函数概述 本次主要介绍Pm4py中一些常见的可视化函数&#xff0c;总览如下表&#xff1a; 函数名说明view_alignments(log, aligned_traces[, format])可视化对齐方法 view_bpmn(…

Python学习之编写学生信息管理系统

实例要求&#xff1a;1.学生基本信息包括学号、姓名、性别、成绩&#xff1b;2.将学号设置为主键值&#xff0c;根据学号进行相应的“增、删、查、改”&#xff1b;实例分析&#xff1a;封装“增、删、查、改”功能函数&#xff0c;在main函数中调用这些函数即可&#xff1b;1.…

华为云云耀云服务器L实例评测 | 云服务器搭建自己的gitlab代码仓库手把手教学

&#x1f4cb; 前言 &#x1f5b1; 博客主页&#xff1a;在下马农的碎碎念&#x1f917; 欢迎关注&#x1f50e;点赞&#x1f44d;收藏⭐️留言&#x1f4dd;✍ 本文由在下马农原创&#xff0c;首发于CSDN&#x1f4c6; 首发时间&#xff1a;2023/09/26&#x1f4c5; 最近更新时…