applescript droplet:为文件名添加后缀

applescript droplet:为文件名添加后缀,applescript,file-rename,suffix,Applescript,File Rename,Suffix,我刚刚发现了Applescript并开始利用它的潜力。我已经为这个问题研究了两天了,但是没有任何效果!。。下面是一个applescript droplet,它通过程序处理文件夹中存放的jpg文件,然后将生成的文件移动到新的子文件夹中。我是编程新手,我的技能非常有限。我试图在最终处理的文件中添加后缀,但我没有办法!这是代码 on adding folder items to this_folder after receiving these_items set project to (this_f

我刚刚发现了Applescript并开始利用它的潜力。我已经为这个问题研究了两天了,但是没有任何效果!。。下面是一个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
我非常感谢您能为每个处理过的文件(在“完成”文件夹中)添加后缀(“\u edited”例如)

提前谢谢


Josh

下面的脚本将后缀添加到所选文件中。它必须添加在您的“图像处理”组之后,并在结束之前重复

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

现在你的要求更清楚了。为此,我们需要文件夹操作中的两个脚本:

第一个脚本附加到文件夹A。它检查文件夹“完成”是否存在,如果不存在,则创建“完成”文件夹,并附加一个名为“完成”的新文件夹操作

然后,该脚本可以拾取放置在中的每个文件,并将其发送到图像处理应用程序

第二个脚本将是folder Done的文件夹操作。 它处理添加到“完成”文件夹中的每个文件(来自图像处理应用程序)以更改其名称,并添加后缀“\u edited”

要使整个流程正常运行,必须执行以下操作:

1) 在下面的第一个脚本中,添加有关process image应用程序的说明(我没有这样做,因为我无法测试它!)

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”的action script文件夹中(它必须与第一个脚本中使用的名称相同)

在测试过程中,我遇到了麻烦,因为每次我更改done文件夹中的文件名时,脚本都会再次运行

我认为这是苹果在《El Captain》上的一个错误(我在《雪豹》上没有这个问题)。为了解决这个问题,我在更改文件名之前添加了一个测试,以检查文件名是否已经包含sufix“\u edited”。如果有,我就跳过

其结果是,在将文件放入父文件夹A之前,文件不应包含“\u edited”


所有测试都正常(除了图像处理本身)。

非常感谢@pbell!。。。您的代码经过一些尝试后运行良好。起初,代码(对于第一滴,处理文件的代码)对我不起作用。。。它什么也没做。然后我决定更改代码的这一部分:

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 Capitan的事情,而我仍然在经营小牛队(我知道…),所以也许这与

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

所以再次感谢@pbell,你是个天才,现在我学到了多一点(尽管仍然很笨)。。。很快,我将需要制作另一个applescript来将特定文件从不同的子文件夹移动到父文件夹,一旦有了父文件夹,就尝试将它们全部压缩。。。这是我的下一个挑战,但我认为这将比这个容易得多:)
我非常感激,我希望这能帮助其他人

非常感谢pbell,你的代码启发了很多,我认为你几乎解决了这个问题。我尝试了你的代码,但我一直得到一个循环,外部应用程序在其中打开一个文件(每次都附加一个新的“_edited”后缀)。我相信问题在于你的代码重命名了源文件,但没有重命名最终处理的文件(在文件夹“Done”中),因此droplet理解一个新文件进入主文件夹并不断生成新文件。这很愚蠢,但我甚至不知道如何更改每个文件的名称(这就是重点)对于生成的文件…有什么建议吗?非常感谢!我要帮助你的困难是我不知道你的第三方应用程序做什么以及它是如何做的。如果该应用程序将结果发布到“完成”文件夹中的新文件中,可能是你可以使用为文件夹“完成”定义的新文件夹操作脚本中的重命名说明?谢谢@Pbell,你说得对!应用程序只需将你删除的文件进行处理,并将它们以相同的名称和分机保存到“完成”文件夹。因此,我想更改分机。我考虑了你的方法,但没有成功地将所有文件删除到“完成”文件夹中的过程自动化文件夹。我尝试了以下方法:
set fileN to item I of items in destination\u path
在代码中使用“fileN”而不是“This\u item”,但它不起作用……重命名指令会自动应用于“Done”文件夹中添加后缀的任何文件吗?Thnks&对不起,我太笨了!总之,你想做什么:1)将文件F.ext放入文件夹A(path=“A/F.ext”)。2) 它触发一个脚本,该脚本在文件夹a(path=“a/Done”)中创建文件夹Done,并将文件F.ext发送到应用程序。3) 此应用程序处理该文件并将结果存储在名为F.ext(path=A/Done/F.ext)的文件夹Done中。我不清楚的最后一件事是后缀。我知道您想将“A/F.ext”更改为“A/F_edited.ext”。那么我的问题是:你怎么知道申请已经处理了它?只是因为现在你有一个
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