在AppleScript中读取文件

时间:2013-04-01 20:27:22

标签: 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 tell

现在我收到如下错误:

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

我做错了什么?我跟着this示例。 HTML文件是否未被识别为文本?

4 个答案:

答案 0 :(得分:3)

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

无需单独的打开或关闭命令即可使用

read。它会将文件读取为没有as «class utf8»的MacRoman。 (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

答案 1 :(得分: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

答案 2 :(得分: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。您还可以将“as Unicode text”用作UTF16。我个人喜欢这个,因为它是vanilla AppleScript,不需要shell脚本。

答案 3 :(得分:1)

使用open进行访问实际上是很难实现的。

如果您想使用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阅读器。