MacOS – Triggering an Automator script upon text selection

automatormacos

Is it possible to trigger an Automator script upon text selection on Mac OS X? I.e. if I select some text in any application (e.g. Safari, Eclipse, etc.), it should trigger an Automator script.

I'm open to suggestions using other languages / automation programs.

Best Answer

If the applications are scriptable like safari is, then you can possibly have a Applescript application running and checking if there is a selection. Then respond by running you task or calling the Automator workflow when there is.

Afaik Applications do not send out notifications when text is selected so this would have to work using a periodic check. Which means wthere ould be a delay in when you Applescript Application reacts

Example of Applescript App. (save as Application with stay open checked)

    property oldSelectedText : ""


on idle
    try
        tell application "Safari"

            set selectedText to (do JavaScript "(''+getSelection())" in document 1)
            if selectedText is not "" then
                if oldSelectedText is not equal to selectedText then

                    (* DO STUFF*)

                    set oldSelectedText to selectedText
                    display notification with title "Safari Selection" subtitle selectedText

                end if
            end if
        end tell

    end try

    return 5 -- seconds idle before next check
end idle