You are not logged in.
Thanks to Shane Stanley and Jonas Whale, here are alternate versions for one of my handlers.
Applescript:
on horoDateur() --> with milliseconds
set theDate to current application's NSDate's |date|() # the 'complete' current date
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd AAAAAAAA"
return (theFormatter's stringFromDate:theDate) as text --> (*2019-08-09 00551161*)
end horoDateur
It doesn't return the time as hh:mm:ss but as the count of milliseconds in the day.
It's more precise than the use of current date which count the seconds, not the milliseconds
Applescript:
on horoDateur() --> without milliseconds
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy-MM-dd AAAAAAAA"
return text 1 thru -4 of ((theFormatter's stringFromDate:(current date)) as text) --> (*2019-08-09 00551*)
end horoDateur
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 9 aout 2019 15:40:15
Last edited by Yvan Koenig (2019-08-11 04:11:41 am)
Offline
I'm an ass.
I just discovered that the script 0 supposed to create a new logfile was not required. Look at message #24 to get new scripts.
A problem bothers me.
I'm afraid that the removing of old folders doesn't work correctly.
Assuming that D1 was created on 2019/07/01, D2-1 and D3-1-1 ditto
D2-2 was created on 2019/07/15
D3-2-1 was created on 2019/07/15 in D2/2
If we try to write a new file in D1/D2-1/D3-1-1 on 2019/09/03,
D3-1-1 will be removed which is OK
D2-1 will be removed which is OK
D1 will be removed too and it's bad.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 10 aout 2019 09:16:24
Offline
I assume that replacing (in both scripts) :
Applescript:
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest2)) < dateMinus45 then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if (creation date of (info for POSIX file dest1)) < dateMinus45 then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
by
Applescript:
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest2)) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest1)) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
would be better.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 10 aout 2019 14:13:30
Last edited by Yvan Koenig (2019-08-10 06:26:27 am)
Offline
Hello,
Yvan thank you again for all your work.
I think that for removing old folder we could rather check the file modification date rather than the creation date. in our use, the files are used between 1 week and a month. That's why I chose 45 days.
I will try your new code and come back to show here.
GK
Offline
At : http://www.hamsoftengineering.com/codeS … Dates.html
I found a CLI allowing me to change the creation dates of items.
Thanks to it I was able to create a folder with old items.
This let me discover that some instructions were wrongly formed.
Here is the correct block of instructions:
Applescript:
--supprime les dossiers vieux de plus de 45 jours
set maybe to POSIX file dest3 # ADDED
if (creation date of (info for maybe)) < dateMinus45 then # EDITED
remove object dest3 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
set maybe to POSIX file dest2 # ADDED
if ((creation date of (info for maybe)) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then # EDITED
remove object dest2 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
set maybe to POSIX file dest1 # ADDED
if ((creation date of (info for maybe)) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then # EDITED
remove object dest1 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
I understand what was wrong with the instruction remove object which was treating only files, not folders.
I'm puzzled by the behavior of info for.
creation date of (info for (POSIX file dest3)) issued an error.
set maybe to POSIX file dest3
creation date of (info for maybe) work flawlessly
I dislike such case where I don't understand what is wrong but at least, I have a working code.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 11:58:49
Last edited by Yvan Koenig (2019-08-11 04:14:39 am)
Offline
I found a CLI allowing me to change the creation dates of items.
Thanks to it I was able to create a folder with old items.
Hi Yvan.
There's a trick which still seems to work if you want to make a file look as if it was created before it really was — and that is to set its modification date to the earlier date. The creation date goes back to match this. You can then put the modification date forward again if you want.
creation date of (info for (POSIX file dest3)) issued an error.
Assuming that dest3's a valid POSIX path, it works for me.
NG
Offline
Yvan Koenig wrote:
creation date of (info for (POSIX file dest3)) issued an error.
Assuming that dest3's a valid POSIX path, it works for me.
It works in a bare demo script but failed in the complete one.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 14:39:13
Offline
The creation date goes back to match this. You can then put the modification date forward again if you want.
Or you can do it this way:
Applescript:
set theURL to current application's NSURL's fileURLWithPath:posixPath
theURL's setResourceValue:(current date) forKey:(current application's NSURLCreationDateKey) |error|:(missing value)
And your file was created after it was last modified
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
Bingo.
At last, the pig headed guy found what was wrong.
creation date of (info for (POSIX file aPath))
works in a 'standard' script but fails in a script using an instruction use framework "aFramework".
In this late case, we must use creation date of (info for ( aPath as «class furl»))
I tested with this bare script:
Applescript:
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "FileManagerLib" version "2.2.2"
use scripting additions
----------------------------------------------------------------
script o # ADDED
property reportPath : "" # ADDED
property dateMinus45 : missing value # ADDED
end script # ADDED
set p2d to path to desktop as text
set sourceDirectory to POSIX path of (p2d & "D1 kopy:")
set logDirectory to POSIX path of p2d
set destinationDirectory to POSIX path of (p2d & "dest:")
# Date used to build the name of the log file / month
set theDate to current date
set time of theDate to 0 -- for future comparisons
set o's dateMinus45 to theDate - 45 * days -- EDITED with o's
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy_MM"
set o's reportPath to logDirectory & ("Log_" & (theFormatter's stringFromDate:theDate)) & ".txt" # EDITED with o's
if not (exists object o's reportPath) then # EDITED with o's
# Whe entered a new month, creates a new logfile
my writeto(o's reportPath, "", «class utf8», false) # EDITED with o's
end if
set aFile to POSIX path of (p2d & "D1 kopie:D2-2:D2-2-2:Ticket.pdf")
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"}
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2"
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
-- Remove the files more than 45 days old
my CleanFolder(dest3)
my CleanFolder(dest2)
my CleanFolder(dest1)
#=====
on CleanFolder(dest)
set existingFiles to objects of dest result type paths list without searching subfolders, include folders and include invisible items
repeat with aPath in existingFiles
if (creation date of (info for (aPath as «class furl»))) < o's dateMinus45 then # EDITED with o's
remove object aPath # OK for file, wrong for folder
my writeto(o's reportPath, my horoDateur() & " - Fichier commande ancien supprimé : " & aPath & linefeed, «class utf8», true) # EDITED with o's
end if
end repeat
end CleanFolder
#=====
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
#=====
The script's history displayed:
Applescript:
tell current application
path to desktop as text
--> "SSD 500:Users:**********:Desktop:"
current date
--> date "dimanche 11 août 2019 à 15:10:57"
(*/Users/**********/Desktop/dest/D1 kopie/D2-2/D2-2-2/Ticket.pdf*)
info for file "SSD 500:Users:**********:Desktop:dest:D1 kopie:D2-2:D2-2-2:Ticket.pdf"
--> {name:"Ticket.pdf", creation date:date "samedi 1 juin 2019 à 00:01:02", modification date:date "samedi 1 juin 2019 à 01:02:03", size:46290, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"pdf", displayed name:"Ticket.pdf", default application:alias "SSD 500:Applications:Preview.app:", kind:"Document PDF", file type:", file creator:", type identifier:"com.adobe.pdf", locked:false, busy status:false, short version:"", long version:""}
current date
--> date "dimanche 11 août 2019 à 15:10:57"
open for access file "SSD 500:Users:**********:Desktop:Log_2019_08.txt" with write permission
--> 719
write "2019-08-11 15:10:57 - Fichier commande ancien supprimé : /Users/**********/Desktop/dest/D1 kopie/D2-2/D2-2-2/Ticket.pdf
" to 719 starting at eof as «class utf8»
close access 719
end tell
So, in the main scripts we must add the new handler on CleanFolder(dest, dateMinus45, reportPath)
the script object
script o # ADDED
property reportPath : "" # ADDED
property dateMinus45 : missing value # ADDED
end script # ADDED
edit the instructions commented by # EDITED withn o's
and replace the old group of instructions by:
Applescript:
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"} # ADDED
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2" # ADDED
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
--supprime les fichiers vieux de plus de 45 jours
my CleanFolder(dest3)
my CleanFolder(dest2)
my CleanFolder(dest1
If like me you dislike the use of the deprecated info for
you may replace the handler by:
Applescript:
on CleanFolder(dest)
set existingFiles to objects of dest result type paths list without searching subfolders, include folders and include invisible items
repeat with aPath in existingFiles
set theURL to (current application's NSURL's fileURLWithPath:aPath)
set {theResult, NSDate, theError} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLCreationDateKey) |error|:(reference))
if (NSDate as date) < o's dateMinus45 then # EDITED with o's
remove object aPath # OK for file, wrong for folder
my writeto(o's reportPath, my horoDateur() & " - Fichier commande ancien supprimé : " & aPath & linefeed, «class utf8», true) # EDITED with o's
end if
end repeat
end CleanFolder
which just requires more typing.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 15:45:31
Last edited by Yvan Koenig (2019-08-11 08:24:21 am)
Offline
Hello Shane.
If I run :
Applescript:
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------
set posixPath to POSIX path of ((path to desktop as text) & "dossier 100:")
set newDate to current date
set month of newDate to 12
set day of newDate to 31
set year of newDate to 1943
set theURL to current application's NSURL's fileURLWithPath:posixPath
theURL's setResourceValue:(newDate) forKey:(current application's NSURLCreationDateKey) |error|:(missing value)
theURL's setResourceValue:(newDate) forKey:(current application's NSURLContentModificationDateKey) |error|:(missing value)
set {theResult, NSDate, theError} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLCreationDateKey) |error|:(reference))
log (NSDate as date)
set {theResult, NSDate, theError} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLContentModificationDateKey) |error|:(reference))
log (NSDate as date)
set {theResult, NSDate, theError} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLAttributeModificationDateKey) |error|:(reference))
log (NSDate as date)
I get:
tell current application
path to desktop as text
current date
(*date vendredi 31 décembre 1943 à 16:34:37*)
(*date vendredi 31 décembre 1943 à 16:34:37*)
(*date dimanche 11 août 2019 à 16:34:37*)
end tell
What is the real use of NSURLAttributeModificationDateKey which is not reflected in the infos window or in the Finder's ones.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 16:37:02
Offline
Hi,
I am really too slow, the time I test, understand, and write my answer (in English, thank you google) there are already several other solutions to test.
Thank you for your interest, I'm learning a lot.
So, I tested and adapted the script 1 to my needs.
- Dates: Thanks for code with milliseconds, but I prefer to keep the minutes and seconds because the log is more readable.
- Errors: I added a test for the 3 types of files that will be used (pdf, jpg, tif).
But I do not know how to deal with file system errors.
For example: file is still growing when the script try to move it.
What's going on?
No blocking of the script, the other files are processed by the script, the file is not "broken" (i.e. file at 0ko) and therefore the file is processed in the next loop. Nothing to do in this case.
Not blocking of the script, the other files are processed by the script, the file is "broken" (0ko file), it should be noted in the log.
Script is stop, it means no action any more by the script, no more file are processed.
that's what happens sometimes with my old script (see my first post) and i would like to avoid it with the new script.
At least the "faulty" file should be identified and logged in the log file.
- For the suppression of the folder > 45 days, i think that it is necessary to make another loop.
The old folders are processed when they are empty and therefore no need to keep them.
Here "set workingFile to objects of sourceDirectory result type paths listing with searching subfolders without include folders and include invisible items"
we work on the list of files and if the folder is empty, it is ignored.
I tried to change the Yvan'scode but I do not know how to use the "objects" and it does not work.
I will try the new code.
Here is the step wherre i am now
Applescript:
# Code Yvan/GK Script 1 --> Pdf vers Caldera V2 OK
----------------------------------------------------------------
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:Macintosh HD:Users:dupont:Desktop:Logs:")
set destinationDirectory to POSIX path of ("MMacintosh HD:Users:dupont:Desktop:Caldera:")
# Date used to build the name of the log file / month
set theDate to current date
set time of theDate to 0 -- for future comparisons
set dateMinus45 to theDate - 45 * days -- for future comparisons
set reportPath to logDirectory & my monthStamp(theDate)
if not (exists object reportPath) then
# Whe entered a new month, creates a new logfile
my writeto(reportPath, "", «class utf8», false)
end if
# 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
#vérifie que le fichier peut être traité par le RIP Caldera
if aFile ends with ".pdf" or aFile ends with ".tif" or aFile ends with ".jpg" then
set typeFileOK to 1
else
set typeFileOK to 0
end if
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"}
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2"
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
if typeFileOK > 0 then
# Create the destination subfolders if they aren't available
#set finalDest to create folder at (destinationDirectory & "/" & partialPath) use name lateSubFolder
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 finalDest new name newName
#log result
my writeto(reportPath, my horoDateur() & " - Le fichier: " & newName & " à été ajouté au RIP Caldera." & linefeed, «class utf8», true) # append data to 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) # append data to the log file
end if
else
my writeto(reportPath, my horoDateur() & " - ERREUR - Le fichier: " & newName & " n'est pas du bon type (.pdf, .tif, .jpg)" & linefeed, «class utf8», true) #
end if
--supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest2)) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest1)) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
end try
end repeat
# Build a list of empty folder embedded in sourceDirectory
set workingFolder to objects of sourceDirectory result type paths list with searching subfolders, include folders and include invisible items
repeat with aFolder in workingFiles
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFolder, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"}
--set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
--display dialog dest3
--display dialog lateSubFolder
--display dialog creation date of (info for lateSubFolder)
--display dialog (creation date of (info for POSIX file dest3))
--supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest2)) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then
remove object dest2
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest1)) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then
remove object dest1
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
end try
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
#=====
on monthStamp(aDate)
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy_MM"
return ("Log_" & (theFormatter's stringFromDate:aDate)) & ".txt"
end monthStamp
#=====
GK
Last edited by gkas (2019-08-11 01:06:31 pm)
Offline
You aren't too slow.
It's just that, as I am retired, I have time available to play with posted scripts.
When I was student, I met a guy named Kerbrat but, as I am quite 77 years old I assume that you aren't this one.
I added several comments in your late code.
Applescript:
# Code Yvan/GK Script 1 --> Pdf vers Caldera V2 OK
----------------------------------------------------------------
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:gkerbrat:Desktop:PDF-PPM:0-Lambda:Entree:")
set logDirectory to POSIX path of ("Macintosh HD:Users:gkerbrat:Desktop:PDF-PPM:0-Lambda:Logs:")
set destinationDirectory to POSIX path of ("Macintosh HD:Users:gkerbrat:Desktop:PDF-PPM:0-Lambda:Caldera:")
# Date used to build the name of the log file / month
set theDate to current date
set time of theDate to 0 -- for future comparisons
set dateMinus45 to theDate - 45 * days -- for future comparisons
# Build the name of the report here because I don't like handlers used only once.
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setDateFormat:"yyyy_MM"
set reportPath to logDirectory & ("Log_" & (theFormatter's stringFromDate:aDate)) & ".txt"
if not (exists object reportPath) then
# Whe entered a new month, creates a new logfile
my writeto(reportPath, "", «class utf8», false)
end if
# Build a list of POSIX path 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
#vérifie que le fichier peut être traité par le RIP Caldera
ignoring case # So it will accept also PDF, TIFF, JPG or JPEG
set typeFileOK to (aFile ends with ".pdf") or (aFile ends with ".tif") or (aFile ends with ".tif") or (aFile ends with ".jpg") or (aFile ends with ".jpeg")
# Will be true or false
end ignoring
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFile, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"}
set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2"
set newName to my recolle(items -4 thru -1 of components, "_") --> "D1_D2_D3_oldName.pdf"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
if typeFileOK then # Means if typeFileOK is true
# Create the destination subfolders if they aren't available
#set finalDest to create folder at (destinationDirectory & "/" & partialPath) use name lateSubFolder
set finalDest to create folder at (destinationDirectory & partialPath) use name lateSubFolder --> "xxx/D1/D2/D3"
# aFile is a POSIX path. We will enter a loop wainting until it stops growing
# As far as I know we can't get the size of an object defined as a POSIX path
# so we convert it into an URL which is able to achieve this task
set theURL to (current application's class "NSURL"'s fileURLWithPath:aFile)
set theSize to -1
repeat
set {theResult, theValue, theError} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLFileSizeKey) |error|:(reference))
if theResult as boolean is false then
error (theError's localizedDescription() as text)
else
set theValue to theValue as integer
if theValue = missing value then set theValue to 0
end if
if theValue > theSize then # the file is growing
set theSize to theValue
delay 0.2 # wait a bit
else
exit repeat # Now the file is no longer growing
end if
end repeat
# Move the file
if forTests then
copy object aFile to folder finalDest new name newName
#log result
my writeto(reportPath, my horoDateur() & " - Le fichier: " & newName & " à été ajouté au RIP Caldera." & linefeed, «class utf8», true) # append data to 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) # append data to the log file
end if
else
my writeto(reportPath, my horoDateur() & " - ERREUR - Le fichier: " & newName & " n'est pas du bon type (.pdf, .tif, .jpg)" & linefeed, «class utf8», true) #
end if
--supprime les dossiers vieux de plus de 45 jours
# As I wrote in messages #30 and #34, info for POSIX file xyz fails in scripts with instruction : use framework trucmuche
# We must use an alternate syntax
if (creation date of (info for (dest3 as «class furl»))) < dateMinus45 then
# Here we are supposed to remove a folder so we must use an other instruction.
remove object dest3 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if ((creation date of (info for (dest2 as «class furl»))) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then
remove object dest2 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if ((creation date of (info for (dest1 as «class furl»))) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then
remove object dest1 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
end try
end repeat
# Build a list of empty folder embedded in sourceDirectory
set workingFolder to objects of sourceDirectory result type paths list with searching subfolders, include folders and include invisible items
repeat with aFolder in workingFiles
try
# Extract the relevant components of the path of the file
set components to my decoupe(aFolder, "/")
set {firstSubFolder, secondSubFolder, lateSubFolder} to items -4 thru -2 of components --> {"D1", "D2", "D3"}
--set partialPath to my recolle({firstSubFolder, secondSubFolder}, "/") --> "D1/D2"
set dest1 to destinationDirectory & firstSubFolder # is a POSIX path
set dest2 to dest1 & "/" & secondSubFolder # is a POSIX path
set dest3 to dest2 & "/" & lateSubFolder # is a POSIX path
--display dialog dest3
--display dialog lateSubFolder
--display dialog creation date of (info for lateSubFolder)
--display dialog (creation date of (info for POSIX file dest3))
--supprime les dossiers vieux de plus de 45 jours
if (creation date of (info for POSIX file dest3)) < dateMinus45 then
remove object dest3 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest3 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest2)) < dateMinus45) and (objects of dest2 with include folders without searching subfolders and include invisible items) = {} then
remove object dest2 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest2 & linefeed, «class utf8», true)
end if
if ((creation date of (info for POSIX file dest1)) < dateMinus45) and (objects of dest1 with include folders without searching subfolders and include invisible items) = {} then
remove object dest1 with folders included # EDITED
my writeto(reportPath, my horoDateur() & " - Dossier commande ancien supprime : " & dest1 & linefeed, «class utf8», true)
end if
end try
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
#=====
I'm not sure that I understand the problem about "file growing".
Here, before moving/copying a file, I inserted a piece of code waiting until it stop growing.
I'm not satisfied by the code cleaning old folders.
Are you sure that a folder created on today-45 doesn't contain a file created recently - say on today-5 ?
In your code, you clean the source folder. Isn't it useful to clean also the folder
("Macintosh HD:Users:gkerbrat:Desktop:PDF-PPM:0-Lambda:Caldera:")
When we have to post scripts embedding pathnames in forum, it's safe to use a syntax which doesn't display our user name. I would use something like :
Applescript:
# Working Folders
set p2d to path to desktop as text
set sourceDirectory to POSIX path of (p2d & "PDF-PPM:0-Lambda:Entree:")
set logDirectory to POSIX path of (p2d & "PDF-PPM:0-Lambda:Logs:")
set destinationDirectory to POSIX path of (p2d & "PDF-PPM:0-Lambda:Caldera:")
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 11 aout 2019 17:53:46
Offline
Hi Yvan,
Thanks for the encouragements.
Maybe you've met my father rather than me (i only going to my sixty) ?
I will check you corrections tomorowThanks again (and had edited my last post)
The script are for a workflow, and all the folders (Entree, Caldera, CalderaPPM) are always emptied every minutes by scripts or the Rip Caldera app. except the last one on remote server (images:0_PDF).
So we need only to clean the first folder of the workflow.
GK
Offline
Hi Yvan
All is fine with the script, except removing old folder.
I test with 2 folders.
D1/D2/D3/
D10/D20/D30/testfile.pdf
The first one is empty, and the second one still contains a file.
I change the date of my system momentarily on November 1st.
I display the value of the variable dest3 and the date of the corresponding folder.
The script never stop on "D3" folder because it's empty i suppose.
How can you check the folder and not only the file?
I try with folders instate without folders but it don't work.
Applescript:
set workingFiles to objects of sourceDirectory result type paths list with searching subfolders without include folders and include invisible items
Thank's for your time,
GK
Offline
As is, your late script test only files because the entry instruction:
Applescript:
set workingFiles to objects of sourceDirectory result type paths list with searching subfolders without include folders and include invisible items
return only files.
Before trying to change something I will wait answer to some questions
Is a subfolder D3 storing only one file or is it able to contain several ones ?
Is sourceDirectory containing other kind of files which must not be removed ?
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 12 aout 2019 10:03:16
Offline
Is a subfolder D3 storing only one file or is it able to contain several ones ?
Yes, the user drop several files (sometimes several hundred) in the 3rd rank folders (D3, D30, ...).
All files (except errors written in the log) are processed by the script.
This folders didn't store any files.
This folders are for workflow purpose: drop then immediately processed by the script.
Is sourceDirectory containing other kind of files which must not be removed ?
No, all folders and files > 45 days (even those in error - they are obsolete), must be deleted.
Tanks,
GK
Offline