Javascript AppleScript/JavaSript在Safari中突出显示匹配的文本
我有以下脚本,它将从剪贴板中获取一些值,分别返回它们,并在当前safari窗口中突出显示匹配的文本:Javascript AppleScript/JavaSript在Safari中突出显示匹配的文本,javascript,safari,applescript,Javascript,Safari,Applescript,我有以下脚本,它将从剪贴板中获取一些值,分别返回它们,并在当前safari窗口中突出显示匹配的文本: set myNewList to {} set myClipboard to the clipboard set theList to words of myClipboard ##set theLength to (get length of mylist) set ColortheList to {"009933", "000000", &quo
set myNewList to {}
set myClipboard to the clipboard
set theList to words of myClipboard
##set theLength to (get length of mylist)
set ColortheList to {"009933", "000000", "660066", "660099", "003399", "0033ff", "336666", "339966", "339999", "3399cc", "3399ff", "33cc66", "006633", "006666", "006699", "0066cc", "33ffff", "9933cc", "0066ff", "009966", "3366ff", "993300", "993333", "339900", "009999", "0099cc", "0099ff", "6600cc", "6600ff", "663300", "663333", "663366", "9900ff", "663399", "6633cc", "6633ff", "666699", "00cc00", "339933", "00cc33", "00cc66", "00cc99", "00cccc", "993366", "00ccff", "00ff00", "336699", "ff3333", "3366cc", "00ff33", "33ff00", "6666cc", "6666ff", "ff9999", "669966", "66ff99", "993399", "990066", "990099", "9900cc", "ff0099", "66ffcc", "33ff33", "00ff66", "ffcccc", "33cc99", "ff66cc", "33cccc", "33ccff", "00ff99", "00ffcc", "33ffcc", "00ffff", "330066", "330099", "3300cc", "ff9999", "3300ff", "33ff66", "990033", "33ff99", "333300", "333333", "3333ff", "336600", "660000", "660033", "66ffff", "990000", "9933ff", "996600", "996666", "996699", "9966cc", "9966ff"}
repeat with a from 1 to length of theList
copy ({item a of theList, some item of ColortheList}) to the end of the |myNewList|
end repeat
tell application "Safari"
## activate
set theWindow to front window
tell theWindow
tell current tab
repeat with colourPair in myNewList
do JavaScript "document.designMode = 'on'"
do JavaScript "var sel = window.getSelection(); sel.collapse(document.body, 0); while (window.find('" & (item 1 of colourPair) & "', true)) {document.execCommand('HiliteColor', false, '" & (item 2 of colourPair) & "');}"
do JavaScript "document.designMode = 'off'"
end repeat
end tell
end tell
end tell
这项工作很好,但速度很慢,特别是当我有10多个变量时。
最重要的是,如果我不小心按下键盘上的某个键,它会更改safari上加载的页面内容,如果我在运行时更改点击,它也会出错
它们是加快脚本速度和改进代码的一种方法吗?查看您发布的代码,下面的示例AppleScript代码就是我如何重写您的代码以加快代码速度的:
将myNewList设置为{}
将列表设置为(剪贴板)的单词
将AppleScript的文本项分隔符设置为换行符
将tempList设置为字符串形式的列表
将AppleScript的文本项分隔符设置为“”
将SorteDuniqList设置为以下段落:
(执行shell脚本“sort-u虽然@user3439894提供的答案肯定比OP在问题中的代码更有效,但他的解决方案可以更优化
我从这个资源中学到的解决方案中的优化
下面的摘录,直接取自我上面提到的资源,是我复制到剪贴板的内容,将在我的优化代码中处理
理论上,无论列表有多长,查找时间都应该是
每次都一样,但当我们看结果时,我们看到
获取列表项所需的时间每次增加一倍
将列表的大小增加一倍—换句话说,是线性增加,或者O(n)
例如,如果我们写一个脚本来循环
如果列表中的项目数量可变,我们将看到所需的时间
与列表中的项目数量相比,以二次方式增加,或
哎哟!如果你有,比如说,
数千个项目要通过,甚至可能成为主要项目
代码中的性能瓶颈。幸运的是,存在
“解决方案”是指诱使AppleScript列表执行
固定时间查找。如果将列表包装在脚本对象中
属性并在那里引用它,以AppleScript的方式
引用工作导致解释器的任何部分
导致绕过减速
下图显示了从剪贴板处理183个单词的结果。左图是我的优化技术的结果,右图显示了“接受答案”的结果
处理剪贴板上的183个单词显示出两者之间13秒的时间差。这不是很大的差异,但看看当剪贴板包含414个单词时会发生什么。哎哟
通常,要处理的列表越大,右侧图像中的代码效率就越低
(这是直接摘自我上面提到的资源的摘录的继续。)
由于我使用了user3439894的代码进行构建,我认为他应该因为“接受的答案”而获得荣誉。
无论哪种方式,我发布的方法都要快得多
您还可以考虑添加其他代码来删除剪贴板中的任何重复的单词,然后将这些条目传递给要处理的脚本的其余部分
。非常感谢,这绝对是更快和更清晰地阅读。@凯文,我已经更新了代码,使列表排序和UNIQ,避免处理任何重复的WO。可能在剪贴板上的rds,从而使其在否定重复单词时更快。
set startTime to current date
set theList to words of (the clipboard)
log ((count of theList) as text) & " Words From Clipboard Processed"
set ColortheList to {"009933", "000000", "660066", "660099", "003399", "0033ff", "336666", "339966", "339999", "3399cc", "3399ff", "33cc66", "006633", "006666", "006699", "0066cc", "33ffff", "9933cc", "0066ff", "009966", "3366ff", "993300", "993333", "339900", "009999", "0099cc", "0099ff", "6600cc", "6600ff", "663300", "663333", "663366", "9900ff", "663399", "6633cc", "6633ff", "666699", "00cc00", "339933", "00cc33", "00cc66", "00cc99", "00cccc", "993366", "00ccff", "00ff00", "336699", "ff3333", "3366cc", "00ff33", "33ff00", "6666cc", "6666ff", "ff9999", "669966", "66ff99", "993399", "990066", "990099", "9900cc", "ff0099", "66ffcc", "33ff33", "00ff66", "ffcccc", "33cc99", "ff66cc", "33cccc", "33ccff", "00ff99", "00ffcc", "33ffcc", "00ffff", "330066", "330099", "3300cc", "ff9999", "3300ff", "33ff66", "990033", "33ff99", "333300", "333333", "3333ff", "336600", "660000", "660033", "66ffff", "990000", "9933ff", "996600", "996666", "996699", "9966cc", "9966ff"}
colorWords(theList, ColortheList)
on colorWords(someList, ColortheList)
script fasterList
property aList : someList
property coloredList : ColortheList
property myNewList : {}
end script
repeat with anItem in fasterList's aList
copy {anItem, some item of fasterList's coloredList} to the end of fasterList's myNewList
end repeat
tell application "Safari"
tell current tab of front window
do JavaScript "document.designMode = 'on'"
repeat with colourPair in fasterList's myNewList
do JavaScript "var sel = window.getSelection(); sel.collapse(document.body, 0); while (window.find('" & (item 1 of colourPair) & "', true)) {document.execCommand('HiliteColor', false, '" & (item 2 of colourPair) & "');}"
end repeat
do JavaScript "document.designMode = 'off'"
end tell
end tell
end colorWords
set timeTaken to ((current date) - startTime as string) & " Seconds"