Script Will Not Return a Value

Hey Guys,

I am trying to return the size value and file ammount from the safari cahce folder. I can get it to work up until i try to return the com.apple.Safari folder - any ideas?:


tell application "Finder"
	
	set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/Webpage Previews/ | tail -rn1 | awk '{print \"\" $1}'")
	
	set number_of_items to (count (every item of "$HOME/Library/Caches/com.apple.Safari/Webpage Previews/"))
	activate
	
	display dialog "The Cache contains " & number_of_items & " items, which use " & Cache_size & " of disk space. "
	
end tell

if i remove the Webpage Previews and place in cache.db it returns the size. i just don’t get it - there is about 1500 files in the directory

Hello.

Before you try this: Let me tell you how I found the errors, - you know learn a man to fish :slight_smile:

I tried to run your code, well, then I looked into the event log and saw that something was wrong.
Removed the awk part: something still wrong, then sees the space in the file name, enters two backslashes to escape it. reruns, and gets more than I bargained for.
Then adds the awk statement, which doesn’t work very well. removes escaped hyphens, and then it is a success.


tell application "Finder"
	
	set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/ | tail -rn1 | awk '{print  $1}'")
	
	set number_of_items to (count (every item of "$HOME/Library/Caches/com.apple.Safari/Webpage Previews/"))
	activate
	
	display dialog "The Cache contains " & number_of_items & " items, which use " & Cache_size & " of disk space. "end tell

