Copy URL to clipboard Applescript

As a blogger, I’ve often wanted the URL from the current website I am browsing put onto the clipboard in HTML format so I can quickly paste it into a blog entry. I wrote a simple Applescript to share.

To use:

  • If you don’t have the AppleScript menu installed, install it by running the Install Script Menu application in /Applications/Applescript/
  • In your home directory, open Library. If there is not a Scripts folder, create one. Inside the Scripts folder, if there is no Safari folder, create one.
  • Open Script Editor in /Applications and paste the script at the end of this entry. Save the file to ~/Library/Scripts/Safari/ as any name you like, such as Copy To Clip

That’s it. Now you can be in safari, choose your script from the Safari Applescript menu item and have your URL in HTML format!

The Script:

Safari

tell application "Safari"
    tell document 1
        set the clipboard to "<a HREF=\"" & my escapeURL(url) & "\" target=\"_blank\"></a>"
    end tell
end tell
on escapeURL(aURL)
    set newURL to ""
    set savedDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"&"}
    
    set aCount to count text item of aURL
    repeat with index from 1 to (aCount - 1)
        set newURL to newURL & (text item index of aURL) & "&"
    end repeat
    set newURL to newURL & last text item of aURL
    
    set AppleScript's text item delimiters to savedDelimiters
    return newURL
end escapeURL

Omniweb

tell application "OmniWeb"
    tell browser 1
        set the clipboard to "<a HREF=\"" & my escapeURL(address) & "\" target=\"_blank\"></a>"
    end tell
end tell
on escapeURL(aURL)
    set newURL to ""
    set savedDelimiters to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"&"}
    
    set aCount to count text item of aURL
    repeat with index from 1 to (aCount - 1)
        set newURL to newURL & (text item index of aURL) & "&"
    end repeat
    set newURL to newURL & last text item of aURL
    
    set AppleScript's text item delimiters to savedDelimiters
    return newURL
end escapeURL

2 comments

  1. @matthew: This simple modification to the above script will include the page title:

    tell application “Safari” tell document 1 set the clipboard to “” & name & “” end tell end tell

    on escapeURL(aURL) set newURL to “” set savedDelimiters to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {“&”}

    set aCount to count text item of aURL
    repeat with index from 1 to (aCount - 1)
        set newURL to newURL & (text item index of aURL) & "&"
    end repeat
    set newURL to newURL & last text item of aURL
    
    set AppleScript's text item delimiters to savedDelimiters
    return newURL
    

    end escapeURL

Comments are closed.