How do I display a hyperlink with an AppleScript?

Solution 1:

AppleScript itself cannot display rich text (HTML) in a popup dialog. So your options are:

  1. Display a normal text dialog with AppleScript, showing the URL and asking the user if she wants to go there. If user clicks "OK", open that URL (that's exactly 1 click, so pretty much equivalent to a clickable link).

    -- tested with Safari 5.1.7 on Mac OS X 10.6.8
    set theUrl to "http://j.mp/LgHoEB"
    try
        display dialog theUrl & "\nClick OK to open this URL in Safari." with title "Open URL?" with icon caution
        if button returned of result is "OK" then
            tell application "Safari" to make new document with properties {URL:theUrl}
        end if
    on error number -128 -- user cancelled
        -- do something else
    end try
    

     

  2. Use the Safari AppleScript command do JavaScript to make a JavaScript popup with the desired URL as a clickable link (and possibly some more custom HTML):

    -- tested with Safari 5.1.7 on Mac OS X 10.6.8
    set theUrl to "http://j.mp/LgHoEB"
    set JSPopup to "(function() {" & ¬
        "var w = window.open('', 'Clickable link');" & ¬
        "w.document.write(" & ¬
        "'<html><body><p>" & ¬
        "<a href=\"" & theUrl & "\">" & theUrl & "</a>" & ¬
        "</p></body></html>'" & ¬
        ");})()"
    tell application "Safari"
        do JavaScript JSPopup in current tab of window 1
    end tell
    

    Of course, this will only work if your Safari allows popup windows (with my settings, for instance, a new tab is opened instead).

Solution 2:

Until now links cannot be made clickable in apple scripts but there can be a way around. For opening a single link, you can use like below,

set theAlertText to "Swiftlint is not installed"
set theAlertMessage to "Download from https://github.com/realm/SwiftLint manually. Would you like to open link?"
display alert theAlertText message theAlertMessage as critical buttons {"Cancel", "Open link"} default button "Open link" cancel button "Cancel" giving up after 60
set the button_pressed to the button returned of the result
if the button_pressed is "Open link" then
    open location "https://github.com/realm/SwiftLint/blob/master/README.md"
end if