Obtain bad URL from Safari’s current tab with Applescript

applescriptsafariurl

The web site my.yahoo.com has an irritating bug (I'm surprised it works at all) where clicking on one of its links sometimes produces a URL such as the following:

http://sports.yahoo.comhttps//sports.yahoo.com/news/rested-kings-ready-host-not-rested-hurricanes-082033171--nhl.html

In the example, you can see that "http://sports.yahoo.com" is repeated twice. I wanted to find the easiest way to correct these URLs while surfing. Sadly, I cannot even perform the first necessary action, which is to pull the URL from the title bar of the current tab. You would think sending the following command to Safari in an Applescript would work:

set thisTabUrl to the URL of the current tab in window 1

Sadly, apparently, if the URL is invalid, I get:

file:///Applications/Safari.app/Contents/Resources/

Is there some other way I can coax Safari to give me the contents of the URL area of the current tab to then process in the Applescript, which will then attempt to set the current tab's URL to the correct url and open that page? I'm pretty sure I have the rest of the necessary code, to open the correct URL in that same tab.

For extra credit, what might be the most effortless way to launch this script when encountering such a bad URL while surfing in Safari?

Best Answer

I've tried numerous times to reproduce the issue, however I couldn't. So this solution was tested by manually entering a standard URL twice, via copy and paste, and going from there. I also used the bad URL you've shown in your question.

In Safari, this essentially tries to build a valid URL from of the double URL using AppleScript run as an Automator Service when pressing B and sets the current document (window, tab) to the proper URL.

Create an Automator Service with the settings as shown in the image below and save it as:
Fix Bad URL

Then in System Preferences > Keyboard > Shortcuts > Services, scroll down to General, select
Fix Bad URL, click Add Shortcut and type: B

Fix Bad URL Automator Service


AppleScript code for the Automator Service:

on badURL()
    try
        --    # Put the bad URL on the Clipboard.
        tell application "Safari"
            activate
            delay 0.5
            tell application "System Events"
                key code 37 using {command down} # ⌘L
                delay 0.5
                key code 8 using {command down} # ⌘C
                delay 0.5
            end tell
        end tell
        --    # Retrieve the bad URL from the Clipboard.
        set theBadURL to get (the clipboard)
        --    # Trim the first eight characters off of 'theBadURL'.
        --    # This is done because the bad URL can have an occurrence of both 'http' and 'https' in the string in either
        --    # order and by trimming off, it lessens the amount of logic necessary to build a good URL from the bad URL.
        --    # The good URL, after all, is going to be built from what's after the second occurrence of either one. 
        --    # Test first for 'https' then 'http', as that makes more sense logically to do so.            
        set theBadURL to do shell script "awk 'BEGIN { print substr(\"" & theBadURL & "\", 9) }'"
        --    # Build the good URL from the bad URL.
        set theGoodURL to do shell script "awk -F 'https.*//' '{print $2}'<<<" & quoted form of theBadURL
        if theGoodURL is not equal to "" then
            set theGoodURL to "https://" & theGoodURL
        else
            set theGoodURL to do shell script "awk -F 'http.*//' '{print $2}'<<<" & quoted form of theBadURL
            if theGoodURL is not equal to "" then
                set theGoodURL to "http://" & theGoodURL
            end if
        end if
        return theGoodURL
    on error eStr number eNum
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
        return
    end try
end badURL

on run
    try
        tell application "Safari"
            activate
            set thisTabsURL to (get URL of current tab of window 1)
            --    # Check for bad URL and if found, build a good URL from it.
            tell current application
                if thisTabsURL contains "file:" then
                    set theGoodURL to my badURL()
                    tell application "Safari" to set the URL of the front document to theGoodURL
                end if
            end tell
        end tell
    on error eStr number eNum
        display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with icon caution
        return
    end try
end run

Note that while I tested this under OS X 10.8.5 and macOS 10.12 and it did work under my test conditions, nonetheless it may not work properly every time under every condition and hence why the try and on error statements are being used in the code. Hopefully this will trap any error(s) with appropriate output to then enhance the code to handle any error(s) that didn't occur during my testing.