Tuesday, June 27, 2017

#1 2017-06-06 08:48:35 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

simple script limit to a folder size

Hi people,

I've been looking for a simple 'folder action script' for a few days now but i can't find it. I tried to make one my self with the best of I found, but to bad didn't work out. Finally I found this forum.

It's just a limit on a folder size, so not more than 1GB of files. When a colleague add more than 1GB to the folder, a message indicates that the limit has been reached. 'Delete files first'

Can you please help me?

Thanks a lot!!

for example i create this! But it misses to much i think.
_____

on adding folder items to this_folder
    tell application "Finder"
        activate
        set the alert_message to the size of this_folder
       
        -- Size limit of the folder in MB
        set folder_limit to 1000
       
    end tell
end adding folder items to

Last edited by rbn_fr_utr88 (2017-06-06 09:12:00 am)

Offline

 

#2 2017-06-06 10:41:35 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

Hello

The folder action script is triggered when files are added to the folder so it's too late to decide if the max size is reached.
Here is a folder action script which tries to move added files into an embedded subfolder.
If the size of the embedded file is supposed to become too large the script warn you and stop.
In fact, it doesn't try to move the dropped files but these files and the files already available in the folder.
reason one : folder action scripts may miss some files
reason two : if we reached the limit allowed for the embedded folder we would move it to an other location and create a new subfolder in which the script will carefully move the remaining files.
I use System Events to get the size s because Finder returned missing value when I asked it for the size of a folder.

Applescript:

on adding folder items to this_folder after receiving these_items
   
   --set this_folder to (path to desktop as text) & "For ASC forums:" as alias
   
   tell application "Finder"
       set these_items to files of this_folder as alias list
   end tell
   set storageName to "storageFolder"
   set storageFolder to (this_folder as text) & storageName
   set qPosixStorage to quoted form of POSIX path of storageFolder
   
   tell application "System Events"
       set storedSize to physical size of folder storageFolder
       if storedSize > (1024 * 1024) then display dialog "Can't add new files to folder " & storageFolder & linefeed & "It remains only " & (1024 * 1024) - storedSize & " Bytes available."
       repeat with anItem in these_items
           if name of anItem is not storageName then
               set itsSize to physical size of anItem
               if (itsSize + storedSize) > (1024 * 1024) then
                   display dialog "Can't add new files to folder " & storageFolder
                   exit repeat
               end if
               do shell script "mv " & my alias2quotedPOSIX(anItem) & space & qPosixStorage
               set storedSize to storedSize + itsSize
           end if
       end repeat
   end tell
end adding folder items to


#=====#=====#=====#=====#=====#=====

on alias2quotedPOSIX(anAlias)
   return (quoted form of POSIX path of anAlias)
end alias2quotedPOSIX

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

Offline

 

#3 2017-06-06 11:41:09 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

Hi,

Thanks for your quick response. I'm not fully understand you but I get the first part, so it is not that easy or not possible at all. I've to look for a work around?

The script you just telling me is one way to do it? I tried but nothing happend when i passend the 1GB or is the 1024x1024 more?

Scipt editor tells me also:
error "De variabele this_folder is not defined." number -2753 from "this_folder"

Last edited by rbn_fr_utr88 (2017-06-06 11:42:04 am)

Offline

 

#4 2017-06-06 11:52:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: 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 :

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

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

Last edited by Yvan Koenig (2017-06-06 12:00:51 pm)

Offline

 

#5 2017-06-06 01:45:25 pm

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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.

Last edited by rbn_fr_utr88 (2017-06-06 01:46:27 pm)

Offline

 

#6 2017-06-07 03:55:20 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

rbn_fr_utr88 wrote:

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

Offline

 

#7 2017-06-07 06:02:10 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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


Filed under: folder limit

Offline

 

#8 2017-06-07 10:13:54 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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.

Applescript:

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.

Applescript:

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

Last edited by Yvan Koenig (2017-06-07 11:06:06 am)

Offline

 

#9 2017-06-07 10:33:18 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

big_smile very cool! The second one works great!!

The first one is not responding?

Many many thanks, you have been very helpful.

Offline

 

#10 2017-06-07 11:06:27 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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

Offline

 

#11 2017-06-08 02:10:12 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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.

Offline

 

#12 2017-06-08 03:58:19 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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

Applescript:

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 :

Applescript:

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

Offline

 

#13 2017-06-08 04:24:48 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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.

Offline

 

#14 2017-06-08 05:01:01 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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

Offline

 

#15 2017-06-08 07:54:49 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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.

Offline

 

#16 2017-06-08 09:28:43 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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

Offline

 

#17 2017-06-08 12:08:34 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: simple script limit to a folder size

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.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#18 2017-06-09 03:42:46 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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:)

Offline

 

#19 2017-06-09 09:40:14 am

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: simple script limit to a folder size

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/ … l?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.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#20 2017-06-10 07:56:02 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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 :

Applescript:

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

Last edited by Yvan Koenig (2017-06-12 07:12:18 am)

Offline

 

#21 2017-06-12 02:49:07 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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!

Offline

 

#22 2017-06-12 05:39:06 am

rbn_fr_utr88
Member
Registered: 2017-06-06
Posts: 11

Re: simple script limit to a folder size

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!

Offline

 

#23 2017-06-12 07:20:15 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: simple script limit to a folder size

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

Applescript:

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 :

Applescript:

set warnedSize to oneMegaByte * 800

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

Applescript:

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

Last edited by Yvan Koenig (2017-06-12 07:20:39 am)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 1.013 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)