I have a list in the form of Txt file with the current file name in Column A and the name to be renamed to in B
I need to rename the images in folder X with the list above. I also need to add a rule in that it reads only the first 13 digits of the files in folder X (some are labeled 123456789ABCD and some like this 123456789ABCD_2)
This would need to be renamed as SanFranciso and SanFranciso_2. basically everything after the 13 digit needs to be carried over from the original name.
Currently I have this system running as Macro in excel but its only slow as it has to open excel! I also want to turn it into a watch folder eventually so that it can ‘just happen in the background’ Also I am aware A better finder renamer 8 is an option, but this too is slow (needs to happen quietly in the background)
(The excel sheet looks like this)
A B C D
OriginalName NewName =IF(ISNA(VLOOKUP(C1,A2:B:B,2,FALSE)),“”,VLOOKUP(C1,A2:B:B,2,FALSE))
And the macro looks like this.
Sub snb()
Dim c00 As String, c01 As String
c00 = “Macintosh HD:Users:user:Desktop:” ‘here you can change the directory to suit your needs.
c01 = Dir(c00)
Do While c01 <> “”
Range(“C1”) = "’" & Left(c01, 13)
If Range(“D1”) <> “” Then
Name c00 & c01 As c00 & Range(“D1”) & Right(c01, Len(c01) - 13)
End If
c01 = Dir
Loop
End Sub
assuming that the name extension is not part of the name in the csv-list.
The *csv should look like this:
This is a folderAction-Script.
The csv is read every time a file is added to the folder.
If the Csv doesn’t change often or it is quite large we should think of storing its contents instead of reading it each time …
Hope it’ll work
property csvAlias : alias ((path to desktop as text) & "my.csv") --path to your csv-File as alias
property csvDelimiter : ";" --delimiter used by your csv-File
on adding folder items to this_folder after receiving itemList
try
set TID to AppleScript's text item delimiters
repeat with i from 1 to count of itemList
set newFile to item i of itemList
tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
if fSuffix is not "" then set fSuffix to "." & fSuffix
set fName to my stripSuffix(fSuffix, fName)
set csvText to read csvAlias
if csvText contains fName then
set AppleScript's text item delimiters to fName & csvDelimiter
set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
if (count of fName) is greater than 13 then
set newName to newListName & (text 14 thru -1 of fName) & fSuffix
else
set newName to newListName & fSuffix
end if
my renameFile(this_folder, fName & fSuffix, newName)
end if
end repeat
set AppleScript's text item delimiters to TID
on error e
display dialog e
end try
end adding folder items to
on stripSuffix(fSuffix, fName)
if fName contains fSuffix then
set AppleScript's text item delimiters to fSuffix
return (text item 1 of fName)
end if
end stripSuffix
on renameFile(this_folder, origName, newName)
set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
do shell script "mv -f " & sourcePath & space & targetPath
end renameFile
In terms of the csv file its as you mentioned it. its actually listing the barcode and the sku, the item sometimes has more than one image and so I add my own characters after the number that then need to be carried across and they also can be jpg’s or tif or PSD.
5012345678913.jpg should become MA66TT64528MU4MM.jog
Struggling a little at the moment, currently it doesn’t appear to do anything.
How can I modify it so that I tell it the folder to use rather than setting it as a folder action as I will eventually use it to run in the launchD. That way it should still my front most action and run in the background.?
Partly working it names about 2 items then I get the prompt.
Can’t make «class docf≫ “5052411856597.psd” of «class cfol» “matthew” of «class cfol» “Users” of «class sdsk» of application “Finder” into type text.
property csvAlias : alias ((path to desktop as text) & "ean2sku.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File
tell application "Finder"
set this_folder to folder "Hotfolder" as alias
set itemList to every file in this_folder
end tell
try
set TID to AppleScript's text item delimiters
repeat with i from 1 to count of itemList
set newFile to item i of itemList
tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
if fSuffix is not "" then set fSuffix to "." & fSuffix
set fName to my stripSuffix(fSuffix, fName)
set csvText to read csvAlias
if csvText contains fName then
set AppleScript's text item delimiters to fName & csvDelimiter
set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
if (count of fName) is greater than 13 then
set newName to newListName & (text 14 thru -1 of fName) & fSuffix
else
set newName to newListName & fSuffix
end if
my renameFile(this_folder, fName & fSuffix, newName)
end if
end repeat
set AppleScript's text item delimiters to TID
on error e
display dialog e
end try
on stripSuffix(fSuffix, fName)
if fName contains fSuffix then
set AppleScript's text item delimiters to fSuffix
return (text item 1 of fName)
end if
end stripSuffix
on renameFile(this_folder, origName, newName)
set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
do shell script "mv -f " & sourcePath & space & targetPath
end renameFile
didn’t mention that fileNames of originals in the csv are cut of after the 13th character …
Well, after changing this in my folderActionScript it works like charm with your data on my system!
property csvAlias : alias ((path to desktop as text) & "SourceFiles:ean2sku.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File
on adding folder items to this_folder after receiving itemList
try
set TID to AppleScript's text item delimiters
repeat with i from 1 to count of itemList
set newFile to item i of itemList
tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
if fSuffix is not "" then set fSuffix to "." & fSuffix
set fName to my stripSuffix(fSuffix, fName)
set tmpName to (text 1 thru 13 of fName)
set csvText to read csvAlias
if csvText contains tmpName then
set AppleScript's text item delimiters to tmpName & csvDelimiter
set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
if (count of fName) is greater than 13 then
set newName to newListName & (text 14 thru -1 of fName) & fSuffix
else
set newName to newListName & fSuffix
end if
my renameFile(this_folder, fName & fSuffix, newName)
end if
end repeat
set AppleScript's text item delimiters to TID
on error
display dialog "Could not rename: " & fName giving up after 3
end try
end adding folder items to
on stripSuffix(fSuffix, fName)
if fName contains fSuffix then
set AppleScript's text item delimiters to fSuffix
return (text item 1 of fName)
end if
end stripSuffix
on renameFile(this_folder, origName, newName)
set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
do shell script "mv -f " & sourcePath & space & targetPath
end renameFile
If you want to launch the script regulary by system you may use this one.
The renamed files are stored in a destFolder.
property csvAlias : alias ((path to desktop as text) & "SourceFiles:ean2sku.csv")
property hotFolder : alias ((path to desktop as text) & "SourceFiles:HotFolder:")
property destFolder : alias ((path to desktop as text) & "SourceFiles:destFolder:")
property csvDelimiter : "," --delimiter used by your csv-File
set TID to AppleScript's text item delimiters
set fileNames to list folder hotFolder without invisibles
repeat with i from 1 to count of fileNames
try
set fName to item i of fileNames
set filePath to (hotFolder as text) & fName
tell application "System Events" to set fSuffix to name extension of disk item filePath
if fSuffix is not "" then set fSuffix to "." & fSuffix
set fName to my stripSuffix(fSuffix, fName)
set tmpName to (text 1 thru 13 of fName)
set csvText to read csvAlias
if csvText contains tmpName then
set AppleScript's text item delimiters to tmpName & csvDelimiter
set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
if (count of fName) is greater than 13 then
set newName to newListName & (text 14 thru -1 of fName) & fSuffix
else
set newName to newListName & fSuffix
end if
set destFilePath to ((destFolder as text) & newName)
my renameFile(filePath, destFilePath)
end if
on error
display dialog "Could not rename: " & fName giving up after 3
end try
end repeat
set AppleScript's text item delimiters to TID
on stripSuffix(fSuffix, fName)
if fName contains fSuffix then
set AppleScript's text item delimiters to fSuffix
return (text item 1 of fName)
end if
end stripSuffix
on renameFile(filePath, destFilePath)
set sourcePath to quoted form of POSIX path of filePath
set targetPath to quoted form of POSIX path of destFilePath
do shell script "mv -f " & sourcePath & space & targetPath
end renameFile