simple script limit to a folder size

The posted script is a folder action one.
It must be attached to a folder using the application “your HD:System:Library:CoreServices:Applications:Folder Actions Setup.app:” delivered by Apple.

For my tests I attached the script to a folder named “For ASC forums” available on the desktop. This folder contained a subfolder named “storageFolder

Once it’s done, if you drop a file onto the folder the script will move it into the subfolder “storageFolder” until the physical size of this subfolder reach 1024 * 1024 bytes.

My understanding is that you didn’t attach the script to the folder and tried to run it from the Editor which logically will claims that the variable this_folder is not defined.

As you spoke about “folder action script” in your original message I assumed that you were aware of what this kind of item is.
Clearly I was wrong.

Looking at AppleScript User Guide would be a good idea.

Here is a part of it :[format]Folder Actions Reference
Folder Actions is a feature of OS X that lets you associate AppleScript scripts with folders. A Folder Action script is executed when the folder to which it is attached is opened or closed, moved or resized, or has items added or removed. The script provides a handler that matches the appropriate format for the action, as described in this chapter.
Folder Actions make it easy to create hot folders that respond to external actions to trigger a workflow. For example, you can use a Folder Action script to initiate automated processing of any photo dropped in a targeted folder. A well written Folder Action script leaves the hot folder empty. This avoids repeated application of the action to the same files, and allows Folder Actions to perform more efficiently.
You can Control-click a folder to access some Folder Action features with the contextual menu in the Finder. Or you can use the Folder Actions Setup application, located in /Applications/AppleScript. This application lets you perform tasks such as the following:
Enable or disable Folder Actions.
View the folders that currently have associated scripts
View and edit the script associated with a folder.
Add folders to or remove folders from the list of folders. Associate one or more scripts with a folder.
Enable or disable all scripts associated with a folder.
Enable or disable individual scripts associated with a folder. Remove scripts associated with a folder.
Folder Actions Setup looks for scripts located in /Library/Scripts/Folder Action Scripts and ~/Library/Scripts/Folder Action Scripts. You can use the sample scripts located in /Library/Scripts/Folder Action Scripts or any scripts you have added to these locations, or you can navigate to other scripts.
A Folder Action script provides a handler (see “Handler Reference” (page 275)) that is invoked when the specified action takes place. When working with Folder Action handlers, keep in mind that:
You do not invoke Folder Actions directly. Instead, when a triggering action takes place on a folder, the associated handler is invoked automatically.

●
●
●

Folder Actions Reference
When a Folder Action handler is invoked, none of the parameters is optional. A Folder Action handler does not return a value.
Here’s how you can use a Folder Action script to perform a specific action whenever an image file is dropped on a specific image folder:

  1. Create a script with Script Editor or another script application.
  2. In that script, write a handler that conforms to the syntax documented here for the “adding folder items to” (page 285) folder action. Your handler can use the aliases that are passed to it to access the image files dropped on the folder.
  3. Save the script as a compiled script or script bundle.
  4. Put a copy of the script in /Library/Scripts/Folder Action Scripts or
    ~/Library/Scripts/Folder Action Scripts.
  5. Use the Folder Actions Setup application, located in /Applications/AppleScript, to:
    a. Enable folder actions for your image folder.
    b. Add a script to that folder, choosing the script you created.
    adding folder items to
    A script handler that is invoked after items are added to its associated folder.
    Syntax
    on adding folder items to alias after receiving listOfAlias [ statement ]…
    end [ adding folder items to ]
    Placeholders
    alias
    An alias (page 98) that identifies the folder that received the items. listOfAlias
    List of aliases that identify the items added to the folder.
    statement
    Any AppleScript statement.
    Examples
    The following Folder Action handler is triggered when items are added to the folder to which it is attached. It makes an archived copy, in ZIP format, of the individual items added to the attached folder. Archived files are placed in a folder named Done within the attached folder.

●
●

Folder Actions Reference

