1
votes

I have this script that peels off the tracking data from an Amazon URL and strips it down to the basics.

Tell application "Google Chrome"
set theUrl to URL of active tab of window 1
set splitCharacters to "?"
set splitOffset to (offset of splitCharacters in theUrl) + (count splitCharacters) - 1
set newURL1 to text 1 thru splitOffset of theUrl
set AppleScript's text item delimiters to "p/"
set itemNumber to text item 2 of newURL1
set newUrl to "https://www.amazon.com/dp/" & itemNumber
set the clipboard to newUrl
--return newUrl
set URL of active tab of window 1 to newUrl
end tell

Using the URL from a FB post: https://www.amazon.com/Squeaky-Aggressive-Interactive-Squeaker-Indestructible/dp/B08P1GKG4D?%2F%3Ftag=renbar-ace-20&fbclid=IwAR2DqPuwdKQ9c1EQXtJJHRlYjnq_Lzj4_mW_qoNPq6BgVs1y94cev5h07B4

the current script gives me:

https://www.amazon.com/dp/B08P1GKG4D? <----- that darn ? needs to be exterminated!

I need it to also get rid of the "?" at the end but I can't figure out how to make that happen since the characters prior to the ? are always different. I can't just tell it to delete the last character because sometimes that ? isn't there and I'd lose the last character of the item number. And the text past the "?" also varies. So I can't use a different character as the split point.

I know (hope?) it's more simple than I can figure out....but I just can't figure it out!

2

2 Answers

0
votes

I've cleaned up the code of your OP a bit, as most of it should not have been wrapped in tell block of Google Chrome.

tell application "Google Chrome" to ¬
    set theUrl to URL of active tab of window 1

set splitCharacter to "?"
set splitOffset to (offset of splitCharacter in theUrl) - 1
set newURL1 to text 1 thru splitOffset of theUrl

set AppleScript's text item delimiters to "p/"
set itemNumber to text item 2 of newURL1
set AppleScript's text item delimiters to ""

set newUrl to "https://www.amazon.com/dp/" & itemNumber
set the clipboard to newUrl

tell application "Google Chrome" to ¬
    set URL of active tab of window 1 to newUrl

Based on the URL in your question, the value of newUrl becomes: https://www.amazon.com/dp/B08P1GKG4D


Notes:

  • Anytime you change AppleScript's text item delimiters from its default, i.e. {} or "", make sure you reset it after using it and thus I added set AppleScript's text item delimiters to ""
  • I made splitCharacters singular as it is after all a singular character in this use case.
  • I removed + (count splitCharacters) as it is just not at all necessary.
0
votes

It's pretty easy with help of AppleScriptObjC because Foundation's NSURL provides everything to parse URLs efficiently.

The track number is the last path component of the path of the URL

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

tell application "Google Chrome" to set theURL to URL of active tab of window 1
set objcURL to current application's NSURL's URLWithString:theURL
set itemNumber to (objcURL's |path|()'s lastPathComponent()) as text
set newUrl to "https://www.amazon.com/dp/" & itemNumber
set the clipboard to newUrl
tell application "Google Chrome" to set URL of active tab of window 1 to newUrl