Tagの条件付き自動付与

大量のフォルダの中に、各々十数個の書類が入っている状況で、書類のうち、ひとつでも特定のTag(例えば赤)がある場合、その書類が入っているフォルダにも特定のTag(例えば赤)を自動的に付けたいのですが、何か良い方法はありませんでしょうか?

具体例を下記します。


フォルダ1---ファイル1、ファイル2(tag)、ファイル3、ファイル4、ファイル5(tag)、ファイル6、...

フォルダ2---ファイル1、ファイル2、ファイル3、ファイル4、ファイル5、ファイル6、...

フォルダ3---ファイル1、ファイル2、ファイル3(tag)、ファイル4、ファイル5、ファイル6、...


この時、フォルダ1と3に指定したtagを自動的に付けたいということです。

よろしくお願いします。

Mac Pro

投稿日 2025/09/27 10:55

返信
スレッドに付いたマーク ランキングトップの返信

投稿日 2025/09/27 18:40

ほんとすみません。リセットできてませんでした。

リセット部分1行足しました。これではどうでしょう?


また、Automatorで実行したいということで、それ用に書き換えました。(アプリケーション形式で作成保存してください)

添付画像のように“Finder項目にフィルタを適用”でフォルダのみに絞るのと“Applescriptを実行”で下記コードを入力し保存→アプリにドロップしてみるといけますかね..



use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property targTag : "レッド"

on run {input, parameters}
	
	set aList to {}
	repeat with i in input
		tell (contents of i)
			set aRes to getFilePathListWithResolveAlias(it) of me
			repeat with ii in aRes
				tell (contents of ii)
					if targTag is in (getTagsForPath(it as alias) of me) then
						set the end of aList to true
					else
						set the end of aList to false
					end if
				end tell
			end repeat
			if true is in aList then
				set bRes to getTagsForPath(it) of me
				addTagsForPath(bRes & targTag, it) of me
			end if
		end tell
		set aList to {} -- リセット
	end repeat
	
	return input
end run

