Curious behavior of ASObjC

I must have done something wrong but I don’t understand what.

Trying to get some infos about files stored in a disk used for Time Machine, I tried to apply a piece of ASObjC code which I use daily.
As it returned “the folder is empty”, I added some instructions grabbing the entire contents of the scanned folder.
As you may see below, the Finder returned a list of 40 aliases.
What may explain this seemingly odd behavior ?

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set sourcePath to "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Images:"

tell application "Finder"
	set entireContents to get entire contents of folder sourcePath as alias list
	
	--> {alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:2017-02-24 Envinet.pdf", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:2019-10-27 ebay.pdf", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Aperture Library.aplibrary:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Aperture Library.migratedaplibrary:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Bibliothèque Photo Booth:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Capture One Catalog.cocatalog:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Captures:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:ImageWell:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Photos Library.photoslibrary:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Snapshots:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:Wondershare UniConverter:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:à moitié.pdf", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:com.iwebvisitor.iscreenior:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:ecsb_background_tile.png", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:numérisées:", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:1 - Microcontrast Auto.cr2", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:1 - Microcontrast Auto.cr2.dop", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:2 - ClearView.jpg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:3 - PRIME.nef", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:4 - Smart Lighting SpotWAuto.rw2", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:DxO OpticsPro 11 Samples:5 - Smart Lighting Uniform.cr2", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_1.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_10.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_11.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_12.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_13.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_14.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_15.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_16.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_2.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_3.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_4.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_5.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_6.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_7.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_8.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:mojave_dynamic:mojave_dynamic_9.jpeg", alias "Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Pictures:numérisées:DSC_0164.jpg"}
end tell
log (count entireContents) --> (*40*)
--set sourcePath to "clone Macintosh HD:Macintosh …2.0:" -- used to check that the script is OK
set sourceURL to current application's NSURL's fileURLWithPath:(POSIX path of sourcePath)

-- Define several constants
set fileManager to a reference to current application's NSFileManager's defaultManager()
set skipsHiddenFiles to current application's NSDirectoryEnumerationSkipsHiddenFiles as integer --> 4
set theOptions to skipsHiddenFiles
set NSURLIsDirectoryKey to current application's NSURLIsDirectoryKey
set NSURLIsPackageKey to current application's NSURLIsPackageKey
set keysToRequest to {NSURLIsDirectoryKey, NSURLIsPackageKey}
-- Grab the content of the folder containing the backup files
--set theURLs to (fileManager's enumeratorAtURL:(sourceURL) includingPropertiesForKeys:keysToRequest options:theOptions errorHandler:(missing value))'s allObjects()
set theURLs to (fileManager's enumeratorAtURL:sourceURL includingPropertiesForKeys:keysToRequest options:0 errorHandler:(missing value))'s allObjects()
if (count theURLs) = 0 then error "Le dossier “" & sourcePath & "” est vide."
-->  "Le dossier “Seagate 3To:Backups.backupdb:iMac de Yvan:2019-11-22-134006:SSD 1000:Users:**********:Images:” est vide." number -2700
theURLs as list

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 5 juillet 2020 17:39:27

Hi Yvan.

Your code looks correct to me. :confused:

My only observation is that the error message you’re getting about the folder being empty is the one you wrote into the script. I get it myself running the code with your folder path! It seems that the enumerator is created and returns an empty array even when the folder for which it’s set doesn’t exist! Your own folder presumably does exist, since the Finder can get its files. But I thought you might be interested in this information.

Hello Nigel.

The message which you point is issued because the script doesn’t find the existing files.
It’s to prove that that I inserted an instruction, urging the Finder which I hate, to list the available items. It found 40 of them.

If you look carefully you will see a disabled instruction:

--set sourcePath to "clone Macintosh HD:Macintosh …2.0:" -- used to check that the script is OK

With this alternate path, embedded items are correctly listed.

It seems that the behavior is linked to the fact that the scanned folder is a Time Machine one : “Backups.backupdb

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 6 juillet 2020 01:00:26

I continue to search.
It really seems that the problem is related to the fact that the items which I want to list were created by Time Machine.
I know that we aren’t allowed to delete such files but, as their permissions are:

theUser (Me) :Read and Write
staff : Read only
everyone : Read only

I don’t understand why, with such permissions, they can’t be listed by ASObjC.

Am I missing something ?

Is it a way to clear the status which give the error message “Impossible d’effectuer l’opération car les éléments de sauvegarde ne peuvent pas être modifiés” when we try to delete one of them ?
At this time I don’t know which is the exact attribut ruling this status.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 6 juillet 2020 18:50:43

To find out why it’s failing, you can try getting the contents of directly non-recursively. The method for doing that will return an error if there’s a problem, and that might give you a clue:

set sourceURL to current application's NSURL's fileURLWithPath:(POSIX path of sourcePath)
set fileManager to current application's NSFileManager's defaultManager()
set {theURLs, theError} to (fileManager's contentsOfDirectoryAtURL:sourceURL includingPropertiesForKeys:(missing value) options:0 |error|:(reference))
if theURLs is missing value then error theError's localizedDescription() as text

Thank you Shane.

Your proposal let me discover what was wrong.
It displayed an error message telling that there was no “Images” folder available.
Reading that, I understood that I was using the folder name displayed in the GUI which is a localized string. Replacing the localized “Images” by the English “Pictures” make my original code doing the wanted job.

How may I missed that although the original history displayed the paths found by the Finder using the English “Pictures” ?

It would have been a good idea to ask you sooner.:rolleyes:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 10 juillet 2020 11:00:37