Safariの最前面のウィンドウで表示中のamazon.co.jpの製品ページへの最短リンクを取得して、クリップボードに入れるAppleScriptです。動作確認はmacOS 26.4+Safari 26.4で確認しています。添付のアプレットはScript Menuに入れて呼び出しています。
–> Download Enhanced Applet with Framework
Amazon.co.jpの製品URLをWebブラウザのURL欄からコピー&ペーストで取得しようとしても、不要な情報が大量に付加されているため、Blogなどに投稿するのにけっこう苦労します。
そこで、実際に対象の製品ページへのリンクが可能な最短のURL(短縮URLではなく)を作成してクリップボードに入れます。
HTMLの解析については、いつものHTMLReader Framework(https://github.com/nolanw/HTMLReader)を利用しています。そのため、Script DebuggerからExportしたEnhanced Appletとして動作するように想定しています。
正直なところ、やり方はいくつもありますし、プログラム内容もやっつけで書いているので見るに堪えない部分はあるのですが、実際に動作する日常的なScriptなんてこんなもんだろうかと。
本Scriptは、日本のAmazon(amazon.co.jp)のURLを対象に処理していますが、他国のAmazon用に書き換えるのもとくに難しくはありません。
Google Chromeなど他のWebブラウザ用に改変するのもとくに難しくないので、試してみるといいのではないでしょうか。
| AppleScript名:最前面に表示中のAmazonのページから製品リンクURLを抽出する_v2.scptd |
| — – Created by: Takaaki Naganoya – Created on: 2026/03/01 — – Copyright © 2026 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "HTMLReader" –https://github.com/nolanw/HTMLReader use scripting additions property NSString : a reference to current application’s NSString property NSPredicate : a reference to current application’s NSPredicate property HTMLDocument : a reference to current application’s HTMLDocument property NSMutableArray : a reference to current application’s NSMutableArray –URL Check set uRes to retURLFromActiveSafariTab() of me if uRes does not start with "https://www.amazon.co.jp" then return –change each location’s Amazon URL set aURL to getProductURLFromAmazon() of me if aURL = "" then return set bURL to current application’s |NSURL|’s URLWithString:aURL set aList to bURL’s pathComponents() aList’s removeObjectAtIndex:1 –2番目のアイテムを削除 set bList to aList as list set bLen to length of bList if bLen is not equal to 3 then return if contents of item 1 of bList is not equal to "/" then return if contents of item 2 of bList is not equal to "dp" then return set aScheme to bURL’s |scheme|() as string set aHost to bURL’s |host|() as string set resURL to aScheme & "://" & aHost & "/" & (contents of item 2 of bList) & "/" & (contents of item 3 of bList) set the clipboard to resURL beep 1 on getProductURLFromAmazon() –Get HTML Source set hRes to retHTMLSourceFromActiveSafariTab() of me set aHTML to current application’s HTMLDocument’s documentWithString:(hRes as string) set linkList to (aHTML’s nodesMatchingSelector:"link") log linkList repeat with i in (linkList as list) set j to contents of i set aProp to (j’s attributes’s valueForKeyPath:("rel")) as string if aProp = "canonical" then set resURL to (j’s attributes’s valueForKeyPath:("href")) return resURL as string end if end repeat return "" end getProductURLFromAmazon on retHTMLSourceFromActiveSafariTab() tell application "Safari" if running then if (every document) = {} then return false tell front document set htmlRes to source end tell else return false end if end tell set aStr to current application’s NSString’s stringWithString:htmlRes set aNFC to (aStr’s precomposedStringWithCanonicalMapping()) as text –Normalize Text return aNFC end retHTMLSourceFromActiveSafariTab on retURLFromActiveSafariTab() tell application "Safari" if running then if (every document) = {} then return missing value tell front document set htmlRes to URL end tell else return missing value end if end tell end retURLFromActiveSafariTab |
(Visited 7 times, 2 visits today)
