Rename file using Numbers worksheet

Hello,

I am trying to rename files on a specific folders. The old name and the new name are found within a number spreadsheet. I am able to loop through the worksheet. Hiowever, the rename command is not working.

Thanks for your help in advance.

set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
--set folder33 to choose folder with prompt "Choose the folder 33 containing the files to rename"
set myFolder to {POSIX path of folderpath}
display dialog "myFolder: " & myFolder

tell application "Numbers"
	
	-- In Numbers the first table is Table 1.  It is hard to know this because the table most likely does not have its name appear, but the active sheet in a new Number file is Table 1.  There can be multiple tables on a sheet.  Therefore you need to designate the document (file) sheet, and table before you can access attributes
	tell table 1 of active sheet of front document
		
		-- For each row for the count of cells in the first column
		repeat with i from 1 to count of cells of column 1
			
			-- Get the values of the Name and Address for each row and store locally
			
			set currentfilename to value of cell i of column "C"
			set newfilename to value of cell i of column "B"
			
			
			-- 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 (myFolder & (currentfilename)) -- the complete path/name to search
			set newPath to quoted form of (myFolder & (newfilename)) -- 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
		
	end tell
	
end tell

Putting everything else aside, using ‘{}’ in this line generates a list and that creates a problem later in the script.

set myFolder to {POSIX path of folderpath}

I’m not sure whether any type of bracket is necessary but if so, use regular parentheses:

set myFolder to (POSIX path of folderpath)

I’ll look at the rest of the script later. I’m suspicious about the find statement, as well.

Yesterday I rewrote the OP’s script to make it functional. I never posted it because the OP may be using the find command to make the script recursive, although that makes little sense. There are better ways to do this, but perhaps the OP has a reason for writing the script as he did. I tested the following script on my Sequoia computer without issue.

set folderpath to choose folder with prompt "Choose the folder containing the files to rename"
--set folder33 to choose folder with prompt "Choose the folder 33 containing the files to rename"
set myFolder to POSIX path of folderpath
display dialog "myFolder: " & myFolder

tell application "Numbers"
	-- In Numbers the first table is Table 1.  It is hard to know this because the table most likely does not have its name appear, but the active sheet in a new Number file is Table 1.  There can be multiple tables on a sheet.  Therefore you need to designate the document (file) sheet, and table before you can access attributes
	tell table 1 of active sheet of front document
		
		-- For each row for the count of cells in the first column
		repeat with i from 1 to count of cells of column 1
			
			-- Get the values of the Name and Address for each row and store locally
			
			set currentfilename to value of cell i of column "C"
			set newfilename to value of cell i of column "B"
			
			
			-- 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 (myFolder & (currentfilename)) -- the complete path/name to search
			set newPath to quoted form of (myFolder & (newfilename)) -- the complete path/name to replace/rename
			moveFile(curPath, newPath) of me
		end repeat
		
	end tell
	
end tell

on moveFile(curPath, newPath)
	try -- to avoid error in case file not found in the folder
		do shell script "mv " & curPath & space & newPath
	end try
end moveFile
1 Like

You are correct.

In replacing the {} by parenthesis it now working.

Thanks again.

1 Like

I’ve replaced my code by the one you are suggesting. Everything works fine!

Thanks again!