noio
noio

Reputation: 5812

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?

Upvotes: 1

Views: 5138

Answers (4)

Simon White
Simon White

Reputation: 736

Using open for access is really doing it the hard way.

If you want to read an HTML file with AppleScript, then the best way to do that is to use AppleScript to tell an HTML editor to read the HTML file for you. That is the fundamental way that AppleScript works. That’s why “tell” is the most important command. That’s why you can accomplish your goal of reading an HTML file into a variable in just 3 lines:

tell application "BBEdit"
    open (choose file)
    set theHTMLSource to the text of document 1
    close document 1
end tell

The following script expands on the above to read an arbitrary number of HTML files from a chosen folder. It works with BBEdit 9, and should also work with BBEdit’s free version, which is called “TextWrangler” and is available in Mac App Store. Or you can fairly easily adapt this script for use with HyperEdit or TextEdit or whatever AppleScript-aware HTML/text editor you prefer to use.

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

When the above script is finished, the variable “theHTMLSourceList” is populated with the names and source code of the entire folder of HTML documents, like so:

{{name of file 1, source of file 1}, {name of file 2, source of file 2}, {name of file 3, source of file 3}}

… and so on up to an arbitrary number of files. But of course you can have the script return the HTML source to you in whatever way you like. The key point is that an AppleScript-aware HTML editor can both read HTML and set AppleScript variables, so you don’t have to write (and debug and maintain) your own HTML reader in tiny AppleScript.

Upvotes: 1

Mark
Mark

Reputation: 2435

Starting from your original code, this should do it:

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

As Lauri wrote, you can add "as «class utf8»" to read the file as UTF8. You could also use "as Unicode text" for UTF16. Personally, I like this, because it is vanilla AppleScript and doesn't need shell scripting.

Upvotes: 1

Lri
Lri

Reputation: 27603

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

Upvotes: 3

adayzdone
adayzdone

Reputation: 11238

Try:

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

Upvotes: 2

Related Questions