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
AppleScript code for the Automator Service:
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
andon 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.