AppleScript newb here ....
尝试设置一个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应用程序,但没有运气。
想知道是否有人有快速帮助建议?**
答案 0 :(得分: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
答案 1 :(得分:0)
FIRST ,您可以编写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
最后注意:必须以 Applescript方式指定文件 theFile 的路径
例如
set theFile to "MyMacHD:Users:Paolo:Desktop:myTextFile.txt"