on adding folder items to this_folder after receiving these_items
tell application “Finder”
if not (exists folder “Done” of this_folder) then
make new folder at this_folder with properties {name:“Done”}
end if
set the destination_folder to folder “Done” of this_folder as alias
set the destination_directory to POSIX path of the destination_folder
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder and ¬
the name extension of the item_info is not in {“zip”, “sit”} then
set the item_path to the quoted form of the POSIX path of this_item
set the destination_path to the quoted form of ¬
(destination_directory & (name of the item_info) & “.zip”)
do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " ¬
& item_path & " " & destination_path)
end if
end repeat
end adding folder items to[/format]

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 6 juin 2017 18:51:57

Ah great I’ll take a look, sorry I was assuming that it was just copy paste a right format(script) into script editor, than save the script, save the scipt into right folder like:/Library/Scripts/Folder Action Scripts

Than just add it the selfmade script at the right folder, and done! Like you can add a standard apple script.

I’m not sure that I understand well the last sentence.

Attaching a folder action to a folder doesn’t mean “add it at the right folder”.
We don’t create a physical link between a script and a folder but we create a logical one.
As I carefully wrote, to do that we must use the application “your HD:System:Library:CoreServices:Applications:Folder Actions Setup.app:” delivered by Apple.
It offer two panes.
Click the [+] button below the left one to navigate and select to the folder in which you plan to drop the files.
Then click the [+] button below the right pane. The pane will display the list of available folder action scripts allowing you to select the one to attach.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 7 juin 2017 10:55:11

OK I get that one!

May I ask you one more question? Because maybe just an attentions telling 1Gb is the limit will be enough for me. So i took one like apple, but I need one extra line on the end tells it to open ‘get info’ so people can watch how much they have in there folder.

Look here:

property dialog_timeout : 30 – set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
try
tell application “Finder”
–get the name of the folder
set the folder_name to the name of this_folder
end tell

	-- find out how many new items have been placed in the folder
	set the item_count to the number of items in the added_items
	--create the alert string
	set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
	if the item_count is greater than 1 then
		set alert_message to alert_message & (the item_count as text) & " the maximum size of this "
	else
		set alert_message to alert_message & "the maximum size of this "
	end if
	set alert_message to alert_message & "folder is 1.0 GB " & «data utxt201C» & the folder_name & «data utxt201D» & "."
	set the alert_message to (the alert_message & return & return & "Would you like to view folder size?")
	
	display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
	set the user_choice to the button returned of the result
	
	if user_choice is "Yes" then
		tell application "Finder"
			--go to the desktop 
			activate
			--open the folder
			open this_folder
			--select the items
			reveal the added_items

??? maybe here?

		end tell
	end if
end try

end adding folder items to

OK. Your script doesn’t limit the size of the folder, it just display its size.

Displaying the info window can’t be made where you proposed. Triggering it at this step would display an info window for each added item.
Here is a script doing the job.

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	(*
set this_folder to (path to desktop as text) & "For ASC forums:"
set added_items to {alias (this_folder & "S-Agreement_liet - копия.txt"), alias (this_folder & "S-Agreement_lthuanian.txt"), alias (this_folder & "StorageFolder")}
set this_folder to this_folder as alias
*)
	try
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell
		
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in the added_items
		--create the alert string
		set alert_message to ("Folder Actions Alert:" & return & return) & "The folder " & «data utxt201C» & the folder_name & «data utxt201D» & " received "
		if the item_count is greater than 1 then
			set alert_message to alert_message & item_count & " items."
		else
			set alert_message to alert_message & "one item."
		end if
		set alert_message to alert_message & " The maximum size of this folder is 1.0 GB."
		set the alert_message to (the alert_message & return & return & "Would you like to view folder size?")
		
		display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
		set the user_choice to the button returned of the result
		
		if user_choice is "Yes" then
			tell application "Finder"
				activate
				--open the folder
				set the selection to this_folder
				# Open the info window of the folder
				tell application "System Events" to tell process "Finder"
					set frontmost to true
					keystroke "i" using {command down}
				end tell
				
				open this_folder
				--select the items
				reveal the added_items
				
				# If we issue keystroke "i" using {command down} here, it will apply to every added items
			end tell
			
		end if
	end try
