I have attached folder action script to an input folder. On dropping files in the input folder, the attached script is called. In the script I do some processing on incoming files.
The problem is when the dropped file is of large size, it takes some time to copy the file but the script is called immediately and the processing fails as copy is in progress. How do I deal with this? Is there a mechanism so that the script is called once file has been copied? Please advice.
Here is a piece of code which I use for this kind of problem.
on adding folder items to this_folder after receiving these_items
my germaine(this_folder, these_items)
end adding folder items to
on germaine(thisfolder, theitems)
try
tell application "System Events"
repeat with anItem in theitems
my waitForFinished(anItem)
# do your standard tasks
end repeat
end tell
end try
end germaine
on waitForFinished(aFile)
tell application "System Events"
set oldSize to -10
repeat
tell me to delay 0.1
set newSize to size of aFile
if oldSize = newSize then exit repeat
copy newSize to oldSize
end repeat
end tell
end waitForFinished
I use also an alternate one using ASObjC features.
use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework
on adding folder items to this_folder after receiving these_items
my germaine(this_folder, these_items)
end adding folder items to
on germaine(thisfolder, theitems)
try
tell application "System Events"
repeat with anItem in theitems
(my waitForSaveCompleted:(POSIX path of anItem))
# do your standard tasks
end repeat
end tell
end try
end germaine
#=====#=====#=====#=====#=====#=====
on waitForSaveCompleted:fullPosixPath
local oldSize, newSize
set oldSize to {-10}
repeat
tell me to delay 0.1
set newSize to its returnSizeFor:fullPosixPath
if oldSize = newSize then exit repeat
copy newSize to oldSize
end repeat
end waitForSaveCompleted:
on returnSizeFor:posixPath # get the size
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theSize} to aURL's getResourceValue:(specifier) forKey:(current application's NSURLFileSizeKey) |error|:(missing value)
if theSize = missing value then return {} -- because when there are none, it returns missing value
return theSize as list
end returnSizeFor:
#=====#=====#=====#=====#=====#=====
I guess that Shane will post a neater version
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 1 novembre 2016 11:12:21
delay is a feature belonging to the Osax named Standard Additions.
When we call such feature in a tell application block we must use the syntax: tell me to .
With the time passing I took the habit to use this syntax everywhere even when it’s not needed.
In my last post, the “long” syntax is the correct one in the first script because the instruction is in the tell application “System Events” block.
In the second script the long syntax is not required.
I assume that when I wrote it, I used the old fashioned one as draft and left the long syntax with no valid reason.
Happily, it doesn’t hurt.
Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 1 novembre 2016 20:55:59
Actually that’s not so, although you could be excused for thinking it is. But at some stage recently a clarification has been added to the dictionary entry:
NOTE: This is a built-in command, not a scripting addition, and is documented here for reference.