使用open进行访问确实是很难实现的。
如果要使用AppleScript读取HTML文件,那么最好的方法是使用AppleScript告诉HTML编辑器为您读取HTML文件。这是AppleScript工作的基本方式。这就是为什么“告诉”是最重要的命令。这就是为什么你可以完成的读取HTML文件到一个变量,在短短3行自己的目标:
tell application "BBEdit"
open (choose file)
set theHTMLSource to the text of document 1
close document 1
end tell
下面的脚本扩展了上述从所选文件夹中读取HTML文件的任意数量。它适用于BBEdit 9,并且还应该使用BBEdit的免费版本,该版本名为“TextWrangler”,可在Mac App Store中使用。或者你可以很容易地修改这个脚本,用于HyperEdit或TextEdit,或者任何你喜欢使用的支持AppleScript的HTML /文本编辑器。
tell application "Finder"
set theFolder to (choose folder)
set theFiles to every file of folder theFolder
set theHTMLSourceList to {}
repeat with theFile in theFiles
if the kind of theFile is equal to "HTML document" then
set theName to the name of theFile
tell application "BBEdit"
open file (theFile as text)
set theSource to the text of document 1
copy {theName, theSource} to the end of theHTMLSourceList
close document 1
end tell
end if
end repeat
end tell
当上面的脚本执行完毕,变量“theHTMLSourceList”填充了名字和HTML文件的整个文件夹的源代码,就像这样:
{{name of file 1, source of file 1}, {name of file 2, source of file 2}, {name of file 3, source of file 3}}
...依此类推,直到到任意数量的文件。但是,当然,您可以让脚本以您喜欢的任何方式将HTML源代码返回给您。关键在于支持AppleScript的HTML编辑器既可以读取HTML,也可以设置AppleScript变量,因此您不必在小型AppleScript中编写(调试和维护)自己的HTML阅读器。
我仍然在'open for access'中得到同样的错误,它不能将文档文件“blablabla.html”输入到“class fsrf”类型中。 – noio
我编辑了脚本。现在要求您选择一个文件夹,以确保folderPath正确。将文件路径转换为文本,然后将其显式引用为文件似乎可行。对不起,我不知道为什么在列表中使用别名不起作用,但可能是因为按定义(?)列表项不是别名。我添加了一个条件,它可以防止加载图片和其他二进制文件。我已经添加了一个函数processHtml来演示你可以用这个做什么。 – Mark