Search code examples
applescriptfile-renamesuffix

applescript droplet: Add a suffix to filename


I just discovered Applescript and started using its potential. I've been researching for a solution for this for 2 days but nothing worked!.. Here is an applescript droplet that processes the deposited jpg files in a folder through a program and then moves the resulting files to a new subfolder. I'm new to programming and my skills are very limited. I was trying to add a suffix to the final processed file, but there's no way I could! This is the code.

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

I'd much appreciate any help to add the suffix ("_edited" for example) to each processed file (in "Done" folder)

Thanks in advance!

Josh


Solution

  • Now your requirements are more clear. To do this, we need 2 scripts in folder actions :

    The first script is attached to folder A. It checks if folder done exists and, if not, it creates Done folder, attaching also a new folder action called Done_Action.

    Then this script can pick every file dropped in A, and send it to your image process application.

    The second script is the one which will be the folder action of folder Done. It processes each file added in Done folder (from your image process application) to change its name, adding the suffix "_edited".

    To make full process operational, you must do the following:

    1) Add, in the first script bellow, your instructions about your process image application (I did not because I can't test this !)

    2) save this script in your Folder Action scripts (name does not matter) and attached your parent folder (A) to this script :

    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
    

    As you can see, when the folder Done is created, the script attaches to it the folder action with script 'Done_Action.scpt'.

    3) save this second script bellow into you folder action script with the name 'Done_Action.Scpt' (it must be same as name used in first script)

    -- 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
    

    During my test, I have been in trouble, because each time I was changing the file name in the done folder, the script was run again.

    I think this is a bug from Apple on El Captain (I did not had that issue on Snow Leopard). To work around that, I added a test before changing the file name, to check if name already contains the sufix '_edited'. if it does, then I skip.

    The consequence is that your files should never contains '_edited' before you drop them in parent folder A !

    All tested and OK (except still the image process itself).