简体   繁体   中英

Need an Applescript making textutil save frontmost TextEdit doc as HTML

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM