AppleScript turns text into link from Safari
  • Hello,

    I have an AppleScript that I find indispensable in TextWrangler. It takes the highlighted text and turns it into a link to the frontmost page in Safari, complete with the appropriate title tag. here's the code:

    tell application "Safari"
    set t to name of window 1
    set s to URL of document 1
    end tell


    tell application "TextWrangler"
    set u to selection as text
    set selection to "" & u & ""
    end tell


    This script is based on one found here:

    http://www.lee-phillips.org/info/Macintosh/free.html

    How would I adapt this to MarsEdit? I tried replacing "TextWrangler" with "MarsEdit", but of course it wasn't that simple. I know I can open a MarsEdit post in TextWrangler with the appropriate File menu command, but I would prefer to remain within MarsEdit, if at all possible. The lack of this one script is the only thing keeping me from doing so!
  • Try this:


    tell application \"Safari\"
    set t to name of window 1
    set s to URL of document 1
    end tell

    tell application \"MarsEdit\"
    set u to selected text of document 1
    set selected text of document 1 to \"<a href=\\"\" & s & \"\\" title=\\"\" & t & \"\\">\" & u & \"</a>\"
    end tell


    MarsEdit has a slightly different terminology for identifying the selected text in the "current" document. I think that should do the trick for you.
  • Daniel,

    That worked perfectly! Thank you. I am not much of a scripter, so even these few changes were beyond me. But I bought MarsEdit today with the hope of finding a solution to this problem. I'm glad my faith paid off!

    In TextWrangler, I have this script assigned to keyboard shortcut Option-Command-Z. Using that same combo in MarsEdit causes the script not to run, but to open in AppleScript Editor. That seems to be a function of holding down the Option key while selecting a script, which I'm now finding is consistent behavior in other applications as well. I wonder why it works in TextWrangler?

    Regardless, I can set another shortcut, like Command-R, which works without issue.

    Thanks again!

    -Ken

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!