NSFileWrapper fileAttributes NSFileSize

In the process of learning the class NSFileWrapper and the instance property fileAttributes.
I made a script to compare 2 files and used NSFileSize property.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theInputFile to POSIX path of (path to desktop) & "inputFile.txt"
set theCompareFile to POSIX path of (path to desktop) & "compareFile.txt"

my compareFilesBySize:theInputFile compareFileURL:theCompareFile

on compareFilesBySize:inputFileURL compareFileURL:inputCompareURL
	set theInputURL to current application's |NSURL|'s fileURLWithPath:inputFileURL
	set theCompareURL to current application's |NSURL|'s fileURLWithPath:inputCompareURL
	set {theInputFile, theInputError} to current application's NSFileWrapper's alloc()'s initWithURL:theInputURL options:0 |error|:(reference)
	set {theCompareFile, theCompareError} to current application's NSFileWrapper's alloc()'s initWithURL:theCompareURL options:0 |error|:(reference)
	
	-- Check if the error message is missing value
	if ((theInputError is not missing value) or (theCompareError is not missing value)) then return "Couldn't find the files"
	
	set {theInputAttributes, theCompareAttributes} to {theInputFile's fileAttributes(), theCompareFile's fileAttributes()}
	
	if ((NSFileSize of theInputAttributes)'s isEqual:(NSFileSize of theCompareAttributes)) as boolean then
		display dialog "Files are equal in size"
	else
		display dialog "Files are not equal in size"
	end if
end compareFilesBySize:compareFileURL:

After input from Shane I made a other one to use the class NSURL instead.

It use Instance Method resourceValuesForKeys: and NSURLResourceKey:NSURLFileSizeKey

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theInputFile to POSIX path of (path to desktop) & "inputFile.txt"
set theCompareFile to POSIX path of (path to desktop) & "compareFile.txt"

my compareFilesBySize:theInputFile compareFileURL:theCompareFile

on compareFilesBySize:inputFileURL compareFileURL:inputCompareURL
	set theInputURL to current application's |NSURL|'s fileURLWithPath:inputFileURL
	set {inputFile, inputError} to theInputURL's resourceValuesForKeys:{"NSURLFileSizeKey"} |error|:(reference)
	set theCompareURL to current application's |NSURL|'s fileURLWithPath:inputCompareURL
	set {compareFile, compareError} to theCompareURL's resourceValuesForKeys:{"NSURLFileSizeKey"} |error|:(reference)
	
	if ((inputError is not missing value) or (compareError is not missing value)) then return "Couldn't find the files"
	
	if ((inputFile)'s isEqual:(compareFile)) as boolean then
		display dialog "Files are equal in size"
	else
		display dialog "Files are not equal in size"
	end if
end compareFilesBySize:compareFileURL:

Using NSFileWrapper like that is a rather roundabout approach. FWIW, NSFileWrapper is perhaps most commonly used for packages or folder structures.

Thanks Shane, I have not study the class NSFileWrapper that close to understand the purpose of it. I do know there are so many ways to get properties from a file. And when fileAttributes return a
dictionary string with values. I thought I could use them to do something useful.

Do you have any pointer to reference like Objective-C on the internet with examples ??

I will do some search on the internet to see if I find any example and attach it to this topic.

NSFileManager’s attributesOfItemAtPath:error: returns the same dictionary. But NSURL is the more modern way of getting the same stuff, and is generally preferred, at least in actual Objective-C code.

I’m not sure NSFileWrapper has much use for ASObjC users.

@Shane is it possible to get allKeys from NSURL NSURLResourceKey…
I like to build a array and extract everything in resource from a file.

Instead of building a array manually… :slight_smile:

No – apart from anything else, the available keys depend on things like the version of the OS.