How can I execute AppleScript from Textedit?

You can't execute AppleScript directly from TextEdit. Apple provides an AppleScript editor though in /Applications/Utilities/Script\ Editor.app which makes it easy to create and run AppleScript code.

Just navigate to /Applications/Utilities in Finder, run Script Editor and copy/paste your code into it.


If this is something you find yourself needing to do fairly often (and, for some reason, don't wish to have to use Script Editor), then one way to do this is to create a systemwide Service (or Quick Action) in Automator:

Run As AppleScript Service

It will be set to receive text in any application and ensure that the Output replaces selected text.

Once saved, you can select the text in TextEdit (or, indeed, any application), and have it execute the AppleScript in situ, returning the result, which will appear below the selection.

AppleScript code:

on run {{input}}
    local input

    try
        return {input, run script input}
    on error E
        return {input, E}
    end try
end run