Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I am trying to get a URL via AppleScript, then displaying it as a clickable link via a window. How do I do this? (if I need to use Xcode for this, please add the answer, but provide detailed instructions, please)

share|improve this question
add comment

2 Answers

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).

share|improve this answer
add comment
tell application "Safari" to display dialog URL of document 1 as string

Or

set theUrl to "http://apple.stackexchange.com/posts/50397"
display dialog theUrl
share|improve this answer
    
See my updated question...the link needs to be clickable. –  daviesgeek May 3 '12 at 21:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.