Hey guys,
I know that this has been discussed more times than I can count, but I cannot get this script to work for the life of me.
I have a list of files within one folder:
1234567890_A.jpg
1234567890_B.jpg
1234567890_C.jpg
1234555555_A.jpg
1234555555_B.jpg
1234555555_C.jpg
1234555555_D.jpg
1234567888_A.jpg
1234567888_B.jpg
…so on and so forth…
Anyway, I need to take these files, move them into a folder with the 10-digit number and a suffix - “1234567890 User Inserted Suffix Here”.
When the files are moved, I am trying to rename them at the same time, so I need the files being moved into the “1234567890 User Inserted Suffix Here” folder to be renamed like this:
1234567890_1_Text Here.jpg
1234567890_2_Text Here.jpg
1234567890_3_Text Here.jpg
Currently, I am grabbing each file within the parent folder and moving only the ones that begin with a 10 digit number (I need it to do it like this), and checking to see if a folder has been created for it yet. If it has, increment the counter and move the file into it’s subfolder with the new name - if it has not, reset the increment counter to 1, create a subfolder with the new name, and move the file into that folder.
My problem is that the incremental counter doesn’t seem to be incrementing. So the script is creating the new folders and everything correctly, and the correct files are being moved into the new folders, but the files within those new folders are being overwritten because the counter isn’t incrementing. I hope that makes sense… So:
1234567890_A.jpg is being renamed to 1234567890_1_Text Here and moved to “1234567890 User Inserted Suffix Here”
BUT
1234567890_B.jpg is being renamed to 1234567890_1_Text Here and moved to “1234567890 User Inserted Suffix Here” as well, thereby overwriting the previous file.
Here is the script that I have:
-- Set properties
property jpgMovedCount : 0
property rawMovedCount : 0
property renameCount : 1
-- Set Handlers
on coerceToInteger(theVariable)
try
-- Check if theVariable can be coerced to an integer
theVariable as integer
return true
on error
return false
end try
end coerceToInteger
on folderExists(theFolder)
try
get theFolder as alias
return true
on error
return false
end try
end folderExists
-- Load Progress Bar
on InitializeProgressBar()
return load script alias (((path to me) as string) & "Contents:Resources:Scripts:BP Progress Bar Controller.scpt")
end InitializeProgressBar
-- Prompt for which action to take
set userAction to button returned of (display dialog "Which files would you like to move?" with icon note buttons {"JPGs", "RAWs", "Both"} default button 3)
if userAction = "JPGs" then
-- Set name of the event
set brandSuffix to text returned of (display dialog "Enter the name of the event." buttons {"Cancel", "OK"} default button 2 default answer "Event Name")
-- Choose source folder
set jpgFolder to choose folder with prompt "Choose source folder containing JPGs."
set jpgFolderPosPath to POSIX path of jpgFolder
set progressBar to my InitializeProgressBar()
tell progressBar to initialize("Folder Creator")
tell progressBar
setStatusTop to "Initializing & Collecting Images."
end tell
-- Find the JPGs
tell application "Finder" to set jpgFiles to files of jpgFolder whose name extension is "jpg"
set jpgFileCount to count (jpgFiles)
tell progressBar to setMax to jpgFileCount
-- If there are more than 0 files, run the script, otherwise stop it.
if jpgFileCount > 0 then
-- Begin moving the JPGs to their own SKU folders
repeat with aFile in jpgFiles
-- Get the name of the file
set fileName to name of aFile
-- Grab the first 10 digits of the file name
set styleNum to text 1 thru 10 of fileName
-- Check the first 10 digits of fileName to see if they're an integer
if coerceToInteger(styleNum) = true then
-- Increment jpgMovedCount
set jpgMovedCount to jpgMovedCount + 1
-- Add brandSuffix to prefix
set prefix to styleNum & " " & brandSuffix
-- Set path of folder to be created
set newFolder to jpgFolderPosPath & prefix
set checkFolder to POSIX path of newFolder
-- Begin renaming function
if folderExists(checkFolder) = false then
-- Create new folder with prefix
do shell script "mkdir -p " & quoted form of POSIX path of newFolder
set renameCount to 1
do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & styleNum & "_" & renameCount & "_NOCOLOR")
else
set renameCount to renameCount + 1
do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & styleNum & "_" & renameCount & "_NOCOLOR")
end if
-- Set status within the progress bar and increment
tell progressBar
setStatusTop to "Creating Folders & Moving Images."
setStatusBottom to "Processing " & fileName
increase by 1
end tell
else
-- Just increment the progress bar
tell progressBar
increase by 1
end tell
end if
end repeat
tell progressBar to quit
-- Display completion dialog with jpgMovedCount
if jpgMovedCount ≥ 150 then
display dialog jpgMovedCount & " images have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
else
display dialog jpgMovedCount & " images have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
end if
-- Reset jpgMovedCount
set jpgMovedCount to 0
else
-- Quit the progress bar
tell progressBar to quit
display dialog "There were 0 images to move! :(" with icon note buttons {"Done!"} default button 1 giving up after 2
end if
...
Any help with this would be GREATLY appreciated!