0
votes

I really need you help and will be very grateful for it. I think this should be no problem for you.

So I have folder with different subfolders in it. In subfolders are images. What I need is:

  1. Change each image name to it’s subfolder name + 001 and so on («1stSubfolder001, 1stSubfolder002, …», «2ndSubfolder001, 2ndSubfolder002, …»)
  2. Move all images from all subfolders to one folder (or at least to root folder)
  3. Add random prefix number to names.

I have script to third task:

tell application "Finder" 
    repeat with this_item in (get items of window 1)
        set name of this_item to ((random number from 0 to 99999) & name of this_item) as string
    end repeat 
end tell

But it only works with opened folder on the foreground. Maybe there is a way to make one script to run it on the background?

Huge thank you in advance.

I found cool automator app here, but I need to correct it a little bit. How do I use the current folder name without a path as a variable in automator in Mac?

This script does everything I need BUT it renames all files to ONE folder name, not each different.

Here is another applescript that does rename files to folders, but it uses 2 folders name (folder + subfolder), and I need only subfolder prefix.

set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
    tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat  

(MacBook Pro Late 2013, OS X Yosemite 10.10.1)

1

1 Answers

1
votes

This accomplishes what you want. You should be able to adjust from here. There are no checks to ignore non-image files.

property top_folder : alias "Macintosh HD:Users:MyName:Downloads:Images:"
property save_folder : ""

set save_folder to choose folder with prompt "Select the folder to save the images in."
process_folder(top_folder)

on process_folder(this_folder)
    set these_items to list folder this_folder without invisibles
    set container_name to name of (info for this_folder)
    repeat with i from 1 to the count of these_items
        set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
        if folder of (info for this_item) is true then
            process_folder(this_item)
        else
            process_item(this_item, container_name, i)
        end if
    end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item, c, i)
    -- make the integer a 3 digit string
    if i < 10 then
        set i to "00" & i
    else if i < 100 then
        set i to "0" & i
    end if
    -- set a random number
    set r to (random number from 0 to 99999) as string

    tell application "System Events"
        -- get file extension so not overwritten
        set e to name extension of this_item
        set new_name to "" & r & "_" & c & "_" & i & "." & e
        set name of this_item to new_name
        move this_item to save_folder
    end tell
end process_item