2019/06/16

フォルダ内のファイル名をテキストエディットにhtmlタグ付きで書き込むAppleScript

時々載せるAppleScriptはメモを兼ねて自分用に作っているのですが、今回のは一年ほど前に作った「フォルダ内のファイル名をテキストエディットにhtmlタグ付きで書き込むAppleScript」。

内容は、フォルダー内にあるファイル(JPEG画像)をリンク(download属性付きaタグ)を持ったhtmlを作成するためのAppleScriptです。
例えばフォルダー内にabc.jpgというファイルがあった時
<a href="abc.jpg" download="abc.jpg">abc.jpg</a>
という文字列をテキストファイルでテキストエディットに書き出します。
フォルダー内のファイルをダウンロードするhtmlを書く時にちょっと便利です。




set objsPath to choose folder
tell application "Finder"
tell folder objsPath
set objs to name of every file
end tell
end tell
tell application "TextEdit"
activate
make new document at before first document
tell front document
repeat with obj in objs
if (obj as text) ends with ".jpg" then -- 抽出拡張子指定
make new paragraph at after last paragraph of it with data "<a href=\"" & obj & "\" download=\"" & obj & "\">" & obj & "</a> " & return
end if
end repeat
end tell

end tell