レ点腫瘍学ノート

日記/2021年/6月28日/Macで写真を半自動で日付毎のフォルダに整理する の履歴差分(No.1)


#author("2021-06-28T07:03:03+09:00;1970-01-01T18:00:00+09:00","default:tgoto","tgoto")
Macの''スクリプトエディタ.app''を使って、デジカメで撮影した写真ファイルなどを半自動で日付毎のフォルダに分類して整理するスクリプトを、忘れないようにここにメモしておきます。

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

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

*スクリプトの準備 [#bb042bd3]

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

#ref(a.png,nolink)

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

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

*スクリプト [#n062fd4e]

 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の画面右上に表示されたスクリプトのマークからこれを時効できます。

#ref(a.png,nolink)

*年/月/年_月_日のフォルダに写真を保存するには [#bac10ebd]

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

#ogp(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