Script Problem?

When I run the following script, it never works on the first run but it does work on the second run

tell application "Finder"
        set x to ":Users:name:Desktop:folder:" as alias
        get physical size of item x as integer
        set y to result
        display dialog y
end tell

I’ve tried several different ways and in every case it’s a no go on the first run, but it works on the second run. What gives?

Similarly…

tell application "Finder"
	set x to choose folder
	get size of folder x
	display dialog result
end tell

Gets the same error “msng” on the first run, but works on the second run.

Any difference with these? Is there anything going on with the target folder (recently changed contents or contents that are changing as the script runs)?

set x to ":Users:rjj:Desktop:Path To Codes 9X:" as alias
tell application "Finder"
	get (physical size of x) as integer
	set y to result
	display dialog y
end tell
set x to choose folder
tell application "Finder"
	get size of x
	display dialog result
end tell

If I run your version it works on the first time, but if I add an empty untitled folder to the target folder and run the script again, it errors. Run it once again and all is well.
Someone else mentioned that it looks like it might be a Finder bug, maybe the Finder is not updating a folders info fast enough. I’m not sure if that’s the case though.

Try adding ‘update x’ before getting the size.

I just tried update x and update folder x, it works kinda –

  1. run the script and it works
  2. add a folder to target and it works
  3. remove the added folder and it errors – “missing value”

I’m stumped…

set x to "MyMac:Users:myname:Desktop:Scripts:" as alias
tell application "Finder"
	activate
	update folder x
	get (physical size of x) as integer
	set y to result
	display dialog y
end tell

So it looks like it works when you add items to the target folder, but it errors when you remove something from the target folder.

missing value
		"Can't make missing value into a integer."

Maybe something like this will work.

set x to "MyMac:Users:myname:Desktop:Scripts:" as alias
set y to ""

tell application "Finder"
	activate
	
	repeat while y is "" or y is missing value
		update x
		set y to (physical size of x)
	end repeat
	
	display dialog y
end tell

Thanks Rob, that does the trick. I still wonder why it behaves the way it does for the previous scripts. :?

I think that the OS X Finder is just a slug! Maybe that’s why Apple has started moving some of its responsibilities to System Events.

My own, personal theory is that it’s precisely because some of the Finder’s functions have been moved to a separate app that the problems occur. Maybe the Finder’s subcontracting the work and not getting the results back in time. I can reproduce Greg’s results exactly on my machine using the Finder, but the following works perfectly every time (so far!):

set x to "MyDisk:Users:myname:Desktop:Scripts:"
tell application "System Events"
  activate -- for display dialog's benefit
  set y to physical size of file x
  display dialog y
end

Interesting. I tried the same code before posting the comment about Finder being a slug and I get “The variable y is not defined.” I’m running the beta version of System Events (UI scripting).

Rob, I get the same result on Nigel’s script – “The variable y is not defined.”

10.2.6 – AS 2.0b – System Events 1.2

Hmmm. I’m using the standard installation for 10.2.6, which includes System Events 1.1.2. I also have a couple of third-party OSAXen installed, but I can’t see how they would make any difference here. In fact, I can’t see how y could be undefined. If the previous line failed to define it, there should have been an error. Very odd. :-

If I run only:

set x to "path:to:some:folder:"
tell application "System Events" to set y to physical size of file x

The only thing that happens is that x is initialized. No error whatsoever. Odd indeed.

(This is a follow up to my previous post, not to Rob’s reply.)

OK. Concerned that I might be posting scripts that only work on my machine, I’ve gone back had another look. Firstly, I uninstalled my third-party OSAXen and rebooted. The script I posted still happily displays a number. However, this number is ridiculously huge: 2760353090. The info window for the folder on my desktop shows “1.8 MB on disk (1,622,795 bytes)”.

I can get these figures by going back to the Finder:

set x to (path to desktop as string) & "Scripts:"
tell application "Finder"
  activate
  set y to (physical size of folder x) / 1024 / 1024
  display dialog y
end tell
--> 1.8203125

tell application "Finder"
  activate
  set y to size of folder x
  display dialog y
end tell
--> 1622795

These work both in Script Editor (1.9) and as free-standing applications. After a reboot, or after changing and restoring the folder contents, I get ‘missing value’ first time, like Greg. (Interestingly, the figures sometimes vary by several bytes, depending on whether I put something into the folder and take it out again, or take something out and then put it back.)

So Rob’s approach seems to be the best. This version of it works on my machine and looks fairly formal and elegant: :slight_smile:

tell application "Finder"
  activate
  get properties of folder x -- not 'update'
  set y to physical size of folder x -- not 'physical size of result'
  display dialog y
end tell

I think the difference between ‘get properties of’ and ‘update’ is that the latter only affects the display of the item on screen.

The first time I ran the new script, it erred with the same old “missing value” on almost but not every new folder I tried it on – the first time only. But now it seems to work on every run on every folder. I tried logging out, then back in and all is well.

tell application "Finder"
	activate
	set x to choose folder
	get properties of folder x -- not 'update' 
	set y to physical size of folder x -- not 'physical size of result' 
	display dialog y
end tell

Thanks Nigel and Rob, this has been a lot of fun ;¬)
There is another discussion about this over at Mac OS Hints


set x to choose folder
tell application "Finder"
	set y to the ((size of x) / 1000 & "k") as string
	display dialog y
end tell

this updates the folder i choose everytime, even when adding in extra files. also you can use the round funtion to round the k number if necessary

Andy, it fails on the first run of every folder I select, but succeeds on the second run of the same folder…

		missing value
		"Can't make missing value into a real."

Mac OS X 10.2.6 – AS 2.0b – System Events 1.2

maybe wrap it in a try statement


set x to choose folder
try
	tell application "Finder"
		set y to the ((size of x) / 1000 & "k") as string
		display dialog y
	end tell
	
on error
	tell application "Finder"
		set y to the ((size of x) / 1000 & "k") as string
		display dialog y
	end tell
end try