Script for select Asin from a link

Hello, this is my first post here, thx for attention

I’m building a script that bring the clipboard and select ONLY a part of the text.
The part is the ASIN code from Amazon.
The Asin code is a string after the dp charters in the link, for example:
amazon.it/dp/
amazon.it//dp/
amazon.it//dp/
amazon.it/dp//

More concrete, in this link:

The Asin is
B0B4KGY1YC

In my script Finder bring the clipboard and create a variable, then I need to trim the Asin and put it into a new variable, and put it into clipboard again

tell application "Finder"
	--Clipboard into variable
	set theData to (the clipboard as text)
	
	-- help for selecting only Asin
	
	--Variable in Clipboard\\
	set the clipboard to newData
end tell

Tnk again

But what’s on the clipboard? Is it the url, like this below? If so, do these urls ever have any text after the asin?

https://www.amazon.it/Philips-Momentum-27M1F5500P-Compatible-DisplayPort/dp/B0B4KGY1YC

Here are two approaches. Above the equal signs, it makes sure that the url is on the clipboard.

set urlLink to "https://www.amazon.it/Philips-Momentum-27M1F5500P-Compatible-DisplayPort/dp/B0B4KGY1YC"
set the clipboard to urlLink
-- ==========

set clipLink to the clipboard

set lWord to last word of clipLink
--> "B0B4KGY1YC"

set AppleScript's text item delimiters to "/"
set ltitem to last text item of clipLink
--> "B0B4KGY1YC"

You can find further information on these approaches in the language guide:

Reference forms - index
Global constants - text item delimiters

1 Like

Hello Mockman and thank for help.
Now the script works fine.
The problem is links from amazon that change a lot.

I’ll come back when I will understand how to fix links

Bye

I reply myself :slight_smile:
Link from amazon are very different one from another, so what I need in the script is to “isolate” the word AFTER the “/dp/”

So for example, the Asin from this link
https://www.amazon.it/KOORUI-Monitor-27-pollici-1920x1080/dp/B09Y98HZW3/?sprefix=monitor+,computers,108&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY

is B09Y98HZW3

How can i trim this string?

Thnx in advance

Assuming the ASIN is always made up with capital letters and numbers:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set theLink to "https://www.amazon.it/KOORUI-Monitor-27-pollici-1920x1080/dp/B09Y98HZW3/?sprefix=monitor+,computers,108&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY"
--set theLink to "https://www.amazon.it/KOORUI-Monitor-27-pollici-1920x1080/dp/B09Y98HZW4"
--set theLink to "https://www.apple.com"

set theString to current application's NSString's stringWithString:theLink
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:"(?<=dp/)([A-Z0-9]+)" options:0 |error|:(missing value)
set theRange to theRegEx's rangeOfFirstMatchInString:theString options:0 range:{0, theString's |length|()}
if |length| of theRange = 0 then return false
return (theString's substringWithRange:(theRange)) as string

Thank you Jonah, I will try it!

Or you can use text items delimiters

set myString to "https://www.amazon.it/KOORUI-Monitor-27-pollici-1920x1080/dp/B09Y98HZW3/?sprefix=monitor+,computers,108&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY"
set text item delimiters to "/dp/"
set myString to item 2 of text items of myString
set text item delimiters to "/"
set myString to item 1 of text items of myString

Then it’s probably easiest to use some variation of the second option… text item delimiters. Basically, they segment the string, breaking it up wherever the specified character(s) is. Since URLs treat ‘?’ and ‘/’ as special characters, we can use them as delimiters.

I’ve put three different urls here, each with a slightly different ending. If you change the digit in the ‘set the clipboard to urllink…’ line then you will see how the script will treat each url.

How it works is that it first grabs all of the url to the left of the question mark. If there isn’t a question mark, then it uses the full url. It then splits that up based on where each ‘/’ is. I reverse it because I think it makes it easier to see what’s happening but it’s not necessary.

Note that the result is different depending upon whether there is a ‘/’ after the asin. It picks the first item that isn’t “”.

set urlLink1 to "https://www.amazon.it/Philips-Momentum-27M1F5500P-Compatible-DisplayPort/dp/B0B4KGY1YC"
set urlLink2 to "https://www.amazon.it/Philips-Momentum-27M1F5500P-Compatible-DisplayPort/dp/B0B4KGY1YC/"
set urlLink3 to "https://www.amazon.it/KOORUI-Monitor-27-pollici-1920x1080/dp/B09Y98HZW3/?sprefix=monitor+,computers,108&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY"

set the clipboard to urlLink3
-- ==========

set clipLink to the clipboard


set AppleScript's text item delimiters to "?"
set clipLink to first text item of clipLink -- excludes everything from '?' on
set AppleScript's text item delimiters to "/"

-- break the url into segments, output is for each of the three urls above
set urlSegments to reverse of (text items of clipLink)
--> {"B0B4KGY1YC", "dp", "Philips-Momentum-27M1F5500P-Compatible-DisplayPort", "www.amazon.it", "", "https:"}
--> {"", "B0B4KGY1YC", "dp", "Philips-Momentum-27M1F5500P-Compatible-DisplayPort", "www.amazon.it", "", "https:"}
--> {"", "B09Y98HZW3", "dp", "KOORUI-Monitor-27-pollici-1920x1080", "www.amazon.it", "", "https:"}

if item 1 of urlSegments is "" then
	set asinStr to item 2 of urlSegments
else
	set asinStr to item 1 of urlSegments
end if
set AppleScript's text item delimiters to ""

asinStr
--> "B0B4KGY1YC"
--> "B0B4KGY1YC"
--> "B09Y98HZW3"

Thank you for suggestions, you are all very good at me with AppleScript, of course.
But this is my code, I’m star testing of multiple Amazon links and for now it works fine

set TextItem1 to (the clipboard as text)
set AppleScript's text item delimiters to "/dp"
set theResults to second text item of TextItem1
set AppleScript's text item delimiters to "/"

set theResults2 to second text item of theResults
set the clipboard to theResults2

Thank u for all

1 Like

Thank you. But it seems to much complicated for me.
I’m sure this works fine, but I cannot understand anything. so I cannot edit it for future implementation.
By the way, love it :slight_smile: