Applescript 将代码添加到现有脚本以删除“?”来自 URL
Applescript adding code to existing script to remove "?" from a URL
我有这个脚本可以从亚马逊 URL 剥离跟踪数据并将其剥离到基础。
告诉应用程序“Google Chrome”
将 Url 设置为 window 1
的活动选项卡的 URL
将 splitCharacters 设置为“?”
将 splitOffset 设置为(theUrl 中 splitCharacters 的偏移量)+(计数 splitCharacters)- 1
通过 theUrl
的 splitOffset 将 newURL1 设置为文本 1
将 AppleScript 的文本项分隔符设置为“p/”
将 itemNumber 设置为 newURL1
的文本项目 2
将 newUrl 设置为“https://www.amazon.com/dp/” & itemNumber
将剪贴板设置为 newUrl
--return 新网址
将 window 1 的活动选项卡的 URL 设置为 newUrl
结束告诉
当前脚本给我:
https://www.amazon.com/dp/B08P1GKG4D? <----- 那该死的?需要被消灭!
我还需要它来摆脱“?”最后,但我无法弄清楚如何实现这一点,因为之前的字符?总是不同的。我不能只告诉它删除最后一个字符,因为有时?不存在,我会丢失项目编号的最后一个字符。以及“?”之后的文字也各不相同。所以我不能使用不同的字符作为分割点。
我知道(希望?)它比我想的更简单....但我就是想不通!
借助 AppleScriptObjC 非常容易,因为 Foundation 的 NSURL
提供了有效解析 URL 的一切。
轨道编号是 URL
的 path
的最后一个路径组件
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
我已经稍微清理了你的 OP 的 code,因为它的大部分不应该包含在 tell
block 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
根据你问题中的URL,newUrl
的值变为:https://www.amazon.com/dp/B08P1GKG4D
备注:
- 任何时候您从 默认值 更改
AppleScript's text item delimiters
,即 {}
或 ""
,请确保在使用后重新设置它,因此我添加了 set AppleScript's text item delimiters to ""
- 我将
splitCharacters
设为单数,因为在这个用例中它毕竟是一个单数 字符。
- 我删除了
+ (count splitCharacters)
,因为根本没有必要。
我有这个脚本可以从亚马逊 URL 剥离跟踪数据并将其剥离到基础。
告诉应用程序“Google Chrome”
将 Url 设置为 window 1
的活动选项卡的 URL
将 splitCharacters 设置为“?”
将 splitOffset 设置为(theUrl 中 splitCharacters 的偏移量)+(计数 splitCharacters)- 1
通过 theUrl
的 splitOffset 将 newURL1 设置为文本 1
将 AppleScript 的文本项分隔符设置为“p/”
将 itemNumber 设置为 newURL1
的文本项目 2
将 newUrl 设置为“https://www.amazon.com/dp/” & itemNumber
将剪贴板设置为 newUrl
--return 新网址
将 window 1 的活动选项卡的 URL 设置为 newUrl
结束告诉
当前脚本给我:
https://www.amazon.com/dp/B08P1GKG4D? <----- 那该死的?需要被消灭!
我还需要它来摆脱“?”最后,但我无法弄清楚如何实现这一点,因为之前的字符?总是不同的。我不能只告诉它删除最后一个字符,因为有时?不存在,我会丢失项目编号的最后一个字符。以及“?”之后的文字也各不相同。所以我不能使用不同的字符作为分割点。
我知道(希望?)它比我想的更简单....但我就是想不通!
借助 AppleScriptObjC 非常容易,因为 Foundation 的 NSURL
提供了有效解析 URL 的一切。
轨道编号是 URL
的path
的最后一个路径组件
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
我已经稍微清理了你的 OP 的 code,因为它的大部分不应该包含在 tell
block 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
根据你问题中的URL,newUrl
的值变为:https://www.amazon.com/dp/B08P1GKG4D
备注:
- 任何时候您从 默认值 更改
AppleScript's text item delimiters
,即{}
或""
,请确保在使用后重新设置它,因此我添加了set AppleScript's text item delimiters to ""
- 我将
splitCharacters
设为单数,因为在这个用例中它毕竟是一个单数 字符。 - 我删除了
+ (count splitCharacters)
,因为根本没有必要。