見出し画像

【DTP作業効率化】ファイル名をフォルダ階層に沿って付け変えるApple Script

下図の様にファイルに名前を付けかえます。
DTP作業では写真ファイルの名前に重複があると思いもよらない事故につながる。当然、クライアントから入稿される写真は、適切に名前がつけられていない。
運動会のフォルダに一年、二年のフォルダがあってその中にそれぞれA、B、Cの写真があるなんてことも。
下図の様に付け替えればフォルダを整理してもどこで使う何の写真かも把握しやすい。


下記のコードを「スクリプトエディタ」にコピペので使えます。

global the_folder, dig_boolean
tell application "Finder"
	set dig_boolean to true
	tell me to display dialog "親フォルダーの名前をファイルに追加します。指定階層から下に全階層分のフォルダー名を追加するか、指定フォルダの階層だけにするか選べます。" buttons {"キャンセル", "指定階層から下に全階層", "指定階層1つだけ"}
	set button_reply to button returned of the result
	if the button_reply is "キャンセル" then
		return
	else if the button_reply is "指定階層1つだけ" then
		set dig_boolean to false
	end if
	set the_folder to choose folder with prompt "リネームするファイルが入ったフォルダを選択してください。"
	
	my into_the_folder(the_folder)
end tell

to into_the_folder(folder1)
	tell application "Finder"
		set its_items to every item of folder1
		repeat with i in its_items
			set its_kind to kind of i
			if its_kind is "フォルダ" and dig_boolean is true then
				set folder2 to i
				my into_the_folder(folder2)
			else
				if its_kind is not "フォルダ" then
					set its_path to ""
					set its_container to container of i as alias
					repeat
						if its_container is the_folder then
							set its_path to name of its_container & "_" & its_path
							exit repeat
						else
							set its_path to name of its_container & "_" & its_path
							set its_container to container of its_container as alias
						end if
					end repeat
					set its_name to name of i
					set new_name to its_path & its_name
					log new_name
					set name of i to new_name
				end if
			end if
		end repeat
	end tell
end into_the_folder

この記事が気に入ったらサポートをしてみませんか?