As insomnia strike, I built a more complete code.
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.2"
use scripting additions
----------------------------------------------------------------
property forTests : true # during tests I use copy object so the original files remain in place
tell application "Finder"
activate -- Bring to the front to display the following dialogs.
set sourceDirectory to POSIX path of (choose folder with prompt "Select the folder containing the subfolders containing the file(s):")
set destinationDirectory to POSIX path of (choose folder with prompt "Select a destination for the renamed file(s):")
end tell
# Build a list of files embedded in sourceDirectory
set workingFiles to objects of sourceDirectory result type paths list with searching subfolders without include folders and include invisible items
set p2d to path to desktop as text
set reportPath to p2d & "myLog.txt"
my writeto(reportPath, "", «class utf8», false) # Create/Clean the log file
# build the folder used as workspace
set tempFolder to POSIX path of (path to temporary items) # Edit to fit your needs
repeat with aFile in workingFiles
if aFile ends with ".pdf" then
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set partialPath to my recolle(items -4 thru -3 of components, "/") --> "D1/D2"
set lateSubFolder to item -2 of components --> "D3"
set newNamePdf to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set newNamePpm to (text 1 thru -3 of newNamePdf) & "pm" --> "D1_D2_D3_oldName.ppm"
# Create the destination subfolders if they aren't available
set finalDest to (create folder at (destinationDirectory & partialPath) use name lateSubFolder) & "/" --> "xxx/D1/D2/D3"
# Duplicate the file to the temp folder so it will remain available if something fail during the process
set tempPDF to copy object aFile to folder tempFolder new name newNamePdf with replacing and return path
#log result
my writeto(reportPath, my horoDateur() & " The file “" & aFile & "” is duplicated as “" & tempPDF & "”" & linefeed, «class utf8», true) # Write in the log file
# Now fake "Caldera RIP" code
set tempPpm to tempFolder & newNamePpm
# For safe, delete such Ppm if it already exists
if exists object tempPpm then remove object tempPpm
# Now we may rename. Returning the path was coded for tests, not needed in working script
rename object tempPDF to name newNamePpm # with return path
# log result
my writeto(reportPath, my horoDateur() & " The file “" & tempPDF & "” is converted as “" & tempPpm & "”" & linefeed, «class utf8», true) # Write in the log file
# Move the Ppm file to final location.
set finalPpm to move object tempPpm to folder finalDest with replacing and return path --> "xxx/D1/D2/D3/D1_D2_D3_oldName.ppm"
#log result
my writeto(reportPath, my horoDateur() & " The document is stored as “" & finalPpm & "”" & linefeed & linefeed, «class utf8», true) # # Write in the log file
# Here the process ended flawlessly so we may delete the original
if not forTests then remove object aFile
on error e number n
my writeto(reportPath, my horoDateur() & " Error : " & e & " number# " & n & " The original file “" & aFile & ". remains available." & linefeed & linefeed, «class utf8», true) # Write in the log file
end try
end if
end repeat
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
on horoDateur()
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd HH:mm:ss"
return (theFormatter's stringFromDate:(current date)) as text
end horoDateur
#=====
@KniazidisR
You made a mixture of fileManagerLib syntax and Finder’s one.
set ripFile to duplicate aFile to RipDirectory with newName – all this was THE FIRST PART OF JOB is a wrong instruction.
There is no duplicate command in FileManagerLib.
There is a create duplicate one but it doesn’t accept the parameter with newName
The correct syntax would be :
set ripFile to copy object aFile to folder RipDirectory new name newName with return path # for safe add “and replacing”
The instruction :
move ripFile to destinationDirectory is wrong too
It would be :
move object ripFile to folder destinationDirectory
I hope that I forgot nothing. Now I will retry to sleep.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 6 aout 2019 23:01:41
I corrected an error and added code writing a log file on the desktop.