--指定したフォルダの第一階層にあるアイテムのパスを返す
on getFilePathListWithResolveAlias(aFol)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of aFol)
	# 指定フォルダの直下のファイルを取得
	set filePaths to current application's NSFileManager's defaultManager's ¬
		contentsOfDirectoryAtURL:aURL ¬
			includingPropertiesForKeys:{current application's NSURLNameKey} ¬
			options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
			|error|:(missing value)
	set fList to filePaths as list
	if fList = {} then return {}
	return fList
end getFilePathListWithResolveAlias

-- get the tags
on getTagsForPath(anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags = missing value then return {} -- because when there are none, it returns missing value
	return theTags as list
end getTagsForPath

-- set the tags, replacing any existing
on setTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTagsForPath

-- add to existing tags
on addTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	-- get existing tags
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags ≠ missing value then -- add new tags
		set tagList to (theTags as list) & tagList
		set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
	end if
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTagsForPath


返信: 9
スレッドに付いたマーク ランキングトップの返信

2025/09/27 18:40 capy77 への返信

ほんとすみません。リセットできてませんでした。

リセット部分1行足しました。これではどうでしょう?


また、Automatorで実行したいということで、それ用に書き換えました。(アプリケーション形式で作成保存してください)

添付画像のように“Finder項目にフィルタを適用”でフォルダのみに絞るのと“Applescriptを実行”で下記コードを入力し保存→アプリにドロップしてみるといけますかね..



use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property targTag : "レッド"

on run {input, parameters}
	
	set aList to {}
	repeat with i in input
		tell (contents of i)
			set aRes to getFilePathListWithResolveAlias(it) of me
			repeat with ii in aRes
				tell (contents of ii)
					if targTag is in (getTagsForPath(it as alias) of me) then
						set the end of aList to true
					else
						set the end of aList to false
					end if
				end tell
			end repeat
			if true is in aList then
				set bRes to getTagsForPath(it) of me
				addTagsForPath(bRes & targTag, it) of me
			end if
		end tell
		set aList to {} -- リセット
	end repeat
	
	return input
end run

--指定したフォルダの第一階層にあるアイテムのパスを返す
on getFilePathListWithResolveAlias(aFol)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of aFol)
	# 指定フォルダの直下のファイルを取得
	set filePaths to current application's NSFileManager's defaultManager's ¬
		contentsOfDirectoryAtURL:aURL ¬
			includingPropertiesForKeys:{current application's NSURLNameKey} ¬
			options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
			|error|:(missing value)
	set fList to filePaths as list
	if fList = {} then return {}
	return fList
end getFilePathListWithResolveAlias

-- get the tags
on getTagsForPath(anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags = missing value then return {} -- because when there are none, it returns missing value
	return theTags as list
end getTagsForPath

-- set the tags, replacing any existing
on setTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTagsForPath

-- add to existing tags
on addTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	-- get existing tags
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags ≠ missing value then -- add new tags
		set tagList to (theTags as list) & tagList
		set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
	end if
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTagsForPath


2025/09/27 12:08 capy77 への返信

おはようございます。

何かしらのスクリプトなどを記述して実行すると時短になりそうな気がしますが..

(他にももっといい方法があるかもしれませんが...)


そこで、こんなAppleScriptはいかがでしょう。


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property targTag : "レッド"

set theseFolders to choose folder with multiple selections allowed

set aList to {}
repeat with i in theseFolders
	tell (contents of i)
		set aRes to getFilePathListWithResolveAlias(it) of me
		repeat with ii in aRes
			tell (contents of ii)
				if targTag is in (getTagsForPath(it as alias) of me) then
					set the end of aList to true
				else
					set the end of aList to false
				end if
			end tell
		end repeat
		if true is in aList then
			set bRes to getTagsForPath(it) of me
			addTagsForPath(bRes & targTag, it) of me
		end if
	end tell
end repeat


--指定したフォルダの第一階層にあるアイテムのパスを返す
on getFilePathListWithResolveAlias(aFol)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of aFol)
	# 指定フォルダの直下のファイルを取得
	set filePaths to current application's NSFileManager's defaultManager's ¬
		contentsOfDirectoryAtURL:aURL ¬
			includingPropertiesForKeys:{current application's NSURLNameKey} ¬
			options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
			|error|:(missing value)
	set fList to filePaths as list
	if fList = {} then return {}
	return fList
end getFilePathListWithResolveAlias

-- get the tags
on getTagsForPath(anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags = missing value then return {} -- because when there are none, it returns missing value
	return theTags as list
end getTagsForPath

-- set the tags, replacing any existing
on setTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTagsForPath

-- add to existing tags
on addTagsForPath(tagList, anAlias)
	set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of anAlias)
	-- get existing tags
	set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
	if theTags ≠ missing value then -- add new tags
		set tagList to (theTags as list) & tagList
		set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
	end if
	aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTagsForPath


スクリプトエディタにペーストして、構文確認からの名前をつけて保存で。

スクリプト実行するとフォルダの選択が求められる(フォルダのみ選択可)ので、処理したいフォルダを選択して実行。

スクリプトによって選択したフォルダを順にループ処理が走ります。そのうちの一つずつに対し、第一階層にあるフォルダ・ファイルのタグを検索し、ターゲットのTagが付与されたもの、そうでないもののFlagを立て、そのタグが含まれているものがあれば、最終的にフォルダに対してTagが追加されます。

こちらで動作確認した限りではそれなりに動いていると思いますが、ひとまずテストフォルダ・ファイルでお試しされます?

2025/09/27 12:32 capy77 への返信

そういうのに特化した?アプリとかあるのかもしれませんが、Tagの取得と設定が含まれてますからねー。

スクリプトの実行が簡潔かと。

と言いますか、私自身他に知らないので私の答えはそうなってしまいますが..


スクリプト、最初は分からないと思いますが簡単に実行できますよ。

テストはごゆっくりどうぞ。


2025/09/27 15:15 vz.r への返信

済みませんが、問題が発覚してしまいました。


例えばフォルダ1〜3の3個に対して実行した場合、フォルダ1に赤タグのファイルがあると、フォルダ1に赤タグが付くのは良いのですが、フォルダ2と3にも問答無用で赤タグが付いてしまいます。フォルダ1にはなく、フォルダ2内に赤タグファイルがあると、フォルダ3には問答無用で赤タグが付いてしまいます。


つまり、赤タグファイルが見つかったフォルダ以降のフォルダには、全て赤タグが付いてしまうようです。Scriptはさっぱりなのですが、前のフォルダのタグ情報を引きずっているような感じなので、毎回、タグ情報を初期化するとかで対応できませんでしょうか?


あと、automaterだと、実行形式で保存すると、そのアイコンにdrag & dropすることで実行できて便利なのですが、scriptではそういう設定は出来ないのでしょうか?


急ぎはしませんので、済みませんが、よろしくお願いします。

2025/09/27 12:33 capy77 への返信

さすがにここまで準備して頂けると、直ぐに実行確認できました。

動作も問題なく、処理速度も速いため、十分使えそうです。ありがとうございます。


スクリプトはあまりなじみがないので、automaterで何とかならないかと少しやってみていたのですが、やはり自由度ではかないませんね。



2025/09/27 12:38 capy77 への返信

良かった^^


Automatorでできることならその方が簡単ですね!

スクリプトでもできないことはあるんですが、こちらの方が自由度高いですし、Automatorでも複雑なことしようと思うと結局何かしらのスクリプトが必要だったり?

作成も実行もAutomatorの方が簡単なんですけどね!

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

Tagの条件付き自動付与

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。