5 Replies Latest reply: Apr 27, 2012 1:48 PM by twtwtw
Brian Mccabe Level 1 Level 1 (5 points)

This is difficult for me to explain.

I'm trying to delay the start of a Folder Action because it's starting before the file has time to finish writing.  So I've written a script to try and sample the file size, wait a few seconds then sample the file size again, compare the two sizes and as long as they are different I figure the file is still being written.  However when I implement the code below I don't see a difference in the two file sizes.  And the delay command does not seem to be working.  I put the display dialogs and the beeps in there for troubleshooting purposes.

 

on run

          tell application "Finder"

                    set myDelta to 1.0

                    set mySize1 to 1.0

                    set muSize2 to 1.0

          end tell

          repeat until myDelta = 0.0

                    tell application "Finder"

                              set myFile1 to item 1 of folder "abc" of desktop

                              set mySize1 to size of myFile1

  display dialog mySize1 as string

  beep

                              delay 8

  beep

                              set myFile2 to item 1 of folder "abc" of desktop

                              set mySize2 to size of myFile2

  display dialog mySize2 as string

                              set myDelta to mySize2 - mySize1

  display dialog myDelta as string

                    end tell

          end repeat

end run

 

Any suggestions?

Thanks.

  • 1. Re: Applescript: file size not recognized as changed in script
    Pierre L. Level 4 Level 4 (3,860 points)

    I have tested your script on a text file whose size was made (by another script) to slowly increase for about 60 seconds, and it worked flawlessly. Could it be that item 1 of folder "abc" of desktop is not the file you think it is?

     

    Of course, I had commented out the display dialogs and the beeps before running your script. I had also replaced “delay 8” with “delay 2”.

     

    Message was edited by: Pierre L.

  • 2. Re: Applescript: file size not recognized as changed in script
    Pierre L. Level 4 Level 4 (3,860 points)

    Here's a simple Folder Action script that can test if a file has a constant size:

     

    on adding folder items to this_folder after receiving added_items

        set thisFile to item 1 of added_items

        tell application "Finder"

            activate

            repeat

                set size1 to size of thisFile

                delay 1

                set size2 to size of thisFile

                if size2 = size1 then exit repeat

            end repeat

            display dialog "Done!" with icon 1

        end tell

    end adding folder items to

  • 3. Re: Applescript: file size not recognized as changed in script
    Brian Mccabe Level 1 Level 1 (5 points)

    Pierre,

     

    Thanks very much for checking out my code and for offering your own. For some reason niether seems to work for me.  As I understand it, not long ago, in Leopard, Folder Actions would start as soon as a file began to be written, which meant if you had a big file that took more than 5 seconds to write, the Folder Action would start before the file was ready.  In Snow Leopard this issue was supposedly fixed.  I find that if I'm copying a big file from a thumb drive Lion will hold off until the file is finished writing (e.g. around 1 minute), however if I'm bouncing a file from ProTools (i.e. 20 min)  the Folder Action starts immediately and the desired action does not work (even with your code or my code.)

     

    Thanks again,

    Brian

  • 4. Re: Applescript: file size not recognized as changed in script
    Pierre L. Level 4 Level 4 (3,860 points)

    I find that if I'm copying a big file from a thumb drive Lion will hold off until the file is finished writing (e.g. around 1 minute), however if I'm bouncing a file from ProTools (i.e. 20 min)  the Folder Action starts immediately and the desired action does not work (even with your code or my code.)

     

    Maybe you might want to try another version of the Folder Action script, where I've added a “with timeout” statement:

     

    on adding folder items to this_folder after receiving added_items

        set thisFile to item 1 of added_items

        with timeout of 3600 seconds

            tell application "Finder"

                activate

                repeat

                    set size1 to size of thisFile

                    delay 1

                    set size2 to size of thisFile

                    if size2 = size1 then exit repeat

                end repeat

                display dialog "Done!" with icon 1

            end tell

        end timeout

    end adding folder items to

     

    Message was edited by: Pierre L.

  • 5. Re: Applescript: file size not recognized as changed in script
    twtwtw Level 5 Level 5 (4,690 points)

    you might try using System Events rather than the Finder.  System Events is less flakey inside of Folder Actions.

     

    on adding folder items to this_folder after receiving added_items

              set thisFile to item 1 of added_items

              set oldSize to 0

              tell application "System Events"

                        repeat

                                  delay 1

                                  set currentSize to physical size of disk item thisFile

                                  if currentSize = oldSize then exit repeat

                                  set oldSize to currentSize

                        end repeat

                        display dialog "Done!" with icon 1

              end tell

    end adding folder items to