Discrepancy ls & du

Tell app "Finder" to set fWin to target of window 1 as text

Set res1 to word 1 of (do shell script "du -k '" & posix path of fWin & "'") 
Log res1
Set res2 to (do shell script "ls -l '" & posix path of fWin & "'")
Return res2 

I want to use ls instead of du, but the calculated file sizes of ls is an useless value. (even more if you add -h)
For a file size of 1,15 gb I get :
Ls = 15708
Du = 1120828

What’s the conversion rate? :rolleyes:

Hi. I got similar results to Fredrik71’s method with the k option, however, both sometimes disagreed with Finder’s reported results on a few image files, so caveat emptor.

(do shell script "ls -lk " & (choose file)'s POSIX path's quoted form)'s paragraphs's item 1's word 7

How does this fare on them?

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

tell application "Finder" to set fileSelection to (selection as alias list)
set theURL to current application's NSURL's fileURLWithPath:((item 1 of fileSelection)'s POSIX path)
set {theResult, theSize} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLTotalFileSizeKey) |error|:(missing value))
return theSize as real

Hi, Shane. That is much improved and aligns with Finder’s report.

Thanks for checking. The difference is that version includes any metadata, of which there’s usually some for image files.

@Shane Stanley
For some unknown reason, your code throws an error

@Marc Anthony @Fredrik71
Thanks, both your scripts return similar file sizes, similar what ls does, and I thought du would be the most accurate output?

I know the Finder was never that precise to measure file sizes (at least in the past) , but maybe this improved meanwhile, because on Mojave, du and Finder’s file sizes alinm pretty much
I hardly can believe that some Metadata is adding a whooping 400mb to the end sum, from 1.1GB (Finder, du) to 1.5 GB (ls, objC)
Now I’m confused, what method is the real thing?

Furthermore, I checked what bytes are in gb, the result is totally dissimilar. In fact, I get :
15708 byte = 0.000015708 gb
:rolleyes: