0

AppleScript 新手在这里....

尝试设置一个applescript,它将替换.htm文件中的几行代码

这很丑陋,但有效:

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

用测试文件试试这个:

set myFiles to (choose file with multiple selections allowed)
repeat with aFile in myFiles
    set myData to do shell script "cat " & quoted form of (aFile's POSIX path)
    set newData to do shell script "echo " & quoted form of myData & " | sed 's/BORDER=1/BORDER=0/g' > " & quoted form of (aFile's POSIX path)
end repeat
于 2013-01-19T03:44:15.090 回答
1

首先,您可以在不模拟键盘事件但直接与应用程序通信的情况下编写 TextEdit 脚本。

您可以打开一个文件,将文本转换为字符串,处理字符串,再次将字符串作为打开文件的内容并保存。

从 AppleScript 编辑器打开 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

它需要字符串s搜索f替换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 回答