クリップボードから値を取得し、それらを個別に返し、現在のサファリウィンドウで一致するテキストを強調表示する次のスクリプトがあります。
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コードの例 は、コードを少し書き直す方法です。
set myNewList to {}
set theList to words of (the clipboard)
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 anItem in theList
copy {anItem, some item of ColortheList} to the end of the myNewList
end repeat
tell application "Safari"
tell current tab of front window
do JavaScript "document.designMode = 'on'"
repeat with colourPair in 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
注:
私はいくつかのタイミングテストを行いましたし、呼び出すことがないことにより、改善のビットがあったdo JavaScript "document.designMode = 'on'"
とdo JavaScript "document.designMode = 'off'"
それぞれの項目にリスト秒でrepeat
ループが。
また、私が持っているように最初のrepeat
ループを書くことは、リストの長さを計算する必要性を否定します。それは無関係であり、ほんの一瞬の時間を削ります。
このアイテムはスピードアップしませんでしたが、括弧や垂直パイプなどの不要な文字も削除しました。
私はまた、使用set theList to words of (the clipboard)
の二行の代わりにコードを使用すると、同じ結果を得るために使用される、set myClipboard to the clipboard
とset theList to words of myClipboard
だけの量減らすためにコードを書くために、それは量少なくなるようIMOそれは、ちょうど読みやすいですコードを読んで、あまりにも維持します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加