weblocファイルからURLを取得

Webインターネットロケーションファイル(.webloc)から記録されているURLを抽出するスクリプトです。
Safari、または、Google Chrome・Firefoxで作成したものに対応しています。それ以外は未確認。

on run
tell application "Finder" to return my main(the selection as alias list)
end run

on main(myFiles)
tell application "Finder"
set urlList to {}
repeat with i in myFiles
if (name extension of i) = "webloc" then
set aCon to (read i)
if aCon starts with "bplist" then
set aStr to text (offset of "http" in aCon) thru -1 of aCon
set bStr to text items of aStr
set cStr to {}
repeat with i in bStr
set aRecord to (i as record)
if (ASCII numberclass ktxt» of aRecord)) is 11 then exit repeat
set cStr to cStr & i
end repeat
set aURL to (text 1 thru -2 of cStr) as text
set the end of urlList to aURL
else if aCon starts with "<?xml " then
repeat with i in (paragraphs of aCon)
if i contains "<string>" then
set bURL to replaceText(text ((offset of "<string>" in i) + 8) thru -1 of i, "</string>", "") of me
exit repeat
end if
end repeat
try
set the end of urlList to bURL
end try
end if
end if
end repeat
end tell
set curDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set aRes to text items of urlList
set AppleScript's text item delimiters to return
set uList to aRes as text
set AppleScript's text item delimiters to curDelim
if urlList is not {} then set the clipboard to uList
return uList
end main

--任意のデータから特定の文字列を置換
on replaceText(origData, origText, repText)
set curDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {origText}
set origData to text items of origData
set AppleScript's text item delimiters to {repText}
set origData to origData as text
set AppleScript's text item delimiters to curDelim
return origData
end replaceText

こちらは、URLとファイル名を一緒に取得するスクリプトです。(2023年02月19日 追加掲載)

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

property NSString : a reference to current application's NSString

on run
tell application "Finder" to return my main(the selection as alias list)
end run

on main(myFiles)
tell application "Finder"
set urlList to {}
repeat with i in myFiles
if (name extension of i) = "webloc" then
set aPath to POSIX path of i
set FN to (NSString's stringWithString:aPath)'s lastPathComponent()'s stringByDeletingPathExtension() as string
set aCon to (read i)
if aCon starts with "bplist" then
set aStr to text (offset of "http" in aCon) thru -1 of aCon
set bStr to text items of aStr
set cStr to {}
repeat with i in bStr
set aRecord to (i as record)
if (ASCII numberclass ktxt» of aRecord)) is 11 then exit repeat
set cStr to cStr & i
end repeat
set aURL to (text 1 thru -2 of cStr) as text
set the end of urlList to (aURL & " " & FN)
else if aCon starts with "<?xml " then
repeat with i in (paragraphs of aCon)
if i contains "<string>" then
set bURL to replaceText(text ((offset of "<string>" in i) + 8) thru -1 of i, "</string>", "") of me
exit repeat
end if
end repeat
try
set the end of urlList to (bURL & " " & FN)
end try
end if
end if
end repeat
end tell
set curDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set aRes to text items of urlList
set AppleScript's text item delimiters to return
set uList to aRes as text
set AppleScript's text item delimiters to curDelim
if urlList is not {} then set the clipboard to uList
return uList
end main

--任意のデータから特定の文字列を置換
on replaceText(origData, origText, repText)
set curDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {origText}
set origData to text items of origData
set AppleScript's text item delimiters to {repText}
set origData to origData as text
set AppleScript's text item delimiters to curDelim
return origData
end replaceText

動画は、Webインターネットロケーションファイル(.webloc)を選択し、上記スクリプトを実行、クリップボードにコピーされたURLをNumbersのシートに貼り付けしたものです。

更新履歴

inserted by FC2 system