0

I'm having trouble creating an applescript to do a "save as html" on the front-most TextEdit document, and save it to the same directory (same name, just add an "html" extension....which I assume would be default behavior anyway).

My understanding is that it ought to use textutil to do this.

For extra polish and gratitude: could it check to see if a file with same name and extension already exists in that directory, and, if so, automatically renames the saved HTML file ala "xxxx2.html"?

Jimmbo
  • 35
  • 1
  • 6
  • What have you tried? Where are you having trouble? We can't help very well without a problem. – LoveAndCoding Mar 09 '13 at 19:50
  • I'm not looking so much for help debugging my several miserably failed applescripts as with being shown a way to do it that would be successful. It wouldn't amount to "fish-giving rather than teaching-to-fish", because I'd surely learn what I need to learn by studying a successful script. – Jimmbo Mar 09 '13 at 20:18

1 Answers1

0

This would save ~/Desktop/test.rtf as ~/Desktop/test.html, overwriting the HTML file if it already exists:

tell document 1 of application "TextEdit"
    save
    path
end tell
do shell script "textutil -convert html " & quoted form of result

Another approach using UI scripting (tested only in 10.8):

tell application "System Events" to tell process "TextEdit"
    click menu item "Save As…" of menu 1 of menu bar item "File" of menu bar 1
    tell sheet 1 of window 1
        tell pop up button 1 of group 1 of group 1
            click
            click menu item "Web Page (.html)" of menu 1
        end tell
        click button "Save"
    end tell
end tell
Lri
  • 26,768
  • 8
  • 84
  • 82
  • That textedit shortcut doesn't seem to work in OS 10.6.8. And while I can easily assign that scroll menu choice a keyboard shortcut (via Keyboards pref panel), it is very resistant to Automator, Quickeys, and apple scripting. Thanks for the Applescript, looks good. – Jimmbo Mar 10 '13 at 01:49