How to store values in an internal table & sort the values afterwards

Hello!

I am totally new to AppleScript and I am trying to learn it by solving a real problem.

THE PROBLEM
I use OmniFocus on my iMac as my task management tool. OF generates a lot of backup-files that fill a certain folder. This folder grows bigger and bigger.

THE GOAL
I wish to monitor the backup-folder and delete the oldest files if the backup-folder-size exceeds a certain size (just like TimeMachine does with the oldest files when the hard drive is full).

Before I have even thought about AppleScript I have imagined something like this:

   repeat until backup-folder-size < maximum-size
       delete oldest file in the backup-folder
   end-repeat

I have tried this with Hazel, but Hazel doesn’t run loops, so I thought I could make an AppleScript for the loop and put this AppleScript in a Hazel-rule.

So I have started to learn AppleScript! I have made a script that asks for the backup-folder and the wish maximum-size and even displays the folder size and the number of the files within this folders.

Right now I would like to

   a) Save the file name, the file size and the creation-date of each file in the backup-folder in an internal table;

   b) Then sort this table by creation-date;

   c) Sum the file-sizes of the files in the sorted table until the wish maximum-size is reached;

   d) Save the index of the table-element, where this happens;

   e) And finally move all the remaining files into a "to_be_deleted"-folder.

THE QUESTIONS
1.- Does something like a table / array exist in AppleScript (for a) above)? Assuming that an AppleScript list is the same as a 1-dimensional table, than a more-dimensional-table should be something like. a list of lists?! Hmmm…

2.- How does one address / load the sizes, creation-dates and the name of each file in the chosen folder in the table-elements?

3.- How does one sort this table?

Thank you for your patience :slight_smile: and suggestions!

Kind regards,
Vlad Ghitulescu

Model: iMac 3.06 GHz Intel Core 2 Duo
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)

Your task is easy because the Finder has a sort command so its easy to let the Finder sort by any of a file’s properties. In your case you want to sort by creation date, but you could sort by modification date, physical size etc.

So basically you won’t even need to create a list-of-lists and then sort it, you can do your task fairly easily. Try this and see if you can understand it…

set maxSize to 10 * (10 ^ 6) -- in bytes, this is 10 MB
set theFolder to choose folder
set toBeDeletedFolder to (path to desktop folder as text) & "To Be Deleted:"

-- make sure the toBeDeletedFolder exists
do shell script "mkdir -p " & quoted form of POSIX path of toBeDeletedFolder

set totalSize to 0
set filesToDelete to {}
tell application "Finder"
	-- get the files of the folder sorted with the newest file listed first
	set sortedFiles to files of theFolder sort by creation date
	
	-- calculate the sizes
	repeat with i from 1 to count of sortedFiles
		set totalSize to totalSize + (physical size of (item i of sortedFiles))
		
		if totalSize is greater than or equal to maxSize then
			-- get a list of the remaining files
			set filesToDelete to items i thru end of sortedFiles
			exit repeat
		end if
	end repeat
	
	-- move the remaining files to the toBeDeletedFolder folder
	move filesToDelete to folder toBeDeletedFolder
end tell

Hello, Hank!

Thank you very much for your quick reply and the solution.

I have tried this (and understood it too :slight_smile: I’m new only to AppleScript, not to programming :wink: and it works like a charm!
(a short question here: is there any posibility to stop the execution of the script to a certain statement - like in a debugger - in order to check the content of the variables? Something like stop- / pause- / brake-script & let’s say display dialog "my variable toBeDeletedFolder: " & toBeDeletedFolder ?)

Now I only have to implement it in Hazel (I will inform you about my progress) and find another interesting subject for my next AppleScript - because I like it! :slight_smile:

But only in order to fill my gaps :slight_smile: (at least some of them ;-), even if my original problem seems to be solved now: could you please help me finding an answer to my initial three questions?

1.- Does something like a table / array exist in AppleScript (for a) above)? Assuming that an AppleScript list is the same as a 1-dimensional table, than a more-dimensional-table should be something like. a list of lists?! Hmmm...

2.- How does one address / load the sizes, creation-dates and the name of each file in the chosen folder in the table-elements?

3.- How does one sort this table?

I would dare a prediction :wink:

to 1) Yes, there is something like a list-of-lists in AppleScript! :slight_smile:

to 2) Using theFolder from your suggestion:

set fileNames to (files of theFolder sort by creation date)
set fileCreationDates to (creation date of items of fileNames)
set fileSizes to (size of items of fileNames)

set table3Columns to {fileNames, fileCreationDates, fileSizes}

– here must come the sorting from point 3 below
tell application “Finder”
repeat with i from 1 to count of fileNames

    set totalSize to totalSize + (item i of fileSizes) -- with the sorted fileSizes

    if totalSize is greater than or equal to maxSize then

        set filesToDelete to items i thru end of fileNames -- with the sorted fileNames

        exit repeat

    end if

end repeat

end tell

to 3) sort table3D using fileCreationDates

Could this be true? :slight_smile:

Thanks again!

Kind regards,
Vlad Ghitulescu

Sure, there’s a couple ways. First, you can see exactly what every line is doing. At the bottom of the script window is the results. But you can change that to view events or replies. Try those views and you’ll see the details of the code as it’s running. To see the value of specific variables use the “log” statement…

log myVariable

You can stop your code at any point with the return statement. You can use return by itself to just stop the script or return a variable to view its value…

return myVariable

Yes, applescript has list of lists. To learn the basics of the language I’ll point you to the tutorials I did when I first learned. They’re short and easy. You’ll learn a lot fast. Do the ones under the section “Tutorials for Beginning Scripters”. Lesson III will show you about lists. http://macscripter.net/viewtopic.php?id=25631

I hope that helps and glad to hear you’re enjoying applescript. After you learn some of the basics, come back and ask more questions. Those tutorials should answer most of the ones you have now. When you want to find specific answers you can usually perform a search. For example I’m sure you can find code on how to sort a list of lists. But you need the basics first, so start with the tutorials.

Good morning, Hank!

Thanks again for the hints!

I have started reading the section “Tutorials for Beginning Scripters” and I have also ordered Soghoians’s book from Amazon - so I will do my homework :slight_smile:

“See you” :wink: again @my next questions!

Best regards,
Vlad Ghitulescu