How to use selected text in apple script? I want to add text before and after it Idea: I select text, and it gets replaced with &lt;sometag&gt;selected text&lt;/sometag&gt;.

Code:

on run {input, parameters}

tell application "System Events" to keystroke "&lt;tag&gt;[what should go here]&lt;/tag&gt;"

    return input
end run
Briefly, have your workflow accept Text from Any Application and check the box that is labelled something like Output Replaces Selection.

Then, all you need is a single Execute AppleScript action, in which you should replace any example code with this:

on run {input as text}
    return ["&lt;sometag&gt;", input, "&lt;/sometag&gt;"] as text
end run


which can also be written like this:

on run {input as text}
    return "&lt;sometag&gt;" &amp; input &amp; "&lt;/sometag&gt;"
end run


The workflow would need to be saved as a Service or Quick Action, and then be activated in System Preferences where other "Services" are listed.