Applescript for file name alteration/folder action

Discussion in 'Mac Apps and Mac App Store' started by TiffSunbacon, Oct 13, 2008.

  1. TiffSunbacon macrumors newbie

    Joined:
    Oct 3, 2008
    #1
    Is there a script that will add the name of the folder to the beginning of each file dropped into it? For example: if the folder is called PROJECT then a file called June Projections when dropped into it will automatically be renamed PROJECT June Projections.

    Many thanks
     
  2. ahenson macrumors newbie

    Joined:
    Oct 30, 2008
    #2
    Folder Name in File Name Script

    Here is a script that I originally pieced together and customized for JPGs. I based it on a few snippets from scripts here and there in order to figure it out, but I'm not sure which I used. I took out my JPG parts, so this will work on any file with a 3-character extension added to a folder.

    I have only tested this on Leopard, but hopefully it will work for you.

    One thing I have noticed on any "added items" folder action is that if there is a tiny delay in adding the files to the folder, the scripts may only work on the first one and then quit. I notice this when running a Save for Web action in Photoshop where I have multiple sizes saved into a folder. If I move or copy all the files at once into the actioned folder, then no problem.

    Hope this helps.





    -- begin applescript to add folder name to beginning of file name. A. Henson, amandahenson.com, compiled 2008.

    on adding folder items to this_folder after receiving added_items

    try

    repeat with i from 1 to number of items in added_items
    tell application "Finder"
    set this_item to item i of added_items
    set the item_info to the info for this_item
    set the the_file_name to the name of this_item


    set the folder_name to the name of this_folder
    set nameExt to name extension of (item i of added_items)
    set oldName to text 1 through -((count nameExt) + 2) of (get name of (item i of added_items))
    --
    set added_items_name to (folder_name as string) & " " & (oldName as string) & "." & the name extension of the item_info


    -- rename the file to the new file name
    set the name of this_item to added_items_name


    end tell
    end repeat


    end try


    end adding folder items to
     

Share This Page