レ点腫瘍学ノート

Top / 日記 / 2021年 / 6月28日

Macで写真を半自動で日付毎のフォルダに整理する

Macのスクリプトエディタ.appを使って、デジカメで撮影した写真ファイルなどを半自動で日付毎のフォルダに分類して整理するスクリプトを、忘れないようにここにメモしておきます。

昔はGoogleのPicasa for Macで、そのあとはAdobe Photoshop Lightroom 5で長らく写真の整理を行っていました。これらのアプリはいずれもデジカメで撮影した写真をSDカードから取り込むと、2021年のフォルダの中の2021-06-27のような1日ごとのフォルダに分けて写真を保存してくれます。しかしPicasaはとっくの昔になくなってしまい、またPhotoshop Lightroom 5は正式サポートが終了してからもしばらくは使えていましたがMac OSがBig Surになってからはついに画像ファイルを開いても一部の表示が崩れたりするようになってしまいました。

せめて写真の整理機能だけでも使いたかったのですが、無料(または安価)で使えてよいものがないので、このスクリプトで代用しています。

スクリプトの準備

Macのアプリケーション -> ユーティリティ -> スクリプトエディタ.appを開きます。「新規作成」でスクリプトの作成画面を開いて後述のスクリプトをコピペします。するとこの画像のような状態になります。

860c0e2f6a.png

Finderで写真を整理したいフォルダを開いておき、スクリプトエディタ.appでこのスクリプトファイルを開いて再生ボタンをクリックすると写真が撮影日毎のフォルダに整理されます。フォルダは2021/2021-06-27/などの構成になります。

外付けストレージではうまく動作しないことがあるようです。うまく行かない場合はローカルのHDD/SSDなどにデータを移してから実行してください。

なお、大切な写真が消えてしまうことがないように最初はちゃんとバックアップを取ったフォルダで実行してください。ファイル消失などの責任は負いかねます。

スクリプト

tell application "Finder"
set aFol to (insertion location) as alias
if (aFol as string) does not end with "Desktop:" then
tell folder aFol

set aList to every file as alias list

repeat with i in aList
set aFile to contents of i
set posixAFile to POSIX path of aFile
set creationDate to creation date of aFile
set aYear to year of creationDate
set aMonth to month of creationDate as number
set aDay to day of creationDate
if aMonth < 10 then set aMonth to "0" & aMonth
if aDay < 10 then set aDay to "0" & aDay

if not (exists folder (aYear as text)) then ¬
make new folder at aFol with properties {name:aYear}

tell application "Finder"
set yearFolder to (aFol & aYear as text) as alias
set thisFolder to aYear & "-" & aMonth & "-" & aDay as text
tell folder yearFolder
if not (exists folder thisFolder) then
make new folder at yearFolder with properties {name:thisFolder}
end if
end tell
end tell
set bFol to POSIX path of ((aFol & aYear & ":" & thisFolder as text) as alias)
do shell script "mv " & quoted form of posixAFile & " " & quoted form of bFol
end repeat

end tell
end if
end tell

さらにこのスクリプトファイル.scptを保存した後、ユーザのライブラリフォルダの中のscriptsフォルダにこのコピーを保存し、またスクリプトエディタの環境設定で「メニューバーにスクリプトエディタを表示」をオンにしておくと、Macの画面右上に表示されたスクリプトのマークからこれを実行できます。

860c0e2f6a_b.png

年/月/年_月_日のフォルダに写真を保存するには

ここにも似たスクリプトに関しての言及がありました。中身は非常によく似ていますが、こちらの場合は2021/06/2021_06_27のフォルダに写真を保存してくれます。

macで日付別フォルダを作成して写真取り込みができるアプリは? - MacにiPhoneやデジカメから写真を取り込んで管... - Yahoo!知恵袋
macで日付別フォルダを作成して写真取り込みができるアプリは? MacにiPhoneやデジカメから写真を取り込んで管理する際に、写真や動画の撮影日ごとにフォルダを作って取り込みをしてくれるMac用のアプリはありませんか?取り込みができない場合は、すでに取り込んだファイルを日付ごとにフォルダを作って整理するものでも構いません。以前はPicasaを使っていました。 --ファイルを作成日ごとに振り分け(...
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10193263630
--ファイルを作成日ごとに振り分け(年/月/日付)
tell application "Finder"
set aFol to (insertion location) as alias
if (aFol as string) does not end with "Desktop:" then -- デスクトップにあるファイルは処理しない
tell folder aFol

set aList to every file as alias list

repeat with i in aList
set aFile to contents of i
set posixAFile to POSIX path of aFile -- POSIX pathに変換
set creationDate to creation date of aFile -- 作成日を取得
set aYear to year of creationDate -- 年を取得
set aMonth to month of creationDate as number -- 月を取得
set aDay to day of creationDate -- 日を取得
if aMonth < 10 then set aMonth to "0" & aMonth -- 10月以前なら0を付ける
if aDay < 10 then set aDay to "0" & aDay -- 10日以前なら0を付ける

if not (exists folder (aYear as text)) then ¬
make new folder at aFol with properties {name:aYear} -- 年のフォルダを作成

tell application "Finder"
set yearFolder to (aFol & aYear as text) as alias -- 年のフォルダのエイリアス
tell folder yearFolder
if not (exists folder (aMonth as text)) then
make new folder at yearFolder with properties {name:aMonth} -- 月のフォルダを作成
end if
end tell

set monthFolder to (yearFolder & aMonth as text) as alias -- 月のフォルダのエイリアス
set thisFolder to aYear & "_" & aMonth & "_" & aDay as text -- 年月日のフォルダ名
tell folder monthFolder
if not (exists folder thisFolder) then
make new folder at monthFolder with properties {name:thisFolder} -- 年月日のフォルダを作成
end if
end tell
end tell
set bFol to POSIX path of ((aFol & aYear & ":" & aMonth & ":" & thisFolder as text) as alias) -- 移動先のフォルダのパス
do shell script "mv " & quoted form of posixAFile & " " & quoted form of bFol -- 移動
end repeat

end tell
end if
end tell
[新版 zsh&bash対応]macOS×コマンド入門 ──ターミナルとコマンドライン、基本の力 | 西村 めぐみ, 新居 雅行 |本 | 通販 | Amazon
Amazonで西村 めぐみ, 新居 雅行の[新版 zsh&bash対応]macOS×コマンド入門 ──ターミナルとコマンドライン、基本の力 。アマゾンならポイント還元本が多数。西村 めぐみ, 新居 雅行作品ほか、お急ぎ便対象商品は当日お届けも可能。また[新版 zsh&bash対応]macOS×コマンド入門 ──ターミナルとコマンドライン、基本の力 もアマゾン配送商品なら通常配送無料。
https://amzn.to/3jgePyR
Macの便利ワザ 333 (Macが超快適に使えるようになる!) | 河本 亮, 小原裕太, standards |本 | 通販 | Amazon
Amazonで河本 亮, 小原裕太, standardsのMacの便利ワザ 333 (Macが超快適に使えるようになる!)。アマゾンならポイント還元本が多数。河本 亮, 小原裕太, standards作品ほか、お急ぎ便対象商品は当日お届けも可能。またMacの便利ワザ 333 (Macが超快適に使えるようになる!)もアマゾン配送商品なら通常配送無料。
https://amzn.to/3ycSnej

この記事に対するコメント

このページには、まだコメントはありません。

お名前:

更新日:2021-06-28 閲覧数:5008 views.