Question

I want AppleScript to loop through a set of of RTF files in folder and save them as HTML files.

This is my simple code so far. The XXXX is where I'm struggling:

tell application "Finder"
    set source_folder to choose folder
    set aList to every file in source_folder
    repeat with i from 1 to number of items in aList
        tell application "TextEdit"
            set aFile to (item i of aList)
            save as aFile XXXXXXXXX
        end tell
    end repeat
end tell

I'm really new to this... any help much appreciated.

Was it helpful?

Solution

You don't need TextEdit for this. There is a command line program textutil which will do the job without all the opening and saving stuff required with TextEdit. We can fix your TextEdit script (it has a few errors) but try this first and let us know if it does the job for you. The html files will have the same name but with the html extension and will be located in source_folder. The ouput path can be changed in the code by using the "-output" switch of textutil. See "man textutil" if you want to look at everything it can do.

And a general question... what is a RTD file? Do you mean rtf or rtfd? Textutil will work with rtf/rtfd but not rtd, so I hope that isn't really your file type.

set source_folder to choose folder with prompt "Choose a source folder."
set output_folder to choose folder with prompt "Choose an output folder."

tell application "Finder"
    set theFiles to (files of entire contents of source_folder) as alias list
end tell

repeat with aFile in theFiles
    tell application "Finder"
        set fileName to name of aFile
        set fileExt to name extension of aFile
    end tell

    set outputPath to (output_folder as text) & text 1 thru -((count of fileExt) + 1) of fileName & "html"

    do shell script "/usr/bin/textutil -convert html -output " & quoted form of POSIX path of outputPath & space & quoted form of POSIX path of aFile
end repeat

You mention you are new to applescript, so I'll give you some general pointers you should keep in mind when writing applescript code.

  1. Avoid putting tell blocks of code inside each other. You have tell app TextEdit inside tell app Finder. That's bad. Doing this is a source of many conflicts because you are basically telling the Finder to tell TextEdit to do something. That's not good because commands can get confused and it's really hard to debug these kinds of issues. So keep your tell blocks separate.
  2. Avoid telling an application to perform a command that is not in its applescript dictionary. You should only tell an application to do commands that it knows and an application only knows about the commands in its dictionary. So for example, you are telling the Finder to "choose folder". The Finder does not know that command. That's an applescript command. So doing as you have done is another possible source of errors. In this case that's a simple command and it will work but in general avoid doing this.
  3. Regarding the Finder, you should avoid using it too much. The Finder is a major program on your computer and is often busy doing computer related stuff. As such it's best to only use it when necessary. As an example you can see in my code that I removed the "choose folder" and the repeat loop from the Finder. I purposely appended "as alias list" to the end of the Finder command to make the list of files usable outside of the Finder tell block of code. Of course use the Finder if needed but it's best to not use it if you don't need it.
  4. Use the applescript dictionary of your applications. As mentioned above, the dictionary lists all of the terms and the syntax that an application understands (granted the dictionaries are difficult to understand but you will get better at it the more you use them). Under the file menu of AppleScript Editor choose "Open dictionary" and a list of all the applications that understand applescript is shown. Choose an application from that to see its dictionary. So for example, you are trying to figure out TextEdit's "save as" command. You can usually get good direction from the dictionary so you should take a look at that. Use the search field to search!

So I hope that helps! Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top