Automate URL Redirection in Mac OS Automator

I would like some assistance to write an AppleScript to automatically redirect amazon.com to smile.amazon.com. I tend to always forget to use smile. With this automator I will never forget to. Unfortunately I am new to using the tool.


Solution 1:

REVISED SOLUTION

This following solution is to be used with Google Chrome.

Paste this following AppleScript code into a new Script Editor.app document then save it as a "Stay Open" application.

Double-clicking this new application in Finder, will launch Google Chrome if it is not already running. It will immediately then start monitoring the URLs of every tab in window 1 of Google Chrome. Whenever the URL of any tab starts with https://www.amazon.com, is detected, it will automatically be redirected to https://smile.amazon.com . When Google Chrome is no longer running, the application will quit on it's own.

The first time you open your new applet, be sure to choose the option for allowing it to run... on any system dialogs which may pop-up on first launch.

This AppleScript code works for me using the latest version of macOS Mojave.

on run
    tell application "Google Chrome" to launch
end run

on idle
    if application "Google Chrome" is not running then quit me
    tell application "Google Chrome" to tell window 1
        try
            set amazonTabs to (tabs whose URL starts with "https://www.amazon")
            delay 0.1
            if amazonTabs is not {} then
                set URL of item 1 of amazonTabs to "https://smile.amazon.com"
            end if
        end try
    end tell
    if application "Google Chrome" is not running then quit me
    return 0.5
end idle

on quit
    continue quit -- allows the script to quit
end quit

Due to some comments on the original code of my solution using too much of the computer resources, I believe the above code no longer produces those issues.

enter image description here

Safari Version

on run
    tell application "Safari" to launch
end run

on idle
    if application "Safari" is not running then quit me
    tell application "Safari" to tell window 1
        try
            set amazonTabs to (tabs whose URL starts with "https://www.amazon")
            delay 0.1
            if amazonTabs is not {} then
                set URL of item 1 of amazonTabs to "https://smile.amazon.com"
            end if
        end try
    end tell
    if application "Safari" is not running then quit me
    return 0.5
end idle

on quit
    continue quit -- allows the script to quit
end quit

To be able to control Safari using AppleScript, be sure to enable "Show Develop menu in menu bar" in Safari's Preferences.

enter image description here

You'll also need to select "Allow JavaScript from Apple Events" in Develop menu bar item.

enter image description here