Macの手書き説明書

macOSのアプリ紹介と基本的な使い方を解説

選択したFinder項目を新規フォルダにまとめるAppleScript

Thing collar Bu logさんが公開なされているファイル整理にとっても役立ちそうなAppleScriptです。

tell application "Finder"
	if (number of items of (selection as list)) < 2 then return
end tell

set theYear to (year of (current date)) as string
set theMonth to ((month of (current date)) as number) as string
if (number of characters of theMonth) = 1 then set theMonth to ("0" & theMonth)
set theDay to (day of (current date)) as string
if (number of characters of theDay) = 1 then set theDay to ("0" & theDay)
set theHour to (hours of (current date)) as string
if (number of characters of theHour) = 1 then set theHour to ("0" & theHour)
set theMinutes to (minutes of (current date)) as string
if (number of characters of theMinutes) = 1 then set theMinutes to ("0" & theMinutes)
set theSeconds to (seconds of (current date)) as string
if (number of characters of theSeconds) = 1 then set theSeconds to ("0" & theSeconds)
set theText to "新規フォルダ-" & (theYear & theMonth & theDay & theHour & theMinutes & theSeconds)

tell application "Finder"
	set theContainer to (container of (item 1 of (selection as list))) as alias
	set theFolder to (make new folder at theContainer)
	move selection to theFolder
	set movedItems to result
	namingFolder(movedItems, theText, theFolder, theContainer) of me
end tell

on namingFolder(movedItems, theText, theFolder, theContainer)
	tell application "Finder"
		activate
		display dialog "新しいフォルダの名前: " & return & "" default answer theText buttons {"キャンセル", "内包する項目名", "OK"} default button 3
		set theResult to result
		if button returned of theResult is "OK" then
			set folderName to (text returned of theResult)
			if folderName is "" then set folderName to theText
			if folderName starts with "." then
				display dialog "\".\"で始まる名前は使えません。"
				namingFolder(movedItems, theText, theFolder, theContainer) of me
			else if folderName contains ":" then
				display dialog "\":\"が含まれる名前は使えません。"
				namingFolder(movedItems, theText, theFolder, theContainer) of me
			else
				set existsFolder to (exists of (folder folderName of theContainer))
				if existsFolder is true then
					set n to 1
					repeat until existsFolder is false
						set newFolderName to (folderName & " " & n)
						set n to (n + 1)
						set existsFolder to (exists of (folder newFolderName of theContainer))
					end repeat
					set name of theFolder to newFolderName
				else
					set name of theFolder to folderName
				end if
			end if
		else if button returned of theResult is "内包する項目名" then
			editText(movedItems, theText, theFolder, theContainer) of me
		end if
	end tell
end namingFolder

on editText(movedItems, theText, theFolder, theContainer)
	tell application "Finder"
		set tmp to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		try
			set itemNameList to {}
			repeat with i from 1 to (number of items of movedItems)
				set tmpName to "" as Unicode text --ここの as Unicode text が重要。
				set itemName to (name of item i of movedItems)
				if (number of every text item of itemName) > 1 then
					set textList to (text items 1 thru -2 of (every text item of itemName))
					repeat with j from 1 to (number of items of textList)
						set tmpName to tmpName & (item j of textList)
						if j is not (number of items of textList) then set tmpName to tmpName & "."
					end repeat
				else
					set tmpName to itemName
				end if
				set itemNameList to (itemNameList & tmpName)
			end repeat
			set AppleScript's text item delimiters to tmp
		on error
			set AppleScript's text item delimiters to tmp
		end try
		choose from list itemNameList default items (item 1 of itemNameList)
		if result is not false then
			set theText to (item 1 of result)
			namingFolder(movedItems, theText, theFolder, theContainer) of me
		end if
	end tell
end editText
このAppleScriptをスクリプトエディタで開く

バックスラッシュが\になってしまっているのでコピペした人は適宜直してください。

このスクリプトを「~/ライブラリ/Scripts」に置き、Finderで新規フォルダへ移動させたい項目を選択してから、スクリプトメニューから実行すると次のようなダイアログが表示されます。

選択した項目を新規フォルダへ移動させるスクリプト

あとはフォルダ名を入力してreturnを押すだけ。

なにも入力しないと今日の日付、内包する項目名からフォルダを作ることもできて親切。

ドロップレット用のコードも紹介されているので、ドロップレットをFinderのツールバーに登録するのも良さそうです。

広告


関連記事

広告

この記事を共有する

広告

関連記事

0 Comments

Add your comment