end adding folder items to

Here is an alternate version which display the size of the folder without displaying the info window.

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell
		
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in the added_items
		--create the alert string
		set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
		if the item_count is greater than 1 then
			set alert_message to alert_message & (the item_count as text) & " the maximum size of this "
		else
			set alert_message to alert_message & "the maximum size of this "
		end if
		set alert_message to alert_message & "folder is 1.0 GB " & «data utxt201C» & the folder_name & «data utxt201D» & "."
		set the alert_message to (the alert_message & return & return & "Would you like to view folder size?")
		
		display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
		set the user_choice to the button returned of the result
		
		if user_choice is "Yes" then
			tell application "System Events"
				set theSize to size of this_folder
				set currentSize to physical size of this_folder
			end tell
			set MBsize to (round (currentSize / (1024 * 1024) / 0.01)) * 0.01
			tell application "Finder"
				--go to the desktop 
				activate
				--open the folder
				open this_folder
				--select the items
				reveal the added_items
				
				#?????????? maybe here?
				display dialog "At this time, the size of " & «data utxt201C» & the folder_name & «data utxt201D» & " is : " & theSize & " bytes (" & MBsize & " Mbytes)"
			end tell
		end if
	end try
end adding folder items to

Please, when you post a script here, use the dedicated markers by clicking the button [AppleScript] before pasting.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 7 juin 2017 17:11:48

:smiley: very cool! The second one works great!!

The first one is not responding?

Many many thanks, you have been very helpful.

Oops, I failed to disable instructions used during tests.
I edited the message #8 accordingly.

Yvan KOENIG (VALLAURIS, France) mercredi 7 juin 2017 18:04:05

Yes men!! This will do the job, slightly less strict than putting a limit. Now I only have to check every month to keep it up;) but they know that this is a different folder than normal and only used for a few documents.

So thanks agian, you where very helpfull.

I’m wondering if displaying the infos after every drag/drop action.
Below are the two versions modified so that the infos are displayed only if the physical size of the folder is greater than a predefined value.
Here I use : set warnedSize to (maxSize * 0.8) as integer

Version using the info window

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	(*
set this_folder to (path to desktop as text) & "For ASC forums:"
set added_items to {alias (this_folder & "S-Agreement_liet - копия.txt"), alias (this_folder & "S-Agreement_lthuanian.txt"), alias (this_folder & "StorageFolder")}
set this_folder to this_folder as alias
*)
	set maxSize to 1024 * 1024
	set warnedSize to (maxSize * 0.8) as integer
	try
		tell application "System Events"
			set theSize to size of this_folder
			set currentSize to physical size of this_folder
		end tell
		if currentSize > warnedSize then
			tell application "Finder"
				--get the name of the folder
				set the folder_name to the name of this_folder
			end tell
			
			-- find out how many new items have been placed in the folder
			set the item_count to the number of items in the added_items
			--create the alert string
			set alert_message to ("Folder Actions Alert:" & return & return) & "The folder " & «data utxt201C» & the folder_name & «data utxt201D» & " received "
			if the item_count is greater than 1 then
				set alert_message to alert_message & item_count & " items."
			else
				set alert_message to alert_message & "one item."
			end if
			set alert_message to alert_message & " The maximum size of this folder is 1.0 GB."
			set the alert_message to (the alert_message & return & return & "Would you like to view folder size?")
			
			display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
			set the user_choice to the button returned of the result
			
			if user_choice is "Yes" then
				tell application "Finder"
					activate
					--open the folder
					set the selection to this_folder
					# Open the info window of the folder
					tell application "System Events" to tell process "Finder"
						set frontmost to true
						keystroke "i" using {command down}
					end tell
					
					open this_folder
					--select the items
					reveal the added_items
					
					# If we issue keystroke "i" using {command down} here, it will apply to every added items
				end tell
			end if
		end if
	end try
