0

AppleScript newb here....

および .htm ファイルから数行のコードを置き換える AppleScript をセットアップしようとしています

これは醜いですが、動作します:

tell application "Finder"
  activate
          tell application "System Events"
  key down {command}
  keystroke "a"
  key up {command}
          end tell
end tell

tell application "Finder"
  activate
          set fileCount to count files in front window
          set fileCountAlert to fileCount
end tell
tell application "System Events"
          tell process "Finder"
                    tell menu bar 1
                              tell menu bar item "File"
                                        tell menu "File"
                                                  tell menu item "Open With"
                                                            tell menu "Open With"
                                                                      click menu item "TextEdit"
                                                            end tell
                                                  end tell
                                        end tell
                              end tell
                    end tell

          end tell
  delay 3
end tell

repeat until fileCount = 0

          tell application "TextEdit"
  activate
                    tell application "System Events"

  key down {command}
  keystroke "f"
  key up {command}
  --delay 1
                              keystroke "BORDER=1"
                              delay 0.5
  keystroke tab
  keystroke tab

                              keystroke "BORDER=0"
                              delay 0.5
  key down {command}
  keystroke "w"
  key up {command}
                              delay 0.5
                    end tell

          end tell

          set fileCount to (fileCount - 1)
end repeat

tell application "Finder"
          display dialog "Completed Adjusting " & fileCountAlert & " files."
end tell

私もこのようなものを使ってみました....しかし、これはまったく機能しませんでした....

set the search_document to (choose file)
replaceText("BORDER=1", "BORDER=0", search_document)

on replaceText(search_string, replacement_text, this_document)
          tell application "TextEdit"
  open this_document
                    set AppleScript's text item delimiters to the search_string
                    set this_text to the text of the front document as list
                    set AppleScript's text item delimiters to the replacement_text
                    set the text of the front document to (this_text as string)
  close this_document saving yes
          end tell
end replaceText

最後に...次のようなものを使用して、applescriptでボタンをクリックする方法を見つけようとしました:

      tell application "System Events"
                    tell process "TextEdit"
  click button "All" of scroll area of window "3.htm"
                    end tell
          end tell

**しかし - ここでエラーが発生し続けます。UIElementInspector アプリを使用していますが、うまくいきません。

誰かが簡単なヘルプの提案を持っているかどうか疑問に思っていますか? **

4

2 に答える 2

1

まず、キーボード イベントをエミュレートせずに TextEdit のスクリプトを作成できますが、アプリケーションと直接通信できます

ファイルを開き、テキストを文字列に取得し、文字列を操作し、開いたファイルのコンテンツとして文字列を再度配置して保存できます。

AppleScript Editor から TextEdit 辞書を開いて、利用可能なものを確認します。

しかし、あなたがする必要があること(ファイルを開く、変更を加える、保存する)には、 TextEdit さえ必要ありません

ファイルをスクリプトに直接ロードし、編集して保存できます。そして、それははるかに高速になります:

ファイルへのパスをtheFileに設定し、ファイルを文字列で開きます

set fref to a reference to file theFile
set theText to read fref as string

theTextにコンテンツを処理する準備ができました。

AppleScript で文字列を扱うチュートリアルはたくさんあります。グーグルだけ...

一部のテキストを置き換えるだけで、その関数を使用すると仮定します

on Substitute(s, f, r)
    if f = "" then return s
    set AppleScript's text item delimiters to f
    set theTextItems to text items of s
    set AppleScript's text item delimiters to r
    set s to theTextItems as string
    set AppleScript's text item delimiters to ""
    return s
end Substitute

fを文字列sで検索すると、 rで置換すると、結果の文字列が返されます。

最後に、テキストを保存します。

theFileをファイルのファイル パスに再度設定します (または元のままにします)。

ファイルが存在し、書き込み可能な場合、黙って上書きされることに注意してください。

set fref to a reference to file theFile
open for access fref with write permission
set eof of fref to 0
write theText to fref starting at eof as string
close access fref

最後の注意:ファイルへのパスtheFileはApplescript の方法で指定する必要があります

例えば

set theFile to "MyMacHD:Users:Paolo:Desktop:myTextFile.txt"
于 2013-01-20T21:07:12.553 に答える