(*ftp Watcher Application
Version 1
Copyright Brian Christmas 2010-2015
*)
-- This sets how long to delay before running a Time Delay testing cycle
property TimeCycle : 20
-- This sets how long to wait before testing for matched sizes of folders
property TimeDelay : 30
-- Once a matched folder size is found, this sets how many times to cycle the TimeDelay
-- above before trying to print. If the size of the folder alters in the meantime
-- the test gets reset to 0.
-- eg if TimeDelay = 180, and SetTheConfirmCycles = 2 then the total delay is
-- 180 times 2 = 360 seconds (6 minutes). If the folder size alters, then
-- the delay gets reset to 0 and another 6 minute wait starts.
-- Setting this to 1 means no extra TimeDelay cycles will be run (just the first TimeDelay).
property SetTheConfirmCycles : 2
property SpeechFlag : false
property PrintingFlag : false
property DividerString : "•---------------------------------•"
property FileCheckString : ""
property TheFileDataName : ""
property TheFlagsDataName : ""
property pathtodesktop : path to desktop
property TimeCounter : 0
property CheckQuit : false
on run
set TheFileDataName to (path to desktop) & "ftp Downloads:AllTheftpData.dat" as text
set TheFlagsDataName to (path to desktop) & "ftp Downloads:ftp FlagsDATA.dat" as text
if ((time to GMT) div 3600) > 9 then
set TimeCycle to 20
set TimeDelay to 30
end if
my ReCycleSetUp()
end run
on ReCycleSetUp()
if (count of paragraphs in my readfile()) ≠ 0 then
delay TimeCycle
my SetUpFile2()
else
my QuitMe()
end if
end ReCycleSetUp
on SetUpFile2()
try
set FileCheckString to my readfile()
set ThisIsATempString to {}
set EraseTheFolderNames to {}
repeat with x from 1 to ((count of paragraphs in FileCheckString) div 7)
set tempStorageName to (paragraph (x * 7 - 6) of FileCheckString) as text
set TheTempFileName to (pathtodesktop & "ftp Downloads" & ":" & tempStorageName) as text
tell application "Finder"
copy size of (info for alias TheTempFileName) to tempFolderSize
end tell
set StartingSize to (paragraph (x * 7 - 5) of FileCheckString)
set TestSize to (paragraph (x * 7 - 4) of FileCheckString)
set OriginalSize to (paragraph (x * 7 - 3) of FileCheckString)
set TestDate to (paragraph (x * 7 - 2) of FileCheckString)
set ConfirmCycles to (paragraph (x * 7 - 1) of FileCheckString)
-- In case folder/file is removed from host folder
if tempFolderSize < StartingSize then
set OriginalSize to tempFolderSize - 1
set StartingSize to tempFolderSize
end if
if (current date) - (date TestDate) > TimeDelay then
if OriginalSize < tempFolderSize then
set ThisIsATempString to (ThisIsATempString & tempStorageName & return & StartingSize & return & tempFolderSize & return & tempFolderSize & return & (current date) as text) & return & 1 & return & DividerString & return
else
set ConfirmCycles to ConfirmCycles + 1
set ThisIsATempString to (ThisIsATempString & tempStorageName & return & StartingSize & return & tempFolderSize & return & OriginalSize & return & (current date) as text) & return & ConfirmCycles & return & DividerString & return
if ConfirmCycles > SetTheConfirmCycles then set end of EraseTheFolderNames to tempStorageName
end if
else
set ThisIsATempString to (ThisIsATempString & tempStorageName & return & StartingSize & return & TestSize & return & OriginalSize & return & TestDate) & return & ConfirmCycles & return & DividerString & return
end if
end repeat
my writefile3(ThisIsATempString)
if EraseTheFolderNames ≠ {} then my SetUpPrinting(EraseTheFolderNames)
if PrintingFlag then my settoprint()
on error errmsg number errNum
display dialog "SetUpFile2" & return & errmsg & return & return & errNum
end try
end SetUpFile2
------•••••••••••••••••••••••••••••••••••••••• Sub-routines ••••••••••••••••••••••••••••••••••••••••
on SetUpPrinting(EraseTheFolderNames)
try
set NewString to {}
if (count of paragraphs in FileCheckString) > 0 then
repeat with tempStorageCycle from 1 to (count of paragraphs in FileCheckString) div 7
set tempStorageName to (paragraph (tempStorageCycle * 7 - 6) of FileCheckString) as text
set StartingSize to (paragraph (tempStorageCycle * 7 - 5) of FileCheckString) as text
set testSize2 to (paragraph (tempStorageCycle * 7 - 4) of FileCheckString) as text
set OriginalSize to (paragraph (tempStorageCycle * 7 - 3) of FileCheckString) as text
set tempdate to (paragraph (tempStorageCycle * 7 - 2) of FileCheckString) as text
set ConfirmCycles to (paragraph (tempStorageCycle * 7 - 1) of FileCheckString)
if tempStorageName is in EraseTheFolderNames then
tell application "Finder"
set theFolder to pathtodesktop & "ftp Downloads:" & tempStorageName as text
set TempFileList to (get items of folder theFolder) as list
set TheByPass to true
repeat with temp in TempFileList
if name of temp ≠ "Archive" then
set TheByPass to false
exit repeat
end if
end repeat
end tell
if not TheByPass then
my writefile4(tempStorageName)
my settoprint()
end if
else
set NewString to NewString & tempStorageName & return & StartingSize & return & testSize2 & return & OriginalSize & return & tempdate & return & ConfirmCycles & return & DividerString & return
end if
end repeat
my writefile3(NewString)
end if
on error errmsg number errNum
display dialog "SetUpPrinting" & return & errmsg & return & return & errNum
end try
end SetUpPrinting
on settoprint()
try
set SpeechFlag to true
set PrintingFlag to false
tell application "Finder"
set tempflag to false # to (exists process "Mail Manager")
end tell
try
if not tempflag then
try
tell application "Dock"
activate
end tell
set theFlag to true
tell application "System Events"
tell process "Dock"
set frontmost to true
activate
tell list 1
if exists UI element "Mail Manager" then
click UI element "Mail Manager"
else
set theFlag to false
end if
end tell
end tell
if not theFlag then
try
set item_path to (path to applications folder) & "Mail Manager:Mail Manager.app" as text as alias
set item_path to POSIX path of item_path
end try
try
tell application "Dock" to quit
end try
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & item_path & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
try
tell application "Dock" to activate
end try
end if
tell process "Dock"
set frontmost to true
activate
tell list 1
if exists UI element "Mail Manager" then
click UI element "Mail Manager"
end if
end tell
end tell
end tell
on error errmsg
display dialog "ftp Fetcher error " & errmsg
end try
end if
end try
on error errmsg number errNum
display dialog "SetToPrint" & return & errmsg & return & return & errNum
end try
end settoprint
on writefile3(tempString)
set fRef to (open for access file TheFileDataName with write permission)
set eof fRef to 0
try
if tempString = {} then
write "" to fRef
else
repeat with temp from 1 to (count of paragraphs in (tempString as text))
--if (paragraph temp of (TempString as text)) ≠ "" then
write (paragraph temp of (tempString as text)) & return to fRef
--end if
end repeat
end if
close access fRef
on error errmsg number errNum
try
--display dialog errMsg & " " & errnum
if errNum = -49 then
say "file open"
close access fRef
my writefile3(tempString)
end if
end try
end try
try
close access fRef
end try
end writefile3
on writefile4(TheWriteItem)
try
#set wholeList to {}
set p to 1
try
set wholeList to read file TheFlagsDataName as list
on error
set wholeList to {}
end try
set p to 2
set end of wholeList to TheWriteItem
set p to 3
set fRef to (open for access file TheFlagsDataName with write permission)
set p to 4
set eof fRef to 0
try
write wholeList as list to fRef
close access fRef
on error errmsg
try
close access fRef
end try
display dialog errmsg
end try
on error errmsg
try
close access fRef
end try
display dialog "WriteFile4 " & errmsg & " p = " & p
end try
end writefile4
on readfile()
try
set wholeList to read file (TheFileDataName)
if (count of paragraphs of wholeList) = 0 then set wholeList to ""
return wholeList
on error
return ""
end try
end readfile
on idle
set IdleFlag to true
my ReCycleSetUp()
return TimeCycle
end idle
on QuitMe()
set CheckQuit to true
quit me
end QuitMe
on quit
if CheckQuit then
# say "ftp downloads completed"
continue quit
else
display dialog "WARNING!" & return & return & "ftp Watcher is actively monitoring ftp downloads. " & return & return & "Do you really want to Quit?" buttons {"Yes, Quit", "Don't Quit"} default button 2
set temp to button returned of the result
if temp = "Yes, Quit" then
say "Quitting"
continue quit
else
my ReCycleSetUp()
end if
end if
end quit