Tuesday, December 6, 2022

Announcement

MacScripeter.net will transition to a new Discourse server soon. Watch this space for the date and time of the transition. Please expect the site to experience periods of outage during this transition. See this and this for more details.

#1 2021-04-01 03:42:43 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1092

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.

Applescript:

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

Applescript:

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:

Last edited by Fredrik71 (2021-04-01 06:21:20 am)


Node-RED makes it easy to automate IoT


Filed under: NSFileWrapper

Offline

 

#2 2021-04-01 04:30:42 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6799

Re: NSFileWrapper fileAttributes NSFileSize

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-04-01 04:56:28 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1092

Re: NSFileWrapper fileAttributes NSFileSize

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.

Shane Stanley wrote:

... NSFileWrapper is perhaps most commonly used for packages or folder structures.


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.


Node-RED makes it easy to automate IoT

Offline

 

#4 2021-04-01 05:16:31 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6799

Re: NSFileWrapper fileAttributes NSFileSize

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 Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-04-01 10:04:18 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1092

Re: NSFileWrapper fileAttributes NSFileSize

@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... smile


Node-RED makes it easy to automate IoT

Offline

 

#6 2021-04-01 05:02:33 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6799

Re: NSFileWrapper fileAttributes NSFileSize

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)