Hello,
Tanks Nigel for your interest.
I tried to do the second part of a project that consists of moving files while retaining their original paths.
I’m using 2 Applescript that are started every 60 second by a .plist file placed in ~ / Library / LaunchAgents directory and loaded with terminal (launchctl load -w ~/Library/LaunchAgents/com.apfile.plist
The first Applescript has been in use for 5 years in my compagny (10 users) and retrieves the file path. (Error handling that does not work very well makes it possible to check that the file has been processed correctly)
For this first script, I did not find at the time of example with the shell.
Between the 2 scripts, the file is processed by a RIP that turns the .pdf into .ppm.
The second Applescript i’m trying to set up now, move the file by recreating its original path on the remote server. Ideally it would be fine to write in the file log the result
1- First script now in use
-- First part : recovering the folder path and putting it in front of the file name
-- Definition des 3 dossiers fixes : Emplacement à partir de la racine du disque
set D_Entree to alias ("Macintosh HD:Entree:")
set D_Sortie to "Macintosh HD:Sortie:" as string
set D_Erreur to "Macintosh HD:Erreurs:" as string
-- formatage de la date pour inscription dans le fichier log
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
-- définition du fichier de log des erreurs
set FichLog to D_Erreur & "Log_" & y & "_" & m & ".txt"
-- pour chaque dossier niveau 1 source entrée
tell application "Finder"
set Niv_1 to every folder of D_Entree
repeat with Dos_1 in Niv_1 -- pour chaque dossier de niveau 1(Dossier Client)
set Nom_1 to name of Dos_1
set Niv_2 to every folder of Dos_1
repeat with Dos_2 in Niv_2 -- pour chaque dossier de niveau 2 contenu dans le niveau 1 (Dossier Commande)
set Nom_2 to name of Dos_2
set Niv_3 to every folder of Dos_2
repeat with Dos_3 in Niv_3 -- pour chaque dossier de niveau 3 contenu dans le niveau 2 dans niveau 1 ((Dossier Support)
set Nom_3 to name of Dos_3
set Liste_Fichier to every file of Dos_3
repeat with F in Liste_Fichier -- boucle sur chaque fichier du dossier
delay 0.3 -- wait for file growing (sometimes more than 100 files at the same time)
set N_Nom to Nom_1 & "_" & Nom_2 & "_" & Nom_3 & "_" & (name of F) -- variable N_Nom = nom du futur fichier
set M_Alias to F as alias -- stock l'alias vers le fichier avant de changer son nom
set the name of F to N_Nom -- change le nom du fichier avant déplacement
delay 0.3
-- Déplacement du fichier renommé et inscription dans le fichier log --------------------------------
try
tell application "Finder" to move M_Alias to folder D_Sortie with replacing
on error -- Don't work anymore ?
-- open for access FichLog with write permission
-- write theDate & " - Erreur de transfert de: " & N_Nom & 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
end try
try
open for access FichLog with write permission
write theDate & " - Ajout correct: " & N_Nom & 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
end repeat -- sur chaque fichier
-- supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for Dos_3 as alias) < (current date) - 45 * days) then
try
open for access FichLog with write permission
write theDate & " - Dossier de commande > 45 jour 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 Dos_3
end if
end repeat -- Niv_3
end repeat -- Niv_2
end repeat -- Niv_1
end tell
2- Second script on work
-- Second part: move file after treatment into the original folder path on remote server
set folderList to {}
set numberList to {}
set DestinationFolder to "Volumes:images:0_PDF:" -- local folder "Macintosh HD:public:0_PDF:"
set sourceFolder to alias ("Macintosh HD:Sortie:")
set POSIXdestinationFolder to POSIX path of DestinationFolder
tell application "Finder" to set theFiles to files of sourceFolder
repeat with oneFile in theFiles
set fileName to (get name of oneFile)
--Récupération des 3 noms de dossiers et du nom de fichier de départ
set lengthfileName to length of fileName
set AppleScript's text item delimiters to "_"
set subfolder1 to text item 1 of fileName
set lengthsubfolder1 to length of subfolder1
set subfolder2 to text item 2 of fileName
set lengthsubfolder2 to length of subfolder2
set subfolder3 to text item 3 of fileName
set lengthsubfolder3 to length of subfolder3
set lengthsubfolder to lengthsubfolder1 + lengthsubfolder2 + lengthsubfolder3 + 4
set AppleScript's text item delimiters to ""
set fileNamePdf to (characters lengthsubfolder thru -1 of fileName) as string
set subfolder1Path to POSIXdestinationFolder & subfolder1 & "/" & subfolder2 & "/" & subfolder3
set subfolder1Path to POSIX path of subfolder1Path
try
tell application "Finder" to set fileExists to exists (file fileName of folder (DestinationFolder & subfolder1 & ":" & subfolder2 & ":" & subfolder3 & ":"))
on error
set fileExists to false
end try
if not fileExists then
set sourceFile to quoted form of POSIX path of (oneFile as text)
do shell script "/usr/bin/ditto " & sourceFile & space & quoted form of (subfolder1Path & "/" & fileNamePdf)
do shell script "/bin/rm -r " & sourceFile
set flag to false
repeat with i from 1 to count folderList
if item i of folderList is subfolder3 then
set flag to true
exit repeat
end if
end repeat
if flag then
set item i of numberList to (get item i of numberList) + 1
else
set end of folderList to subfolder3
set end of numberList to 1
end if
end if
end repeat
set resultList to {}
repeat with i from 1 to count folderList
set end of resultList to (item i of numberList as text) & " fichier(s) ont été copié(s) dans: " & item i of folderList
end repeat
if resultList is {} then
set resultString to "No files were copied"
else
set {TID, text item delimiters} to {text item delimiters, return}
set resultString to resultList as text
set text item delimiters to TID
end if
display dialog resultString
Tanks for your time
GK