end adding folder items to

Version displaying the size in a dialog :

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
	set maxSize to 1024 * 1024
	set warnedSize to (maxSize * 0.8) as integer
	try
		tell application "System Events"
			set theSize to size of this_folder
			set currentSize to physical size of this_folder
		end tell
		if currentSize > warnedSize then
			tell application "Finder"
				--get the name of the folder
				set the folder_name to the name of this_folder
			end tell
			
			-- find out how many new items have been placed in the folder
			set the item_count to the number of items in the added_items
			--create the alert string
			set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
			if the item_count is greater than 1 then
				set alert_message to alert_message & (the item_count as text) & " the maximum size of this "
			else
				set alert_message to alert_message & "the maximum size of this "
			end if
			set alert_message to alert_message & "folder is 1.0 GB " & «data utxt201C» & the folder_name & «data utxt201D» & "."
			set the alert_message to (the alert_message & return & return & "Would you like to view folder size?")
			
			display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
			set the user_choice to the button returned of the result
			
			if user_choice is "Yes" then
				
				set MBsize to (round (currentSize / maxSize / 0.01)) * 0.01
				tell application "Finder"
					--go to the desktop 
					activate
					--open the folder
					open this_folder
					--select the items
					reveal the added_items
					
					#?????????? maybe here?
					display dialog "At this time, the size of " & «data utxt201C» & the folder_name & «data utxt201D» & " is : " & theSize & " bytes (" & MBsize & " Mbytes)"
				end tell
			end if
		end if
	end try
end adding folder items to

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 8 juin 2017 10:58:02

Oh I’ll test these ones:) sounds better because than its more important to know.
And some people are connected from different mac’s is it olso possible to set it stuck to the folder so on the mac that is currently does the change get the warning on there screen:P?

Otherwise I’ll do lokal changes.

I apologizes but my old scholar english knowledge doesn’t allow me to understand your last question.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 8 juin 2017 12:00:52

Haha I think its more my Englisch skills, but we came already very far!

What I was trying to say is, it’s a shared server. So when I’m doing it for each personal folder of the employees. I also get all the notifications. What in the last form you’ve sent me is perfect, but when employees can get a message to, it’s the best.

Even though I can add it locally to everyone. Unless you have one more genius idea? But otherwise perfectly fine!

Kind regards Robin.

OK.
As I know absolutely nothing about servers, I am unable to help you in this area.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 8 juin 2017 16:28:34

Is this OSX sharing folders to user via AFP? I’m not sure, but I think that if you just go through the process of attaching the scripts to the folders on the user’s computers, they should run local to them and give them the notifications.

  • Tom.

Hi Tom,

Yes, that’s what I mean. Ah ok, I thought so. Then I have 2 options or let me get notified when a co-worker exceeds the limit or I activate the script at any mac and they get the message.

Thanks:)

If you want to get fancy with it and save yourself from having to set up the script on every user’s computer, it appears that if the file server host is running OSX Server, you can script using the command line to send alert messages via AFP to the user’s computer:

https://www.manualslib.com/manual/8664/Apple-Mac-Os-X-Server.html?page=142

So that would probably be the most elegant solution - only attach the scripts to the folders at your end (on the server), and have the script both display a local message to you when the user goes over, and also use AFP to broadcast an alert to the user’s machine.

  • Tom.

