Hi,
Here are my two scripts between the Rip Caldera Proccess. (www.caldera.com for the curious)
I start with the example of Yvan, because I like the functions “decoupe” and “recolle”.
The part I added or modified is a lot less elegant, but seems to work as desired.
I still have to find how to write in the log file the files in error (files deposited in the source folder, but impossible to move for example - corrupted file …)
And I must also delete the folders D1, D2 and D3 not used for more than 45 days.
GK
Script 1
# Code Yvan/GK Script 1 --> Pdf vers Caldera
----------------------------------------------------------------
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
# Working Folders
set sourceDirectory to POSIX path of ("Macintosh HD:Users:dupont:Desktop:Entree:")
set logDirectory to POSIX path of ("Macintosh HD:Users:dupont:Desktop:Logs:")
set destinationDirectory to POSIX path of ("Macintosh HD:Users:dupont:Desktop:Caldera:")
# Date use to create one log file / month
set theDate to (current date)
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
set mn to time string of (current date) -- ajoute les minutes et secondes
set theDate to y & "-" & m & "-" & d & "-" & mn
# create one log file / month
set reportPath to logDirectory & "Log_" & y & "_" & m & ".txt"
my writeto(reportPath, "", «class utf8», false) # Create/Clean the log file
# 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
repeat with aFile in workingFiles
# 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 newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf" # ADDED
# Create the destination subfolders if they aren't available
--set finalDest to create folder at (destinationDirectory & "/" & partialPath) use name lateSubFolder --> "xxx/D1/D2/D3"
# Move the file
if forTests then
copy object aFile to folder destinationDirectory new name newName
#log result
my writeto(reportPath, my horoDateur() & " - PDF, ajout correct sur RIP Caldera: " & newName & linefeed, «class utf8», true) # Write in the log file
else
move object aFile to folder finalDest new name newName
#log result
my writeto(reportPath, my horoDateur() & " - PDF, ajout correct sur RIP Caldera: " & newName & linefeed, «class utf8», true) # Write in the log file
end if
end repeat
#======
-- TODO: GERER LA SUPPRESSION DES DOSSIERS D1/D2/D3 VIEUX DE PLUS DE 45 JOURS
-- --supprime les dossiers vieux de plus de 45 jours
-- if (creation date of (info for D3 ) < (current date) - 45 * days) then
--
-- try
-- open for access FichLog with write permission
-- write theDate & " - Dossier commande ancien supprime : " & Dos_3 & return to file the FichLog starting at eof -- écrire le message en fin de fichier-
-- close access FichLog
-- on error
-- try
-- close access file the FichLog
-- end try
-- end try
--
-- tell application "Finder" to delete D3
--> Idem for D2 and D1
#=====
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
#=====
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
#=====
#================
Rip Caldera Process
#================
Script2
# Code Yvan/GK Script 2 --> Caldera-ppm vers Serveur
----------------------------------------------------------------
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
# Working Folders
set sourceDirectory to POSIX path of ("Macintosh HD:Users:dupont:Desktop:CalderaPPM:")
set logDirectory to POSIX path of ("Macintosh HD:Users:dupont:Desktop:Logs:")
set destinationDirectory to POSIX path of ("images:0_PPM:") -- Folder on remote server -- local folder for test("Macintosh HD:Users:dupont:Desktop:0_PPM:") --
# Date use to create one log file / month
set theDate to (current date)
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
set mn to time string of (current date) -- ajoute les minutes et secondes
set theDate to y & "-" & m & "-" & d & "-" & mn
# create one log file / month
set reportPath to logDirectory & "Log_" & y & "_" & m & ".txt"
my writeto(reportPath, "", «class utf8», false) # Create/Clean the log file
# Build a list of files embedded in sourceDirectory
set workingFiles to objects of sourceDirectory result type paths list without searching subfolders and include invisible items --include folders and include invisible items
repeat with aFile in workingFiles
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
# Extract the relevant components of the file
set components to item -1 of components
set components to my decoupe(components, "_")
set nbcomponents to (number of components)
set partialPath to my recolle(items 1 thru 3 of components, "/") --> "D1/D2/D3"
set ppmName to my recolle(items 4 thru nbcomponents of components, "_") --> Nom du fichier d'origne
# Create the destination subfolders if they aren't available
set finalDest to create folder at (destinationDirectory & "/" & partialPath) --> "xxx/D1/D2/D3"
# Move the file
if forTests then
copy object aFile to folder finalDest new name ppmName
#log result
my writeto(reportPath, my horoDateur() & " - Ppm, ajout correct sur Prepstation: " & ppmName & linefeed, «class utf8», true) # Write in the log file
else
move object aFile to folder finalDest new name ppmName
#log result
my writeto(reportPath, my horoDateur() & " - Ppm, ajout correct sur Prepstation: " & ppmName & linefeed, «class utf8», true) # Write in the log file
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
#=====
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
#=====