Best Regards ( and thanks for the nice script! (552MB!)

McUsr

Hey McUsr

Many thanks for you Help, i wouldn’t have figured that out without help.

However, i am still stuck with item count - the script is returning 55 items however there is 1500 + any ideas.

If your size return is around 550 + then i would suspect that this would be the same for you??

I think it is counting the amount of letters in my code:

set number_of_items to (count every item of “$HOME/Library/Caches/com.apple.Safari/Webpage\ Previews/”)

if i change anything in it the number goes up and down, but i cannot see the issue.

Cheers

Tried This - Can’t Get it to Work:

set number_of_items to count every item of (files in “$HOME/Library/Caches/com.apple.Safari/Webpage\ Previews”)

  • Returns - 4 Incorrect

set number_of_items to count every item of (files in “$HOME/Library/Caches/com.apple.Safari/Webpage\ Previews”)

  • Returns 0

Tried with anouther call, but no luck:



tell application "Finder"
	
	set theFolder to "$HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/"
	
	set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/ | tail -rn1 | awk '{print  $1}'")
	
	set number_of_items to count of (files in "$HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/")
	
	set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l") as integer
	
	activate
	
	display dialog "The Cache contains " & number_of_items & " items, which use " & Cache_size & " of disk space. " & numberOfFiles & ""
	
end tell

Sorry, the script I returned to you was totally wrong.

As a matter of fact I assumed the $HOME folder to work, which it didn’t, when I checked it in your other problem.


tell application "Finder"
	
	” set theFolder to "$HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/"
	
	set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/Webpage\\ Previews/ | tail -rn1 | awk '{print  $1}'")
	
	set number_of_items to (count every file in folder ((path to home folder as text) & "Library:Caches:com.apple.Safari:Webpage Previews"))
	
	-- set numberOfFiles to (do shell script "find " & quoted form of POSIX path of theFolder & " \\! -name '.*' | wc -l") as integer
	
	activate
	
	display dialog "The Cache contains " & number_of_items & " items, which use " & Cache_size & " of disk space. " -- & numberOfFiles & ""
	
end tell

I really think, you of all people, considering the stuff you are obviously accustomed to do with the shell, should really read up a little bit before hacking your way with AppleScript, which is even more fun.

You are already dangerous (to your self).

Best Regards

McUsr

McUsr

Cheers McUsr - Again your help is appreciated.

I have a few books i am trying to get through.

Hello again.

I think you really ought to look at some of the rather short articles here. And read the books later. :wink:

I believe that to be the fastest way to learn it. -And you can always bring your snippets here, when you are having some issues, and people will help you, and explain.

Best Regards

McUsr

Hi MacUsr

I have been developing this code, putting in various other elements that seem to work fine. The one issue i have though is that i cannot get an overall sum of amount that is going to be deleted.

I believe this to be due to this part of code:


set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/ | tail -rn1 | awk '{print  $1}'")

is counts it into MB or Kb

is there a way to have it count say 2 areas:


set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/ | tail -rn1 | awk '{print  $1}'")
set Cache_size2 to (do shell script "du -h $HOME/Library/Caches/Test Folder/ | tail -rn1 | awk '{print  $1}'")


display dialog "Detected Cache contains items, which use " & Cache_size + Cache_size2& "of disk space" buttons {"CleanUp", "Cancel"} default button 2

This obviously throws cannot convert to boolian

Spent a couple of hours googling with no luck, reading up around the area, i can get a result back, but still do not think its right:


	-- Count The Ammount of Space Being Taken  in each Cache
	
set Cache_size to (do shell script "du -s $HOME/Library/Caches/com.apple.Safari/ | tail -rn1 | awk '{print  $1}'")
set y to the result
set Cache_size2 to (do shell script "du -s $HOME/Library/Caches/com.apple.Safari/| tail -rn1 | awk '{print  $1}'")
set x to the result
	
	
	-- Adding All Cache Sizes Together to Display 1 Ammount
	
	set Cache_SizeOverall to x + y

display dialog "Detected Cache contains items, which use " & Cache_SizeOverall & "of disk space" buttons {"CleanUp", "Cancel"} default button 2

The Finder recognises Finder references, aliases, and occasionally HFS paths. There are no files in the text “$HOME/Library/Caches/com.apple.Safari/Webpage\ Previews/”!


tell application "Finder"
	
	--
	
	set number_of_items to count files of folder "Library:Caches:com.apple.Safari:Webpage Previews:" of home
	
	--
	
end tell

There’s no need to tell the Finder to ‘do shell script’. The command is part of the StandardAdditions.

Hi Nigel,

Thanks for the input below, have adapted my script to pull back ammounts.

However, stiull having the same issue with trying to pull back folder sizes and add them together to get an overall ammount . :confused:

Hello.

I used your “du -h” as an example which was most difficult.



-- Count The Ammount of Space Being Taken  in each Cache

set Cache_size to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/ | tail -rn1 | awk '{print  $1}'")
set y to text 1 thru -2 of Cache_size as integer
set Cache_size2 to (do shell script "du -h $HOME/Library/Caches/com.apple.Safari/| tail -rn1 | awk '{print  $1}'")
set x to text 1 thru -2 of Cache_size2 as integer



-- Adding All Cache Sizes Together to Display 1 Ammount

set Cache_SizeOverall to x + y

display dialog "Detected Cache contains items, which use  " & Cache_SizeOverall & " Mb of disk space" buttons {"CleanUp", "Cancel"} default button 2

To convert a text string that only contains numbers like your du -s example does, just do like this:


set Cache_size2 to (do shell script "du -s $HOME/Library/Caches/com.apple.Safari/| tail -rn1 | awk '{print  $1}'")
set x to Cache_size2 as integer

The operation on line 2 in the snippet above is something we do often in Apple Script, it is called coercion.

Do never however try to be smart and coerce the result of the do shell script command directly, that may lead to very strange and unforeseen results. :slight_smile: Especially in bigger script. Once it had me looking around, tearing apart and rebuilding a script for some hours, before I had it figured out.

Couple of things more. If your are on SL, then you can watch the results of your log statements in the AppleScript Editor, while your are editing it, and you can watch the same statements in the Console.log when you run your Script from your menu. Always look at the event log to check out how things are going, and use log Statements liberally.

Thanks McUser Very Much Appreciated!!

didnt think of the -2 to remove the MB text that was returned.

Cheers

Hello.

That is nice to hear, but please do yourself a favor and start using the inspection mechanisms that I mentioned above.

They should be as important to you as using

od -cb

In shell scripting to really track what is going on. Open the “skirt” of your editor window so that you can see her ankles, then save her dressing by selecting Window then “save as default”. When you ever need to expose more, then you can just drag the skirt upwards! :slight_smile: If you put your scripts in the script menu, then log statements are immensely cool if you are on Snow Leopard, as they pop up in the Console.Log

Still have an issue.

if a folder is a GB is detected the code refuses to work correctly:






-- Count The Ammount of Space Being Taken in each Cache




set Cache_size to (do shell script "du -h /System | tail -rn1 | awk '{print $1}'")

set y to text 1 thru -2 of Cache_size as integer

set Cache_size2 to (do shell script "du -h $HOME/Downloads/ | tail -rn1 | awk '{print $1}'")

set x to text 1 thru -2 of Cache_size2 as integer




-- Adding All Cache Sizes Together to Display 1 Ammount




set Cache_SizeOverall to x + y




display dialog "Detected Cache contains items, which use " & Cache_SizeOverall & " Mb of disk space" buttons {"CleanUp", "Cancel"} default button 2





Image: http://www.mediafire.com/download.php?sckrja4icku6806

I am gonna play with trying to setup a procedure that takes bytes that then does math
e.g.

set size as 1024

set finalanswer as cachesize / size - to return GB size

I guess that would be the proper way to do it.

Then you can just use the modulus operator in the end, landing on either MB or GB.

Hey McUsr -

Tried for the past 3 /4 hrs cant seem to get anything to work:



set trash_sizeKB to (do shell script "du -s /System | tail -rn1 | awk '{print \"\" $1}' ")

set y to text 1 thru -2 of trash_sizeKB as integer

set trash_sizeKB2 to (do shell script "du -s $HOME/Downloads/ | tail -rn1 | awk '{print \"\" $1}' ")

set x to text 1 thru -2 of trash_sizeKB2 as integer

set trash_sizeMB to ((trash_sizeKB / 1024 * 100000) / 100000) --MB
set trash_sizeMB to ((trash_sizeKB2 / 1024 * 100000) / 100000) --MB
set trash_sizeGB to ((trash_sizeKB / 1048576 * 100000) / 100000) --GB
set trash_sizeGB to ((trash_sizeKB2 / 1048576 * 100000) / 100000) --GB

set final to X + Y

tell application "Finder"
	set number_of_items to (count every item of the trash)
	activate
	display dialog "Test contains " & final & "of disk" with icon stop buttons {"Cancel", "Empty"} default button 2
	
end tell


Hi,

I guess you need a conversion routine depending on the last character (K, M or G),
something like this


to convertByteSize on humanReadableSize
	set unit to last character of humanReadableSize
	set num to (text 1 thru -2 of humanReadableSize) as integer
	if unit is "K" then
		return (num * 1024) as text
	else if unit is "M" then
		return (num * (1024 ^ 2)) as text
	else if unit is "G" then
		return (num * (1024 ^ 3)) as text
	end if
	return humanReadableSize
end convertByteSize