(这是我之前的一个问题的新编辑,获得了-3票。希望这个新的有更好的资格)
我需要创建一个Automator服务来将大量文件组织到文件夹中。我使用插画家和每个.ai文件我创建了3种格式:[name.pdf],[name BAJA.jpg]和[name.jpg],总共有4个文件
我的问题是,在本周我将这个过程重复到90多个不同的.ai文件。所以90个文件* 4是360个独立的文件,都进入了一些项目文件夹。
我想将所有4个相关文件抓取到一个文件夹中,并将文件夹名称设置为与.ai文件相同。
由于所有文件名都相同(除了一个),我想告诉查找器抓取所有具有相同名称的文件,复制名称,创建文件夹并将这些文件放入其中,但我有一个文件名variant [name] LOW.jpg]也许我可以告诉脚本将这项工作剥离为例外。
这样我就将所有4个文件统一到一个文件夹中。先感谢您
更新:此问题最初发布于2013年,现在我有了解决方案。人们帮我组装这个脚本以满足我的需求。
我将此作为服务添加并在MacOs上分配了键盘快捷键。这是代码:
on run {input, parameters} -- create folders from file names and move
set output to {} -- this will be a list of the moved files
repeat with anItem in the input -- step through each item in the input
set {theContainer, theName, theExtension} to (getTheNames from anItem)
try
# check for a suffix and strip it off for the folder name
if theName ends with " BAJA" then
set destination to (makeNewFolder for (text 1 thru -6 of theName) at theContainer)
else
set destination to (makeNewFolder for theName at theContainer)
end if
tell application "Finder"
move anItem to destination
set the end of the output to the result as alias -- success
end tell
on error errorMessage -- duplicate name, permissions, etc
log errorMessage
# handle errors if desired - just skip for now
end try
end repeat
return the output -- pass on the results to following actions
end run
to getTheNames from someItem -- get a container, name, and extension from a file item
tell application "System Events" to tell disk item (someItem as text)
set theContainer to the path of the container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
set theExtension to "." & theExtension
end if
return {theContainer, theName, theExtension}
end getTheNames
to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist
set theParent to theParent as text
if theParent begins with "/" then set theParent to theParent as POSIX file as text
try
return (theParent & theChild) as alias
on error errorMessage -- no folder
log errorMessage
tell application "Finder" to make new folder at theParent with properties {name:theChild}
return the result as alias
end try
end makeNewFolder
希望这可以帮助。
您也可以将其保存为script.sh
(在纯文本模式下的TextEdit中)并在终端中使用bash script.sh
运行它:
cd ~/Target\ Folder/
for f in *.ai *.pdf *.jpg; do
dir=${f%.*}
dir=${dir% LOW}
mkdir -p "$dir"
mv "$f" "$dir"
done
遗憾的是,我个人喜欢回答这些问题,因为这有助于我练习和提高自己的技能。
感谢您发布解决方案。我认为这是一个很好的姿态,其他人会发现它很有用。
此脚本比使用“系统事件”而不是“Finder”短一些,因此对于大量文件会更快:
set IllustratorOutputFolder to "/Users/CK/Desktop/example"
tell application "System Events" to ¬
set ai_files to every file in folder IllustratorOutputFolder ¬
whose name extension is "ai"
set Output to {}
repeat with ai_file in ai_files
set AppleScript's text item delimiters to "."
get name of ai_file
get text items of result
set basename to reverse of rest of reverse of result as text
tell application "System Events"
get (every file in folder IllustratorOutputFolder ¬
whose name begins with basename)
move result to (make new folder ¬
in folder IllustratorOutputFolder ¬
with properties {name:basename})
end tell
set end of Output to result
end repeat
return Output -- list of lists of moved files
只是另一种做事方式。不是它更好或更糟,而是一个不同的解决方案。