I frequently drag pictures from a webcam onto my desktop which names them in sequence “current.jpg”, “current-1.jpg”…I would like to do a applescript to clean up the images on my desktop that begin with “current” activated at my choosing. The files would go into a folder which stores them and renames them to “reef” & a number which is the next number in the sequence of jpgs already in the folder.
So far I have used a previous script and can get y and z to function properly.
I am losing the logic in the repeat section which should move the files to the location of “this_folder” and rename and renumber them to defaultname and a number which I guess would be “z+1”. I don’t know how to declare the files which contain the currentname into thoose_files and to move them.
set this_folder to alias "external drive path to folder"
set desktopfolder to alias (path to desktop folder as Unicode text)
set defaultname to "reef" -- the name you want to give to your files
set currentname to "current"
set leadingZeros to 3 -- the size of the argument of the fil
tell application "Finder"
try
set z to count (files in desktopfolder whose name contains currentname)
on error
set z to 0
end try
try --count the number of files in the folder with the default name
set y to count (files in this_folder whose name contains defaultname)
on error
set y to 0
end try
repeat with x from 1 to count thoose_files
set argumentnumber to (x + y - z - 1) as string -- create the correct argument
tell application "Finder"
repeat until (count (argumentnumber)) > leadingZeros - 1
set argumentnumber to "0" & argumentnumber
end repeat
end tell
set name of item x of thoose_files to defaultname & argumentnumber --change the name
move item x to this_folder
end repeat
end tell
I don’t have time to put together a solution tonight but I would suggest
you get a list of the files in the folder first. Sort them and get the last
name in the list. Split the name apart grabbing the number. Now that
you have the number start moving the files from your desktop into
the folder changing their name along the way.
The reason I suggest you do it this way is if for some reason some
of the files are removed from the folder the count of files and the
last file’s number will not be the same.
Since it is probably a small number of files you can use the ‘mv’
command to move the files and rename them at the same time.
do shell script "mv " & quoted form of posix path of this_file & quoted form of posix path of new_name
Doing it this way will also eliminate the sound the Finder makes when
moving files.
property defaultname : "reef" -- the name you want to give to your files
property currentname : "current"
property leadingZeros : 3 -- the size of the argument of the fil
set desktopfolder to path to desktop
set this_folder to alias "external drive path to folder"
tell application "Finder"
set newFiles to files in desktopfolder whose name contains currentname
if newFiles is {} then return -- no files to rename
set defFiles to files in this_folder whose name contains defaultname
end tell
set leadingZeroString to ""
repeat leadingZeros times
set leadingZeroString to leadingZeroString & "0"
end repeat
if (count defFiles) = 0 then
set lastNumber to 0
else
tell application "Finder" to set {name:lastName, name extension:lastEx} to item -1 of defFiles
set exOffset to (offset of lastEx in lastName) - 2
set lastNumber to text (exOffset - (leadingZeros - 1)) thru exOffset of lastName as integer
end if
repeat with oneFile in newFiles
set lastNumber to lastNumber + 1
set destinationPath to quoted form of (POSIX path of this_folder & defaultname & text -leadingZeros thru -1 of (leadingZeroString & lastNumber as text) & "." & lastEx)
do shell script "/bin/mv " & quoted form of POSIX path of (oneFile as alias) & space & destinationPath
end repeat
Note:
try
set z to count (files in desktopfolder whose name contains currentname)
on error
set z to 0
end try
the error part will never be passed, because z is 0 anyway if there are no files matching the filter condition
there are 2 files on the desktop with current in them and one in the new folder called reef.jpg.
Exoffset is 4 and leadingzero is 3.
I’m not sure what the
the script expects either one or more files with format reef001.ext or no file at all containing “reef”.
With this change the counter starts at 0 if there is any other file containing “reef” but no numbering.
.
if (count defFiles) = 0 then
set lastNumber to 0
else
tell application "Finder" to set {name:lastName, name extension:lastEx} to item -1 of defFiles
set exOffset to (offset of lastEx in lastName) - 2
try
set lastNumber to text (exOffset - (leadingZeros - 1)) thru exOffset of lastName as integer
on error
set lastNumber to 0
end try
end if
.
I didn’t understand the script.
I renamed the files with the convention 001… and it works fine.
The script looks for a three digit number and counts it but what is the best way to deal with a string that changes number of digits?
if the property leadingZeros is 2, then the script looks for an processes a 2 digit number,
but I assume it will cause problems if the folder contains files with a mixed amount of digits
Of course, but it doesn’t help if you have 2 and 3 digit numbered files in the same folder.
The script reads all files which contain “reef” and takes the current number from the last one (lastNumber)
The next number is lastNumber + 1