Hello people, i made an folder action to be used in my Download folder which is added as a stack folder,
this action send me a growl notify every time any new files are added when i download something, is there
any way to check when a Safari download finish ? i dont want my grolnotify until the file is done downloading…
search google and this forum, but no luck in finding any info on that subject…
Here’s what you can do. Make your folder action an action that will do something when a file is added to the folder. Make the action a script that runs another script like so…
set path_to_script to "whatever"
run script file path_to_script
Then the second script (i.e. path_to_script) this…
-- this script will watch the downloads folder and alert you when the your downloads are finished
-- to use it: after a download has been started run this script.
-- how it works: when something is being downloaded the size of the folder will be changing, so this script watches for changes in the folder size and when the folder stops changing its size then the download is finished
set folder_to_watch to path to downloads folder
set time_delay to 5 -- the time between checks of the folder size
set success to my watchChangingFolderSize(folder_to_watch, time_delay)
if success then
display dialog "Success:" & return & " The items have been download." buttons {"OK"} default button 1 with icon note
else
display dialog "Error:" & return & "The script stopped for some reason." buttons {"OK"} default button 1 with icon stop
end if
on watchChangingFolderSize(theItem, time_delay) -- loop until the size of (theItem) stops changing over time
set theItem to theItem as text
set sizeThen to size of (info for file theItem) --get initial size
repeat --if they don't equal then loop until they do
try
delay time_delay
set sizeNow to size of (info for file theItem) -- get new size
if sizeNow - sizeThen = 0 then exit repeat
set sizeThen to sizeNow
on error
return false
exit repeat
end try
end repeat
return true
end watchChangingFolderSize
Thx alot regulus6633, the script is working like it is, but not like i wanted it to, im missing the names of the files being downloaded,
This is what i got so far, but for some reason i dont get any growl notify, if i use the dialog popup provived in your post it pops up for
like 0.2 sec and then it is gone…
on adding folder items to this_folder after receiving added_items
try
set folder_to_watch to path to downloads folder
set time_delay to 2 -- the time between checks of the folder size
set success to my watchChangingFolderSize(folder_to_watch, time_delay)
set notification to ""
set fileList to ("") as Unicode text
set notifyTitle to ("") as Unicode text
tell application "Finder"
set the folder_name to the name of this_folder
end tell
set the item_count to the number of items in the added_items
repeat with thisFile in added_items
tell application "Finder"
set the file_name to the name of thisFile
end tell
if the fileList is "" then
set the fileList to the fileList & file_name
else
set the fileList to the fileList & ", " & file_name
end if
end repeat
if the item_count is 1 then
set notifyTitle to "Ny fil i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
else
set notifyTitle to "Nye filer i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
end if
if success then
tell application "System Events"
if (application processes whose name is "GrowlHelperApp") is not {} then
tell application "GrowlHelperApp"
register as application "Folder Actions" all notifications {"Added File"} default notifications {"Added File"} icon of application "Finder"
notify with name "Added File" title notifyTitle description notification application name "Folder Actions" icon of file this_folder
end tell
end if
end tell
end if
end try
end adding folder items to
on watchChangingFolderSize(theItem, time_delay) -- loop until the size of (theItem) stops changing over time
set theItem to theItem as text
set sizeThen to size of (info for file theItem) --get initial size
repeat --if they don't equal then loop until they do
try
delay time_delay
set sizeNow to size of (info for file theItem) -- get new size
if sizeNow - sizeThen = 0 then exit repeat
set sizeThen to sizeNow
on error
return false
exit repeat
end try
end repeat
return true
end watchChangingFolderSize
I don’t know why it doesn’t work. If I start a download and then manually run my original script it works fine. But if I attach the script as a folder action and let it run using folder actions it doesn’t work. I’m stumped. I tried troubleshooting and found that the script stops working inside of the watchChangingFolderSize handler, but I don’t know why. It seems to get lost somewhere inside the repeat loop.
try this handler, it uses a different way to check the size
on watchChangingFolderSize(myFile, time_delay)
set F to quoted form of POSIX path of myFile
set sizeThen to first word of (do shell script "du -d 0 " & F) as integer
repeat
try
delay time_delay -- seconds
set sizeNow to first word of (do shell script "du -d 0 " & F) as integer
if sizeNow - sizeThen = 0 then return true
set sizeThen to sizeNow
on error e
if e contains "No such file" and (sizeNow - sizeThen = 0) then return true
return false
end try
end repeat
end watchChangingFolderSize
Maybe it’s better, to check each file instead of the folder,
try this, but I haven’t tested it
on adding folder items to this_folder after receiving added_items
set time_delay to 2 -- the time between checks of the file size
set fileList to {}
set the folder_name to the name of (info for this_folder)
repeat with thisFile in added_items
if watchChangingFileSize(thisFile, time_delay) then
set end of fileList to name of (info for thisFile)
end if
end repeat
if fileList is not {} then
set {TID, text item delimiters} to {text item delimiters, ", "}
set fileList to fileList as text
set text item delimiters to TID
if (count fileList) is 1 then
set notifyTitle to "Ny fil i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
else
set notifyTitle to "Nye filer i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
end if
tell application "System Events"
if (application processes whose name is "GrowlHelperApp") is not {} then
tell application "GrowlHelperApp"
register as application "Folder Actions" all notifications {"Added File"} default notifications {"Added File"} icon of application "Finder"
notify with name "Added File" title notifyTitle description notification application name "Folder Actions" icon of file this_folder
end tell
end if
end tell
end if
end adding folder items to
on watchChangingFileSize(myFile, time_delay)
set F to quoted form of POSIX path of myFile
set sizeThen to first word of (do shell script "du -d 0 " & F) as integer
repeat
try
delay time_delay -- seconds
set sizeNow to first word of (do shell script "du -d 0 " & F) as integer
if sizeNow - sizeThen = 0 then return true
set sizeThen to sizeNow
on error e
if e contains "No such file" and (sizeNow - sizeThen = 0) then return true
return false
end try
end repeat
end watchChangingFileSize
I’ve just spent a few hours trying to google a possible solution to, by the sound of it, the same problem you are having. I was attempting to write a script that transferred files from a drop box to a folder on a remote server. The script worked when it was run manually, but when attached as a folder action, it didn’t do anything. Seems that the folder window must be open for the folder action to work. http://docs.info.apple.com/article.html?path=AppleScript/2.1/en/as216.html
Why? I don’t know, but it’s frustrating as hell, not to mention impossible to do with a drop box, as far as I know.
Hope that helps.
yes frustrating as hell, if this is true, then i dont see any solution for this problem which is bad, sines i would like to hide the transfer window in Safari and only show the Growl notify on completed download… hmm
While downloading with Safari, every file gets the extension “download”.
This scripts waits until there is no download extension any more.
on adding folder items to this_folder after receiving added_items
set fileList to {}
repeat with i in added_items
set end of fileList to name of (info for i)
end repeat
repeat until added_items is {}
set L to {}
repeat with oneFile in added_items
if name extension of (info for oneFile) is "download" then set end of L to contents of oneFile
end repeat
copy L to added_items
delay 2
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set fileList to fileList as text
set text item delimiters to TID
if (count fileList) is 1 then
set notifyTitle to "Ny fil i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
else
set notifyTitle to "Nye filer i " & the folder_name & " mappe"
set notification to fileList & " blev tilføjet " & the folder_name & " mappe."
end if
tell application "System Events"
if (application processes whose name is "GrowlHelperApp") is not {} then
tell application "GrowlHelperApp"
register as application "Folder Actions" all notifications {"Added File"} default notifications {"Added File"} icon of application "Finder"
notify with name "Added File" title notifyTitle description notification application name "Folder Actions" icon of file this_folder
end tell
end if
end tell
end adding folder items to
on adding folder items to theFolder after receiving thefile
set fileDownloading to true -- assume a download is in progress
set thefile to item 1 of thefile -- thefile is a list. Multiple downloads will respond all at the end
tell application "Finder" -- Some may argue about using System Events instead.
set fileName to (name of thefile)
repeat while fileDownloading is true
set fileDownloading to (name extension of thefile is "download") -- Safari downloads have '.download' appended to the end of the name while the download is in progress. So this is looking for the received file (myDownload.dmg.download). When the download is done the extension is removed so the received file will appear to NOT exist.
if result is false then set fileDownloading to (exists file (fileName & ".part")) of theFolder
-- Firefox downloads are a combination. They put the downloaded file AND .part file (myDownload.dmg.part) in the download folder and trash the .part file when done.
end repeat
end tell
activate
say "file downloaded" -- Or call your Growl Handler
end adding folder items to
This is a rough framework but it works with Firefox and Safari downloads (unless Firefox is configured to open certain files, like .ZIPs). Multiple files will not report as they’re done but when they’re ALL finished but as I said, this is more Proof of Concept than live code.
You just need to point the end to your Growl handler.
PS:
an important thing to consider:
When the name extension changes from “download” to the original name extension,
the folder action will be triggered once again!
This might be the problem, why the other scripts don’t work
Stefan:
Are you seeing this behavior on your system? I just downloaded files from Firefox 2.0.0.8 and Safari 3.0 (522.11) and it only responded (.said “File downloaded”. ) once per download. I am on Tiger right now. Are you on Leopard?
these scripts are working as is, but as you said Stefan, the ext renaming well start the folder action once more,
so my next attempt will be launchd and then activate when new downloads are done.
sines the folder action start once more this is getting more tricky that first hoped for… darn