applescript droplet:为文件名添加后缀

时间:2016-02-17 20:31:50

标签: applescript file-rename suffix

我刚刚发现了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

我非常感谢为每个已处理的文件添加后缀(例如“_edited”)的任何帮助(在“完成”文件夹中)

提前致谢!

约什

3 个答案:

答案 0 :(得分:1)

现在您的要求更加清晰。为此,我们在文件夹操作中需要2个脚本:

第一个脚本附加到文件夹A.它检查文件夹是否存在,如果没有,则创建Done文件夹,同时附加一个名为Done_Action的新文件夹操作。

然后,此脚本可以选择A中删除的每个文件,并将其发送到您的图像处理应用程序。

第二个脚本是文件夹Done的文件夹操作。 它处理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文件夹中更改文件名时,脚本都会再次运行。

我认为这是来自El Captain的Apple的一个错误(我在Snow Leopard上没有这个问题)。为了解决这个问题,我在更改文件名之前添加了一个测试,以检查名称是否已包含sufix“_edited”。如果是,那么我跳过。

结果是你的文件在将它们放到父文件夹A中之前永远不应该包含'_edited'!

所有测试和OK(除了图像处理本身)。

答案 1 :(得分:0)

下面的脚本会为您选择的文件添加后缀。必须在告诉“图像处理”块之后,在结束重复之前添加它。

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 :(得分:0)

非常感谢@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)为什么变化后它会变得很好。

你提到了一些关于ElCapitán的事情,我还在经营小牛队(我知道......)所以也许它有事可做......

那么,实际上它没有立即开始工作之后,经过几次尝试在完全开始工作时删除/删除文件(在A / F.ext中)。我不知道为什么会发生这种情况,我想它与文件系统结构有关:我使用Pathfinder并启用了隐藏文件,并注意到隐藏文件“.DS_Store”出现在两个文件夹中(A&完成一切都开始正常工作。

再次感谢@pbell,你是一个天才,现在我学到了更多(虽然仍然愚蠢)...很快我需要制作另一个AppleScript,将特定文件从不同的子文件夹移动到父文件夹并且一旦尝试压缩它们......那是我的下一个挑战,但我认为它会比这个更容易:)    我非常感激,希望这有助于其他人!...