How to make Folder Action to watch a folder and if it sees 2+ files with same filename and diff ext, create new folder and move those files in folder

huangapple go评论66阅读模式
英文:

How to make Folder Action to watch a folder and if it sees 2+ files with same filename and diff ext, create new folder and move those files in folder

问题

I've translated the content as requested:

我已经搜索了好几个星期,但在我的运行 Ventura 的 Mac 上找不到解决这个问题的方法。

我有很多文件,它们具有相同的文件名,并以不同的扩展名保存在同一个文件夹中。我正在寻找一个作为文件夹操作使用的脚本,该脚本将执行以下操作。

  1. 监视文件夹并将其用作由脚本驱动的文件夹操作的拖放区。
  2. 如果文件夹中看到具有相同文件名的 2 个或更多文件,则在工作文件夹内创建一个子文件夹,随后;
  3. 将具有相同名称但不同扩展名的 2 个或更多文件移动到新创建的子文件夹中。

示例;

监视的文件夹位于我的主文件夹中,名为“Work”。

Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif
Green Bird.png
Green Bird.jpg
Lizard Jumping.mkv

理想情况下,如果将其作为文件夹操作在名为“work”的文件夹上运行,它应该返回以下结果;

/users/blaine/work/Example Print/
Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt

/users/blaine/work/Weird Logo 3454/
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif

/users/blaine/work/Green Bird/
Green Bird.png
Green Bird.jpg

/users/blaine/work/Lizard Jumping.mkv

当打开名为“work”的文件夹时,应该看起来像这样。

/users/blaine/work/Example Print/
/users/blaine/work/Weird Logo 3454/
/users/blaine/work/Green Bird/
/users/blaine/work/Lizard Jumping.mkv

有任何想法吗?我已经成功创建了一个允许我运行“快速操作”的快捷方式,当排序并手动突出显示文件以创建新文件夹并自动以文件名命名新文件夹时,但有数千个文件,这使我相信一定有更好的方法。感谢任何帮助。

这些是我能找到的关于此主题的最接近的帖子,但对我来说有点不同以至于无法解析和使其工作。

https://stackoverflow.com/a/57465078/21894037

https://stackoverflow.com/q/14866051/21894037


供参考,这是我目前已经转化为快捷操作或用作文件夹操作的脚本。它运行得很好,只是需要手动扫描通常约有 10-20k 个文件。

tell application "Finder"
    activate
    set selectedFiles to selection as alias list
    set containingFolder to container of (item 1 of selectedFiles)
    repeat with i from 1 to count of selectedFiles
        set foldersRef to (a reference to folders of containingFolder)
        set foldersRefItems to name of (contents of foldersRef)
        set thisItem to item i of selectedFiles
        set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
        if fileName is not in foldersRefItems then
            move thisItem to (make new folder at containingFolder with properties {name:fileName})
        else
            move thisItem to folder fileName of containingFolder
        end if
    end repeat
end tell
英文:

I've been searching for weeks and have been unsucessful on finding a way on my Mac running Ventura to help solve this wish.

I have a lot of files that have the same file name and are saved with different extensions and all exist in the same folder. I am looking for a script to use as a folder action that will do the following.

  1. Watch a folder and use it as a drop zone with a folder action powered by a script.
  2. If the folder sees 2 or more files with the same file name, create a subfolder inside of the working folder, and subsequently;
  3. Move files that have 2 or more with the same name but different extensions into the newly created subfolder.

Example;

Watched Folder Location is in my home folder named "Work".

Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif
Green Bird.png
Green Bird.jpg
Lizard Jumping.mkv

Ideally, it would return the following results if run as a folder action on the folder "work" where all of these items reside;

/users/blaine/work/Example Print/
Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt

/users/blaine/work/Weird Logo 3454/
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif

/users/blaine/work/Green Bird/
Green Bird.png
Green Bird.jpg

/users/blaine/work/Lizard Jumping.mkv

Which should look like this when the folder "work" is opened.

/users/blaine/work/Example Print/
/users/blaine/work/Weird Logo 3454/
/users/blaine/work/Green Bird/
/users/blaine/work/Lizard Jumping.mkv

Any ideas? I have managed to create a shortcut that allows me to run a "quick action" and I can manually highlight files when sorted and create new folder from selection and automatically name the new folder the same as the file name without the extension, but there are thousands which makes me believe there must be a better way. Any help is appreciated.

These are the closest threads I was able to find on this topic, but they're a little too different for me to dissect and make work.

