1

我刚刚发现了 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”)添加到每个处理的文件(在“完成”文件夹中)

提前致谢!

乔什

4

3 回答 3

1

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

第一个脚本附加到文件夹 A。它检查文件夹 done 是否存在,如果不存在,它会创建 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

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

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

结果是您的文件在将它们放入父文件夹 A 之前绝不应该包含“_edited”!

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

于 2016-02-20T13:18:18.047 回答
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)为什么在更改后它可以正常工作。

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

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

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

于 2016-02-21T19:03:26.587 回答
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
于 2016-02-18T06:36:45.220 回答