0
votes

I have an applescript for renaming files based on names provided by a numbers spreadsheet.

Each image has three different sizes, each size in one folder. So there is

/50/exportedImage-01.svg

/33/exportedImage-01.svg

/25/exportedImage-01.svg

The script works totally fine and does what it should do, BUT it is painfully slow and stops at 50+ files. Because I have several hundred files to rename, the question is:

is there any way to alter the script to make it more efficient/powerful?

The workflow is as follows:

  • read the numbers file - new names (featuredImage) in column B, old names (exportedImage) in column C
  • loop through files in first folder and rename them according to column B
  • loop through files in second folder and rename them according to column B
  • loop through files in third folder and rename them according to column B

Here is what I got so far:

set numbersFile to choose file with prompt "Choose the Numbers file" of type {"numbers", "XLS7", "XLS8", "XLSX"}
set theFolder to choose folder with prompt "Choose the folder 50 containing the files to rename"
--get a list of the old and the new filenames
set exportedImage to {}
set featuredImage to {}
tell application "Numbers"
    open numbersFile
    tell table 1 of sheet 1 of document 1
    --tell document 1
    --tell sheet 1
    repeat with i from 1 to row count
        if value of cell ("C" & i as text) > "" then
            set exportedImage to exportedImage & value of cell ("C" & i as text)
            set featuredImage to featuredImage & value of cell ("B" & i as text)
        else
            exit repeat
        end if
    end repeat
    --end tell
end tell
close window 1
end tell

--loop through the files in folder 50 and rename them
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

--loop through the files and rename them 33
set theFolder to choose folder with prompt "Choose the folder 33 containing the files to rename"
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

--loop through the files and rename them 25
set theFolder to choose folder with prompt "Choose the folder 25 containing the     files to rename"
tell application "Finder"
    repeat with k from 1 to (count of (get every item of theFolder))
        repeat with i from 1 to count of exportedImage
            if (name of item k of theFolder) as text = (item i of exportedImage) as text then
                set name of (item k of theFolder) to (item i of featuredImage as text)
            end if
        end repeat
    end repeat
end tell

I would like to stick with applescript, because this script is part of a bigger automator workflow.

Any help appreciated, thanks!

1
I suggest you to use 'do shell script' instruction to run shell command instead. for the shell command, you can use 'find' to find the file with name you're looking for and a pipe to rename the file found. Doing so, you should ask for the 3 folders (50, 33 and 25) on top of your script. then you only loop through your rows in number, and inside that loop process each of the 3 folders with the do shell script find/ rename. with these shell commands renaming 100 files takes just couple of seconds. - pbell

1 Answers

1
votes

I take some time to complete my first comment. Let me explain how you can use the 'find' command.

We are looking for file name "ExportedImage1.svg" in folder "/Users/Document/Folder50/". The shell command is : find /Users/Document/Folder50/ExportedImage1.svg

'Find' command can be enhanced by using '-exec' function. For each file found, -exec will apply the command just after. In the exec , the next command can use {} which means the file found in the find command. last, but not least, the syntaxe requires a ';' at the end.

Example : find path/file.svg -exec mv {} path/newName.svg ';'

It searches for path/file.svg, and if found, it applies the mv command (move or rename) to that file {} with new value path/newName.svg.

In the script bellow, I used Excel (because I do not have Numbers !), but you can easily adjust the first block of the script to use your Numbers.

Also I changed your logic by doing a loop through all Excel data row (the second colonne=Exported image). Inside that loop, for each image/excel row, I loop through the 3 folders you have selected (25, 33, and 50). inside that loop I search the file in the folder, and if found, I change the name using the first colonne of the Excel file (FeaturedImage).

I assume that file extensions are always '.svg'

-- you must replaced that block by your Numbers block
set numbersFile to choose file with prompt "Choose the Numbers file" of type {"numbers", "XLS7", "XLS8", "XLSX"}
tell application "Microsoft Excel"
-- my Excel file is made of column B=FeaturedImage (will be new file names) and column C=ExportedImage (current file name)
-- I assume that name in files have no extension '.svg'
    open numbersFile
    tell active sheet to set myData to value of used range
    close window 1
    -- myData is list of rows: each item is a list made of 2 values : value in col B and value in col C
end tell


-- select the 3 folders
set folder50 to choose folder with prompt "Choose the folder 50 containing the files to rename"
set folder33 to choose folder with prompt "Choose the folder 33 containing the files to rename"
set folder25 to choose folder with prompt "Choose the folder 25 containing the files to rename"
-- build a list of the 3 folders
set myFolders to {POSIX path of folder50}
set myFolders to myFolders & {POSIX path of folder33}
set myFolders to myFolders & {POSIX path of folder25}


repeat with myRow in myData -- loop through all data rows   
    repeat with aFolder in myFolders -- loop through folders : 25, 33, 50

        -- search file name = item 2 of myRow (=ExportedImage) in folder 50 and if find, rename to item 1 of myRow
        set curPath to quoted form of (aFolder & (item 2 of myRow) & ".svg") -- the complete path/name to search
        set newPath to quoted form of (aFolder & (item 1 of myRow) & ".svg") -- the complete path/name to replace/rename
        try -- to avoid error in case file not found in the folder
            do shell script "find " & curPath & " -exec mv {} " & newPath & " ';'"
        end try
    end repeat -- next folder 25, 33, 50
end repeat -- next data row

Optimisation of the loops and use of 'find - exec' make this script much more faster than your !!