0

単純な Web フォームへの大量のデータ入力を必要とするソフトウェアを使用せざるを得ないため、入力としてテキスト ファイルを取得し、内容をキーストロークで Safari に出力するように AppleScript を設計しました。 Web フォームの次のフィールドに進むために使用されます。テキストファイルを読みやすくするために、テキストファイルの改行をタブとして扱うことができるように、文字列の置換を行っています。

クリップボードからキーストロークすることでこれを合理化したいのですが、これは機能しますが、文字列の置換が本来の方法で行われておらず、その理由を特定できません。この時点での私の回避策は、コピーする前に改行をタブで手動で検索/置換することですが、基本的にはファイルを保存してスクリプトをポイントするのと同じくらい遅くなります。

tell application "Safari"
    activate
end tell

set sourceString to (the clipboard) as text

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\n"
set the lineList to every text item of sourceString
set AppleScript's text item delimiters to "\t"
set keystrokeString to the lineList as string
set AppleScript's text item delimiters to ASTID

tell application "System Events"
    keystroke keystrokeString as text
end tell

\t\nこれを AppleScript Editor に入れると、不可視にコンパイルされます。次を使用してファイルから読み取ると、文字列編集 (中間) 部分が宣伝どおりに機能します。

set theFile to (choose file with prompt "Select a file to read:" of type {"txt"})
open for access theFile
set fileContents to (read theFile)
close access theFile

クリップボードからテキストを取得したときに文字列の置換が機能しない理由はありますか?

4

3 に答える 3

1

区切り文字を設定するときに ASCII 文字関数を使用すると機能するはずです。改行は ASCII 文字 10set AppleScript's text item delimiters to ASCII character 10 です。

于 2013-03-04T09:50:52.410 に答える
0

ページ上のフォームをタブで移動すると、エラーが発生しやすくなります。各フォームを ID で識別し、フォームに JavaScript を入力する必要があります。

tell application "Safari"
    set URL of document 1 to "http://www.google.com/"
    delay 3
    do JavaScript "document.getElementById('gbqfq').value = 'My Text'" in document 1
end tell
于 2013-03-04T12:16:57.480 に答える
0

テキストをクリップボードにコピーすると、改行がキャリッジ リターンに変換され、\n一致しないように見えます。

\rただし、両方ともコンパイルして見えない文字になるため、少し面倒です。

少しテストした後、テキスト ファイルをディスクに保存してから AppleScript で読み取る場合、ファイルで使用されている行末、LF または CR を\nまたはと一致させる必要があるようです\rただし、クリップボードにコピーされたテキストを読み取るときは、常に一致する必要があります\r

編集:

これらの文字の多くには、AppleScript キーワードがあります。tablinefeed、およびreturnすべてそのまま使用できます。私のスクリプトは次のようになります。

set backspace to ASCII character 8

get the clipboard
set keystrokeString to (replacement of return by tab for the result)
set keystrokeString to (replacement of linefeed by tab for the result)
set keystrokeString to (replacement of tab by tab & backspace for the result)

tell application "System Events"
    keystroke keystrokeString as text
end tell

on replacement of oldDelim by newDelim for sourceString
    set oldTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to oldDelim
    set strtoks to text items of sourceString
    set text item delimiters of AppleScript to newDelim
    set joinedString to strtoks as string
    set text item delimiters of AppleScript to oldTIDs
    joinedString
end replacement

また、各タブの後にヒットされるバックスペース文字も含まれるようになったため、既に入力されているフィールドにテキストが入力されていない場合は、先に進んだ後に削除されます。

于 2013-03-04T13:30:18.097 に答える