在AppleScript中读取文件

在AppleScript中读取文件,applescript,Applescript,我试图将一个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 tel

我试图将一个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文件不能识别为文本吗?

请尝试:

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
尝试:


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

read可以在没有单独的打开或关闭命令的情况下使用。但它以MacRoman的形式读取文件,而不以«类utf8»的形式读取文件。因为Unicode文本是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

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

read可以在没有单独的打开或关闭命令的情况下使用。但它以MacRoman的形式读取文件,而不以«类utf8»的形式读取文件。因为Unicode文本是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

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

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所写,您可以添加为«类utf8»以将文件读取为utf8。还可以将其用作UTF16的Unicode文本。就我个人而言,我喜欢这个,因为它是香草AppleScript,不需要shell脚本。

从原始代码开始,这应该可以做到:

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所写,您可以添加为«类utf8»以将文件读取为utf8。还可以将其用作UTF16的Unicode文本。就个人而言,我喜欢这个,因为它是香草AppleScript,不需要shell脚本。

使用openforaccess确实很难做到这一点

如果您想使用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应用商店中提供。或者,您可以非常轻松地调整此脚本,以便与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变量,因此您不必在tiny AppleScript中编写、调试和维护自己的HTML阅读器。

使用open for access真的很难做到这一点

如果您想使用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应用商店中提供。或者,您可以非常轻松地调整此脚本,以便与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变量,因此您不必在tiny AppleScript中编写、调试和维护自己的HTML阅读器。

我在open for access中仍然会遇到同样的错误,无法将文档文件bla.html类型转换为«class fsrf»类型。我已经编辑了脚本。现在要求您选择一个文件夹,以确保folderPath正确。将文件路径转换为文本,然后将其显式地引用为文件似乎是可行的。抱歉,我不知道为什么在列表中使用别名不起作用,但可能是因为列表项在定义上不是别名。我添加了一个条件,防止加载图片和其他二进制文件。我添加了一个函数processHtml来演示如何使用它。我仍然在open for access中遇到相同的错误,即无法将文档文件blabla.html类型转换为«class fsrf»类型。我已经编辑了脚本。现在要求您选择一个文件夹,以确保folderPath正确。将文件路径转换为文本,然后将其显式地引用为文件似乎是可行的。对不起,我不知道为什么在列表中使用别名不起作用,但也许 这是因为列表项在定义上不是别名。我添加了一个条件,防止加载图片和其他二进制文件。我添加了一个函数processHtml来演示如何使用它。