applescript droplet:为文件名添加后缀
applescript droplet: Add a suffix to filename
我刚刚发现 Applescript 并开始发挥其潜力。我已经为此研究了 2 天的解决方案,但没有任何效果!.. 这是一个 applescript droplet,它通过程序处理文件夹中存放的 jpg 文件,然后将生成的文件移动到新的子文件夹。我是编程新手,我的技能非常有限。我试图为最终处理的文件添加后缀,但我做不到!这是代码。
on adding folder items to this_folder after receiving these_items
set project to (this_folder as text) & "processor.imprc" -- This is the name of the file that calls the external application
set done_folder to "Done"
tell application "Finder"
if not (exists folder done_folder of this_folder) then
make new folder at this_folder with properties {name:done_folder}
end if
set the destination_folder to folder done_folder of this_folder as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder and the name extension of the item_info is "jpg" then
set the item_path to the POSIX path of this_item
set the destination_path to the destination_directory & (name of the item_info) -- Here is where I think it defines the name of the "produced" file
tell application "Image Process" -- From this point on commands for the external application start
activate
open project
tell first document
set source to item_path
impose in destination_path
close saving no
end tell
end tell
end if
end repeat
end adding folder items to
如果能帮助我为每个已处理的文件(在 "Done" 文件夹中)添加后缀(例如“_edited”),我将不胜感激
提前致谢!
乔希
下面的脚本将后缀添加到您选择的文件。它必须在你的 tell "Image Process" bloc 之后和结束 repeat 之前添加。
set This_item to choose file -- to be delete, this is just to test this script alone
set Sufix to "_edited"
tell application "Finder"
set Ext to name extension of This_item
set FileName to name of This_item
set NameOnly to text 1 thru ((offset of Ext in FileName) - 2) of FileName -- get name without .extension
set name of This_item to (NameOnly & Sufix & "." & Ext) -- change the name of the file
end tell
现在你的要求更明确了。为此,我们需要文件夹操作中的 2 个脚本:
第一个脚本附加到文件夹 A。它检查文件夹 done 是否存在,如果不存在,则创建 Done 文件夹,同时附加一个名为 Done_Action 的新文件夹操作。
然后这个脚本可以选择 A 中放置的每个文件,并将其发送到您的图像处理应用程序。
第二个脚本将是文件夹完成的文件夹操作。
它处理添加到 Done 文件夹(来自您的图像处理应用程序)中的每个文件以更改其名称,添加后缀“_edited”。
要使整个过程可操作,您必须执行以下操作:
1) 在下面的第一个脚本中添加有关过程映像应用程序的说明(我没有这样做是因为我无法测试它!)
2) 将此脚本保存在您的文件夹操作脚本中(名称无关紧要)并将您的父文件夹 (A) 附加到此脚本:
on adding folder items to this_folder after receiving these_items
set project to (this_folder as text) & "processor.imprc" -- This is the name of the file that calls the external application
set Done_Folder to "Done"
set Done_Script to "Done_Action.scpt"
-- this part creates Done folder if not exist and attached new folder action script Done_Script to Done folder.
tell application "Finder"
if not (exists folder Done_Folder of this_folder) then
make new folder at this_folder with properties {name:Done_Folder}
tell application "System Events"
make new folder action at end of folder actions with properties {enabled:true, name:Done_Script, path:(this_folder as string) & Done_Folder}
tell folder action Done_Script to make new script at end of scripts with properties {name:Done_Script}
end tell
end if
set the destination_folder to folder Done_Folder of this_folder as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat This_item in these_items times -- loop on each added item in parent folder
set the item_info to info for This_item
if This_item is not the destination_folder and the name extension of the item_info is "jpg" then
set the item_path to the POSIX path of This_item
set the destination_path to the destination_directory & (name of the item_info)
-- add here your Image Process script (I can't test it !)
end if
end repeat
end adding folder items to
如您所见,创建文件夹 Done 时,脚本会使用脚本 'Done_Action.scpt'.
附加文件夹操作
3) 将下面的第二个脚本保存到您的文件夹操作脚本中,名称为 'Done_Action.Scpt'(必须与第一个脚本中使用的名称相同)
-- this script is trigger when file is added into the done folder
on adding folder items to this_folder after receiving these_items
set Sufix to "_edited"
repeat with This_Item in these_items
tell application "Finder"
set Ext to name extension of This_Item
set FileName to name of This_Item
if (offset of Sufix in FileName) = 0 then
set NameOnly to text 1 thru ((offset of Ext in FileName) - 2) of FileName -- get name without .extension
set name of This_Item to (NameOnly & Sufix & "." & Ext) -- change the name of the file
end if
end tell
end repeat
end adding folder items to
在我测试的过程中,我遇到了麻烦,因为每次我在更改done文件夹中的文件名时,脚本又是运行。
我认为这是 Apple 在 El Captain 上的错误(我在 Snow Leopard 上没有这个问题)。为了解决这个问题,我在更改文件名之前添加了一个测试,以检查名称是否已经包含后缀“_edited”。如果是,那么我跳过。
结果是,在将文件放入父文件夹 A 之前,您的文件不应包含“_edited”!
所有测试都正常(除了图像处理本身)。
非常感谢@pbell!...经过一些尝试,您的代码运行良好。起初代码(对于第一个 droplet,处理文件的代码)对我不起作用……它什么也没做。然后我决定更改您的这部分代码:
repeat This_item in these_items times -- loop on each added item in parent folder
对于另一个:
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items -- loop on each added item in parent folder
说实话,我不是很了解
1)为什么我改变了它。
2) 为什么修改后可以正常工作了
你提到了一些关于 El Capitán 的事情,我仍然是 运行 小牛队(我知道......)所以也许它有一些事情要做......
那么,在更改之后它实际上并没有立即开始工作,而是在尝试 dropping/deleting 文件(在 A/F.ext 中)之后才开始完美工作。我不知道为什么会这样,我想这与文件系统结构有关:我使用 Pathfinder 并启用了隐藏文件,并注意到它是在隐藏文件“.DS_Store”出现在两个文件夹之后(A & 完成)当一切开始正常工作时。
所以再次感谢@pbell,你是个天才,现在我学到了更多(虽然仍然很愚蠢)......很快我需要制作另一个applescript来将特定文件从不同的子文件夹移动到父级,然后尝试将它们全部压缩......这是我的下一个挑战,但我认为它会比这个容易得多 :)
我非常感激,我希望这对其他人有帮助!...
我刚刚发现 Applescript 并开始发挥其潜力。我已经为此研究了 2 天的解决方案,但没有任何效果!.. 这是一个 applescript droplet,它通过程序处理文件夹中存放的 jpg 文件,然后将生成的文件移动到新的子文件夹。我是编程新手,我的技能非常有限。我试图为最终处理的文件添加后缀,但我做不到!这是代码。
on adding folder items to this_folder after receiving these_items
set project to (this_folder as text) & "processor.imprc" -- This is the name of the file that calls the external application
set done_folder to "Done"
tell application "Finder"
if not (exists folder done_folder of this_folder) then
make new folder at this_folder with properties {name:done_folder}
end if
set the destination_folder to folder done_folder of this_folder as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder and the name extension of the item_info is "jpg" then
set the item_path to the POSIX path of this_item
set the destination_path to the destination_directory & (name of the item_info) -- Here is where I think it defines the name of the "produced" file
tell application "Image Process" -- From this point on commands for the external application start
activate
open project
tell first document
set source to item_path
impose in destination_path
close saving no
end tell
end tell
end if
end repeat
end adding folder items to
如果能帮助我为每个已处理的文件(在 "Done" 文件夹中)添加后缀(例如“_edited”),我将不胜感激
提前致谢!
乔希
下面的脚本将后缀添加到您选择的文件。它必须在你的 tell "Image Process" bloc 之后和结束 repeat 之前添加。
set This_item to choose file -- to be delete, this is just to test this script alone
set Sufix to "_edited"
tell application "Finder"
set Ext to name extension of This_item
set FileName to name of This_item
set NameOnly to text 1 thru ((offset of Ext in FileName) - 2) of FileName -- get name without .extension
set name of This_item to (NameOnly & Sufix & "." & Ext) -- change the name of the file
end tell
现在你的要求更明确了。为此,我们需要文件夹操作中的 2 个脚本:
第一个脚本附加到文件夹 A。它检查文件夹 done 是否存在,如果不存在,则创建 Done 文件夹,同时附加一个名为 Done_Action 的新文件夹操作。
然后这个脚本可以选择 A 中放置的每个文件,并将其发送到您的图像处理应用程序。
第二个脚本将是文件夹完成的文件夹操作。 它处理添加到 Done 文件夹(来自您的图像处理应用程序)中的每个文件以更改其名称,添加后缀“_edited”。
要使整个过程可操作,您必须执行以下操作:
1) 在下面的第一个脚本中添加有关过程映像应用程序的说明(我没有这样做是因为我无法测试它!)
2) 将此脚本保存在您的文件夹操作脚本中(名称无关紧要)并将您的父文件夹 (A) 附加到此脚本:
on adding folder items to this_folder after receiving these_items
set project to (this_folder as text) & "processor.imprc" -- This is the name of the file that calls the external application
set Done_Folder to "Done"
set Done_Script to "Done_Action.scpt"
-- this part creates Done folder if not exist and attached new folder action script Done_Script to Done folder.
tell application "Finder"
if not (exists folder Done_Folder of this_folder) then
make new folder at this_folder with properties {name:Done_Folder}
tell application "System Events"
make new folder action at end of folder actions with properties {enabled:true, name:Done_Script, path:(this_folder as string) & Done_Folder}
tell folder action Done_Script to make new script at end of scripts with properties {name:Done_Script}
end tell
end if
set the destination_folder to folder Done_Folder of this_folder as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat This_item in these_items times -- loop on each added item in parent folder
set the item_info to info for This_item
if This_item is not the destination_folder and the name extension of the item_info is "jpg" then
set the item_path to the POSIX path of This_item
set the destination_path to the destination_directory & (name of the item_info)
-- add here your Image Process script (I can't test it !)
end if
end repeat
end adding folder items to
如您所见,创建文件夹 Done 时,脚本会使用脚本 'Done_Action.scpt'.
附加文件夹操作3) 将下面的第二个脚本保存到您的文件夹操作脚本中,名称为 'Done_Action.Scpt'(必须与第一个脚本中使用的名称相同)
-- this script is trigger when file is added into the done folder
on adding folder items to this_folder after receiving these_items
set Sufix to "_edited"
repeat with This_Item in these_items
tell application "Finder"
set Ext to name extension of This_Item
set FileName to name of This_Item
if (offset of Sufix in FileName) = 0 then
set NameOnly to text 1 thru ((offset of Ext in FileName) - 2) of FileName -- get name without .extension
set name of This_Item to (NameOnly & Sufix & "." & Ext) -- change the name of the file
end if
end tell
end repeat
end adding folder items to
在我测试的过程中,我遇到了麻烦,因为每次我在更改done文件夹中的文件名时,脚本又是运行。
我认为这是 Apple 在 El Captain 上的错误(我在 Snow Leopard 上没有这个问题)。为了解决这个问题,我在更改文件名之前添加了一个测试,以检查名称是否已经包含后缀“_edited”。如果是,那么我跳过。
结果是,在将文件放入父文件夹 A 之前,您的文件不应包含“_edited”!
所有测试都正常(除了图像处理本身)。
非常感谢@pbell!...经过一些尝试,您的代码运行良好。起初代码(对于第一个 droplet,处理文件的代码)对我不起作用……它什么也没做。然后我决定更改您的这部分代码:
repeat This_item in these_items times -- loop on each added item in parent folder
对于另一个:
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items -- loop on each added item in parent folder
说实话,我不是很了解 1)为什么我改变了它。 2) 为什么修改后可以正常工作了
你提到了一些关于 El Capitán 的事情,我仍然是 运行 小牛队(我知道......)所以也许它有一些事情要做......
那么,在更改之后它实际上并没有立即开始工作,而是在尝试 dropping/deleting 文件(在 A/F.ext 中)之后才开始完美工作。我不知道为什么会这样,我想这与文件系统结构有关:我使用 Pathfinder 并启用了隐藏文件,并注意到它是在隐藏文件“.DS_Store”出现在两个文件夹之后(A & 完成)当一切开始正常工作时。
所以再次感谢@pbell,你是个天才,现在我学到了更多(虽然仍然很愚蠢)......很快我需要制作另一个applescript来将特定文件从不同的子文件夹移动到父级,然后尝试将它们全部压缩......这是我的下一个挑战,但我认为它会比这个容易得多 :) 我非常感激,我希望这对其他人有帮助!...