2013-04-01 70 views
1

我想要将一个html文件读入AppleScript中的变量,我有以下代码。在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文件不被识别为文本?

回答

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 
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 
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 

正如劳里写道,你可以加上 “«类UTF8»” 读文件为UTF8。你也可以使用UTF16的“作为Unicode文本”。就我个人而言,我喜欢这个,因为它是香草AppleScript,不需要shell脚本。

+0

我仍然在'open for access'中得到同样的错误,它不能将文档文件“blablabla.html”输入到“class fsrf”类型中。 – noio

+1

我编辑了脚本。现在要求您选择一个文件夹,以确保folderPath正确。将文件路径转换为文本,然后将其显式引用为文件似乎可行。对不起,我不知道为什么在列表中使用别名不起作用,但可能是因为按定义(?)列表项不是别名。我添加了一个条件,它可以防止加载图片和其他二进制文件。我已经添加了一个函数processHtml来演示你可以用这个做什么。 – Mark

1

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