1

我正在尝试将 html 文件读入 AppleScript 中的变量,我有以下代码。

tell application "Finder"
    set theItems to every file of folder folderName
    repeat with theFile in theItems
        open for access theFile
        set fileContents to (read theFile)
    end repeat
end tell

现在我收到如下错误:

Finder got an error: Can’t make document file "index.html" of folder 
[...] of startup disk into type «class fsrf».

我究竟做错了什么?我按照这个例子。HTML 文件不被识别为文本吗?

4

4 回答 4

3

您必须将 Finder 文件对象转换为别名或文本。

read无需单独的打开或关闭命令即可使用。它以 MacRoman 的形式读取文件,但没有as «class utf8»。(as Unicode text是 UTF-16。)

tell application "Finder" to files of folder "HD:Users:lauri:Sites" as alias list
repeat with f in result
    read f as «class utf8»
end repeat
于 2013-04-01T21:53:24.460 回答
2

尝试:

tell application "Finder" to set theItems to every file of folder folderName
repeat with theFile in theItems
    set aFile to POSIX path of (theFile as text)
    set fileContents to do shell script "cat " & quoted form of aFile
end repeat
于 2013-04-01T21:05:40.373 回答
1

从您的原始代码开始,应该这样做:

set folderPath to choose folder
set someData to ""
tell application "Finder"
    set theItems to every file of folder folderPath as list
    repeat with theFile in theItems
        set theFilePath to theFile as text
        if characters -5 thru -1 of theFilePath as string is ".html" then
            set theFileHandle to (open for access file theFilePath)
            set fileContents to (read theFileHandle)
            -- for testing, call some function
            set someData to someData & return & processHtml(fileContents) of me
            close access theFileHandle
        end if
    end repeat
    -- do something with someData here
    return someData
end tell

on processHtml(theData)
    -- do something with theData here
    return theData
end processHtml

正如 Lauri 所写,您可以添加“as «class utf8»”以将文件读取为 UTF8。您还可以将“作为 Unicode 文本”用于 UTF16。就个人而言,我喜欢这个,因为它是普通的 AppleScript,不需要 shell 脚本。

于 2013-04-02T10:32:47.720 回答
1

使用开放访问确实很难做到。

如果您想使用 AppleScript 读取 HTML 文件,那么最好的方法是使用 AppleScript 告诉 HTML 编辑器为您读取 HTML 文件。这是 AppleScript 工作的基本方式。这就是为什么“告诉”是最重要的命令。这就是为什么您只需 3 行就可以实现将 HTML 文件读入变量的目标:

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 阅读器。

于 2014-08-11T09:55:49.813 回答