Applescript

2013.06.17

ダウンロードしたファイルを自動的にフォルダ分け

この記事は3965日前に投稿されたものです。

すぐにダウンロードしたファイルが大量になってきて、分かりにくくなります。
ダウンロードしたものをApplescriptで整理できたら便利だと思い、調べてみました。

Applescript

ダウンロードしたファイルは「ダウンロード」フォルダに入るように設定しています。なので、このフォルダにアクションを設定すればOK!

 

ただ僕はChromeを使用しており、DL終了後にウィンドウ下部に表示されているアイコンから解凍することが多々あるので、DL直後にファイルが移動されパスが変わるのはまずいです。
なので、「今日」の日付を取得し、作成日が「今日のものでない」場合にのみ、移動させるようにしました。

フォルダに入ったら実行 > 今日の日付取得 > 今日のものでない場合、作成日のフォルダへ

Mac OS X Hintsに投稿されていたコードを参考に、「今日」ダウンロードしたものじゃなければ、そのファイルの「作成日のフォルダ」を作って移動させるようにしています。

on adding folder items to thisFolder after receiving theseItems
	tell application "Finder"
		set fileList to every file of thisFolder as list
		set folderList to every folder of thisFolder as list
		
		repeat with i from 1 to number of items in fileList
			set curItem to item i of fileList as alias
			set curItemPath to (do shell script "stat -f "%B" " & (quoted form of (POSIX path of curItem)))
			set createDate to do shell script ("date -r " & curItemPath & " "+%Y-%m-%d"")
			set todayDate to do shell script "date +%Y-%m-%d"
			
			if not (createDate = todayDate) then
				if not (exists folder createDate of thisFolder) then
					make new folder at thisFolder with properties {name:createDate}
				end if
				try
					move file curItem to folder createDate of thisFolder
				end try
			end if
		end repeat
	end tell
end adding folder items to

保存してフォルダアクションに設定すれば、完成です。

関連記事:フォルダアクションの設定の仕方

まとめ

ファイルの種類によっても振り分けもいいかもしれません。jpgはjpgフォルダへ、zipはzipフォルダへ。みたいな。その場合は、拡張子で分岐すればいけそうですね。