https://stackoverflow.com/a/57465078/21894037

https://stackoverflow.com/q/14866051/21894037


For Reference, this is the script I have currently turned into a quick-action or use as a folder action. It works great except for having to manually scan through usually around 10-20k files.

tell application "Finder"
	activate
	set selectedFiles to selection as alias list
	set containingFolder to container of (item 1 of selectedFiles) --as alias
	repeat with i from 1 to count of selectedFiles
		set foldersRef to (a reference to folders of containingFolder)
		set foldersRefItems to name of (contents of foldersRef)
		set thisItem to item i of selectedFiles
		set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
		if fileName is not in foldersRefItems then
			move thisItem to (make new folder at containingFolder ¬
				with properties {name:fileName})
		else
			move thisItem to folder fileName of containingFolder
		end if
	end repeat
end tell

答案1

得分: 0

以下是您要翻译的内容:

尝试这个。添加到热门(观察)文件夹的文件将被移动到热门文件夹的适当子文件夹中。

在将文件夹项目添加到此文件夹后,收到这些项目
将此文件夹的HFS路径设置为thisFolder作为文本 - 此观察文件夹的HFS路径
告诉“Finder”应用程序将所有文件设置为(此文件夹作为文本的文件夹的文件)作为别名列表
获取所有基本名称列表
将基本名称设置为空
重复处理所有文件
将POSIX路径的anItem的基本名称添加到基本名称的末尾
结束重复处理
重复处理这些项目的anItem
设置文件的baseName为我的getBaseName(anItem的POSIX路径) - 文件的baseName
告诉“Finder”应用程序
尝试
- 如果适当的子文件夹存在,那么确保文件应该移动到其中
将文件(anItem作为文本)移动到subFolder
end try
on error - 如果没有适当的子文件夹,那么
设置aCount为0
重复处理basenames中的b
如果baseName是b的内容,则将aCount设置为aCount + 1
结束重复处理
如果aCount > 1 then - 如果有重复的基本名称,那么文件应该移动
在thisFolder处使用属性{name: baseName}创建新的文件夹
尝试
将文件(anItem作为文本)移动到subFolder
end try
end if
end try
end tell
结束重复处理
end adding folder items to

on getBaseName(posixPath)
设置{ATID,AppleScript的文本项分隔符}为{AppleScript的文本项分隔符,"/"}
如果(posixPath以"/"结尾)则
设置baseName为posixPath的倒数第2个文本项
否则
设置baseName为posixPath的最后一个文本项
end if
如果(baseName包含".")则
设置AppleScript的文本项分隔符为"."
设置baseName为基本名称的文本项1到文本项-2
end if
设置AppleScript的文本项分隔符为ATID
返回baseName
end getBaseName

英文:

Try this. Added to hot (watch) folder file(s) will be moved into the hot folder's appropriate subfolder(s).

on adding folder items to thisFolder after receiving these_items
	set thisFolderHFSPath to thisFolder as text -- this watch folder's HFS path
	tell application "Finder" to set allFiles to (files of folder (thisFolder as text)) as alias list
	-- get all basenames list
	set basenames to {}
	repeat with anItem in allFiles
		set end of basenames to my getBaseName(POSIX path of anItem)
	end repeat
	repeat with anItem in these_items
		set baseName to my getBaseName(POSIX path of anItem) -- baseName of file
		tell application "Finder"
			try
				-- if the appropriate subfolder exists then sure the file should be moved in it
				set subFolder to folder (thisFolderHFSPath & baseName)
				try -- required try block here   
					move file (anItem as text) to subFolder
				end try
			on error -- if no appropriate subfolder, then 
				set aCount to 0
				repeat with b in basenames
					if baseName is (contents of b) then set aCount to aCount + 1
				end repeat
				if aCount > 1 then -- the file should be moved if duplicate basename
					set subFolder to make new folder at thisFolder with properties {name:baseName}
					try -- required try block here   
						move file (anItem as text) to subFolder
					end try
				end if
			end try
		end tell
	end repeat
end adding folder items to

on getBaseName(posixPath)
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	if (posixPath ends with "/") then
		set baseName to text item -2 of posixPath
	else
		set baseName to text item -1 of posixPath
	end if
	if (baseName contains ".") then
		set AppleScript's text item delimiters to "."
		set baseName to text 1 thru text item -2 of baseName
	end if
	set AppleScript's text item delimiters to ATID
	return baseName
end getBaseName

huangapple
  • 本文由 发表于 2023年5月14日 04:09:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定