Search code examples
applescript

Reading file in AppleScript


I'm trying to read an html file into a variable in AppleScript, I have the following code.

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

Now I get an error like:

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

What am I doing wrong? I followed this example. Are HTML files not recognized as text?


Solution

  • You have to convert the Finder file objects to aliases or text.

    read can be used without separate open or close commands. It reads files as MacRoman without as «class utf8» though. (as Unicode text is 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