Applescript create URL from text

If you wrap the link in HTML you can then convert that to RTF and paste into an OO cell by shelling out to textutil,

See, for example:

or by using the NSAttributedString interface:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

-- rtfFromHTML :: String -> Either String String
on rtfFromHTML(strHTML)
    set ca to current application
    set s to ca's NSString's stringWithString:strHTML
    set d to (s)'s dataUsingEncoding:(ca's NSUTF8StringEncoding)
    
    set attStr to ca's NSAttributedString's alloc()'s initWithHTML:d documentAttributes:(missing value)
    if attStr is missing value then
        |Left|("String could not be parsed as HTML")
    else
        set {rtfData, err} to attStr's ¬
            dataFromRange:{location:0, |length|:attStr's |length|()} ¬
                documentAttributes:{DocumentType:"NSRTF"} ¬
                |error|:(reference)
        
        if (missing value = rtfData) or (missing value is not err) then
            |Left|(err's localizedDescription() as text)
        else
            |Right|((ca's NSString's alloc()'s ¬
                initWithData:rtfData encoding:(ca's NSUTF8StringEncoding)) as text)
        end if
    end if
end rtfFromHTML

-- Left :: a -> Either a b
on |Left|(x)
    {type:"Either", |Left|:x, |Right|:missing value}
end |Left|

-- Right :: b -> Either a b
on |Right|(x)
    {type:"Either", |Left|:missing value, |Right|:x}
end |Right|

-- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
on bindLR(m, mf)
    if missing value is not |Right| of m then
        mReturn(mf)'s |λ|(|Right| of m)
    else
        m
    end if
end bindLR

or, using JS:

// rtfFromHTML :: String -> Either String String
const rtfFromHTML = strHTML => {
    const
        as = $.NSAttributedString.alloc
        .initWithHTMLDocumentAttributes($(strHTML)
            .dataUsingEncoding($.NSUTF8StringEncoding), 0
        );
    return bindLR(
        typeof as
        .dataFromRangeDocumentAttributesError !== 'function' ? (
            Left('String could not be parsed as HTML')
        ) : Right(as),

        // Function bound if Right value obtained above:
        htmlAS => {
            let error = $();
            const rtfData = htmlAS
                .dataFromRangeDocumentAttributesError({
                        'location': 0,
                        'length': htmlAS.length
                    }, {
                        DocumentType: 'NSRTF'
                    },
                    error
                );
            return Boolean(ObjC.unwrap(rtfData) && !error.code) ? Right(
                ObjC.unwrap($.NSString.alloc.initWithDataEncoding(
                    rtfData,
                    $.NSUTF8StringEncoding
                ))
            ) : Left(ObjC.unwrap(error.localizedDescription));
        }
    );
};

// Left :: a -> Either a b
const Left = x => ({
    type: 'Either',
    Left: x
});

// Right :: b -> Either a b
const Right = x => ({
    type: 'Either',
    Right: x
});

// bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
const bindLR = (m, mf) =>
    undefined !== m.Left ? (
        m
    ) : mf(m.Right);
2 Likes