阿星的空间

使用 apple script 实现逐行粘贴的功能

前言

一直以来有这样一个需求在我脑海里徘徊,然而我没有去深究,今天忍不住还是给它实现掉吧。

枯燥的事儿

是这样的,在做开发的时候,特别是测试功能的时候,比如重复注册很多个用户账号,经常需要重复复制->粘贴->提交->再复制->再粘贴->再提交,期间还涉及多个页面切换去复制文字,真是让人非常烦躁。

让复杂的事简单化

换个思路,这事儿可以这么解决:一次性复制大量的文本,然后一次粘贴只取文本中的一行

这样岂不是完美了。

脚本代码

话不多说,直接上 apple script 代码:

set oldClipText to (the clipboard as text)
set theList to paragraphs of oldClipText
if (count of theList) = 0 then
    return
end if
set s to the item 1 of theList
set the clipboard to s
delay 0.1
tell application "System Events" to keystroke "v" using command down
delay 0.1
if (count of theList) > 1 then
    set theList2 to text (paragraph 2) thru -1 of oldClipText
    set newClipText to theList2 as string
else
    set newClipText to ""
end if
set the clipboard to newClipText

简单的说,就是操作剪切板中的文本,每次调用脚本只粘一行,然后剩下的文本继续放回剪切板。

后语

配合神器better touch tool,将上述脚本绑定成快捷键,比如FN + v,即可实现一次复制大量文本,每次粘贴只粘一行,酷吧:)

原文来自阿星的空间:https://wanyaxing.com/blog/20190128193741.html

X