4

Newbie question. I've created an Applescript that looks at the selected Folder name, copies the last term of the Folder name, and appends that to the beginning of the File Name of the files inside the Folder. I'd like to do choose the Folder automatically instead of manually. I'm sure it's a small change, just not sure how...

tell application "Finder"

    set selected_items to selection

    repeat with this_item in selected_items

        set current_name to name of this_item as text

        set current_extension to name extension of this_item

        set length_extension to length of current_extension

        set name_no_ext to text 1 thru (-length_extension - 2) of current_name

        --set AppleScript's text item delimiters to " "
        set AppleScript's text item delimiters to {"("}


        if (number of text items of name_no_ext > 1) then

            set z to last text item of name_no_ext --get the number at the end of the name before the extension

            --set comments of this_item to z

        end if

        set AppleScript's text item delimiters to ""

    end repeat


    repeat with this_file in (get files of (choose folder))

        set the_name to name of this_file
        set name of this_file to {"(", z, ")", " ", the_name} as string

    end repeat

end tell
2
  • What do you mean ? "I'd like to do choose the Folder automatically instead of manually." Please elaborate. – CJK Sep 28 '19 at 10:50
  • When I drop the folder onto the application icon, the command "get files of (choose folder)" forces a window to open with the contents of the folder and I need to hit return for the process to continue. The files in the folder get renamed, and then I need to drop the next folder onto the icon to do the same with its contents. I don't want to hit return for the process to continue... – Sherwin Sep 29 '19 at 16:11
1

The way you handle dropping files onto an AppleScript application is like this:

on open theFolders
    -- do your stuff here
end open

You're dropping a folder, or maybe more than one folder. So there needs to be a repeat in there to handle the dropped items, and then another repeat to handle the items inside each of the dropped items. Like this:

on open theFolders
    tell application "Finder"
        repeat with aFolder in theFolders
            set theFolderName to name of aFolder
            set theFiles to every file of aFolder
            --
            repeat with aFile in theFiles
                set theName to name of aFile
                set theNameLength to length of theName
                set theExtension to name extension of aFile
                set theExtensionLength to length of theExtension
                set thePureNameLength to theNameLength - theExtensionLength
                set theName to (characters 1 through thePureNameLength) of theName as string
                set the name of aFile to the last word of theFolderName & "_" & theName & "." & theExtension
            end repeat
            --
        end repeat
    end tell
end open

I think this does what you want. I don't know what your original file names look like, nor what your folder names look like, but for a case where your folder names look like this:

Folder A, Folder B, Folder C

and where the contents of each folder look like this:

file 1.jpg, file 2.jpg, file 3.jpg

The above droplet would give the following results:

For Folder A

A_file 1.jpg, A_file 2.jpg, A_file 3.jpg

For Folder B

B_file 1.jpg, B_file 2.jpg, B_file 3.jpg

For Folder C

C_file 1.jpg, C_file 2.jpg, C_file 3.jpg

1
  • Thank you, Christian! – Sherwin Apr 13 '20 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .