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:
----------------------------------------------------------------
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:
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:
# 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:
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