AppleScript to perform actions to text in new TextEdit document

applescripttextedit

I need to make an AppleScript that would create a new TextEdit file, type any random text, then a table would pop up and give you two options to choose font of that text, then same with colour, save the file on desktop and close TextEdit.
This is what I have so far, but I came to an end of my knowledge:

tell application "TextEdit"
    activate
    make new document with properties {text:"XDXDXD"}
    set theDesktopPath to the path to the desktop folder as text
    tell front document
        set font to "Comic Sans MS"
        set size to 40
        set its color to {65535, 0, 0}
    end tell
end tell

I'm not sure how to tell what isn't working from the script above. Can you help with the completion of the script?

Best Answer

This is what I've written based on what was posted as the sample. First I set the filename and the file path of the document to create:

set filename to "test.txt"
set filePath to path to desktop

I wrote a display dialog that prompts a linefeed with a timeout and vague validation:

try
    set enteredText to (display dialog "What is your text?" default answer linefeed with title scriptTitle giving up after 40)
    if button returned of result = "" or gave up of result = true then error number -128
    set enteredText to text returned of enteredText
on error
    return display notification "Script cancelled" with title scriptTitle
end try

After the entry of text I then tell TextEdit to manipulate the text based on what you've supplied and save it to a file on the desktop:

tell application "TextEdit"
    activate
    set theDoc to make new document with properties {text:enteredText}
    tell theDoc
        set the color of every word to {65535, 0, 0}
        set size to 29
        try
            set font of theDoc to "Comic Sans MS"
        on error
            set font of theDoc to "Times"
        end try
        save in file ((filePath as text) & filename)
    end tell
end tell

The script's entire code:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

(*
    Date: 18-12-20
    Developer: r2d2
    Purpose: prompt for text, manipulate in TextEdit and save to file.
    Version: 1.1
    Name: textedit_experiment.scpt
*)

set filename to "test.txt"
set filePath to path to desktop
set scriptTitle to "r2d2 TextEdit script"

try
    set enteredText to (display dialog "What is your text?" default answer linefeed with title scriptTitle giving up after 40)
    if button returned of result = "" or gave up of result = true then error number -128
    set enteredText to text returned of enteredText
on error
    return display notification "Script cancelled" with title scriptTitle
end try

tell application "TextEdit"
    activate
    set theDoc to make new document with properties {text:enteredText}
    tell theDoc
        set the color of every word to {65535, 0, 0}
        set size to 29
        try
            set font of theDoc to "Comic Sans MS"
        on error
            set font of theDoc to "Times"
        end try
        save in file ((filePath as text) & filename)
    end tell
end tell
return display notification "Script COMPLETED" with title scriptTitle

The script as it is is a base and there are far other forms of validation and improvements that can be made such as file existence, text returned testing or dialog testing but I wanted to answer the question.


Edit:

Code for the TextEdit tell modified to include closure of the document, text passed is hard coded as foobar:

tell application "TextEdit"
    activate
    set theDoc to make new document with properties {text:"foobar"}

    tell theDoc
        set the color of every word to {65535, 0, 0}
        set size to 29
        try
            set font of theDoc to "Comic Sans MS"
        on error
            set font of theDoc to "Times"
        end try
        save in file ((filePath as text) & filename)
    end tell

    set theDoc to front window
    try
        close theDoc
    on error
        display notification "Didn't close document"
    end try
end tell

Screenshot of dialog:

enter image description here

Screenshots of the above hard coded tell block and re-opened RTF file:

enter image description here

Code modified to take passed text to dialog and RTF opened:

enter image description here