2013-01-19 97 views
0

的AppleScript福利局这里....使用AppleScript通过文本编辑

试图建立一个AppleScript将替换的几行代码从和.htm文件

这是为了替换在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应用程序,但没有运气。

想知道如果任何人有ANNY快速帮助建议**

回答

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 
0

FIRST,你可以不模仿键盘事件,而是直接与通信脚本文本编辑应用。

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

从AppleScript Editor打开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[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"