I played again with this script.
I disliked the fact that large sizes were displayed with the scientific format : 7.61364945E+8
So I borrowed an ASObjC handler written by Shane STANLEY.
The code became :

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.
-- Simple number formatter borrowed from Shane STANLEY
property theNSNumberFormatter : missing value
on formatNumber:n
	if my theNSNumberFormatter = missing value then
		-- make a date formatter
		set my theNSNumberFormatter to current application's NSNumberFormatter's alloc()'s init()
		tell my theNSNumberFormatter -- configure the formatter
			its setLocale:(current application's NSLocale's alloc()'s initWithLocaleIdentifier:"fr")
			its setNumberStyle:(current application's NSNumberFormatterDecimalStyle)
			--its setMinimumFractionDigits:0
			--its setMaximumFractionDigits:0
			its setMinimumIntegerDigits:1
			its setRoundingMode:(current application's NSNumberFormatterRoundHalfUp)
			its setHasThousandSeparators:true
		end tell
	end if
	-- tell the formatter to format the number
	return (theNSNumberFormatter's stringFromNumber:n) as text
end formatNumber:

on adding folder items to this_folder after receiving added_items
	(*
set this_folder to "Macintosh HD:Users:Important:pour ebay:"
set added_items to {this_folder & "_X28706Æ’YK" as alias, this_folder & "_X28737Æ’YK" as alias}
set this_folder to this_folder as alias
*)
	
	set oneMegaByte to 1024 * 1024 # 1 Mbyte
	set warnedSize to oneMegaByte * 800
	try
		tell application "System Events"
			set theSize to size of this_folder
			set currentSize to physical size of this_folder
		end tell
		if currentSize > warnedSize then # OOPS, EDITED
			set MBsize to (round (currentSize / oneMegaByte / 0.01)) * 0.01 # to display Mbytes with two decimal digits.
			tell application "Finder"
				--get the name of the folder
				set folder_name to name of this_folder
			end tell
			
			-- find out how many new items have been placed in the folder
			set item_count to count added_items
			--create the alert string
			set alert_message to "Folder Actions Alert:" & linefeed & linefeed & "The folder " & «data utxt201C» & folder_name & «data utxt201D» & " whose maximum size is " & 1.0 & " GB, received "
			if item_count is greater than 1 then
				set alert_message to alert_message & item_count & " items."
			else
				set alert_message to alert_message & "one item."
			end if
			set theSize to my formatNumber:theSize
			if MBsize > 1 then
				set alert_message to alert_message & linefeed & "Its current size is : " & theSize & " bytes (" & MBsize & " Mbytes)"
			else
				set alert_message to alert_message & linefeed & "Its current size is : " & theSize & " bytes (" & MBsize & " Mbyte)"
			end if
			set alert_message to (alert_message & linefeed & linefeed & "Would you like to view the folder's content?")
			
			display dialog alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
			set user_choice to button returned of result
			
			if user_choice is "Yes" then
				
				tell application "Finder"
					--go to the desktop 
					activate
					--open the folder
					open this_folder
					--select the items
					reveal added_items
				end tell
			end if
		end if
	end try
end adding folder items to

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 10 juin 2017 14:55:29

Goodmoring @t.spoon i wasn’t able to looking into it last week. This week I will. So very thanks for the help it would be sure more elagant if it work that way:) let you if its work out.

@Yvan haha I was thinking the same but I was very glad you helpt me already this much.

Im gonna try/test my self today or tomorrow!

Ha Yvan, yes that looks much better! Could I get this one displaying the infos only if the physical size of the folder is greater than the predefined value?

Now its displaying the infos after every drop action and stops when the size is bigger than 1GB!

Oops, for tests I inverted the comparison and forgot to reset the original instruction.
The message is edited at the instruction :

 if currentSize > warnedSize then # OOPS, EDITED

As is, the size is displayed when the real size is greater than 800 MBytes.
If you want to change that, you must edit the instruction :

set warnedSize to oneMegaByte * 800

If you want to be warned only when the size is greater than one GByte, edit it as :

set warnedSize to oneMegaByte * 1024

I just thought that it may be useful to be alerted a bit before reaching the limit but 800 MByte may be too small as the size forcing the script to warn.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 12 juin 2017 14:13:49