Hi All,
Working on a script to check for newly added folders to a drive. (in my scenario job folders) and log the names of the folders for the current day to a text file.
Processes of script.
- Determine source drive.
- List all folders
- Check creation date against current date.
- For folders that were created today (current date) - write the name of the folder to a text file.
- Tally the number of folders created today and write this to the same text file.
Issues.
- Being a novice scripter the script seems quite repeditive - 3 instances where I use the "set XLF to open … "
- Folders are on the root level of the hard drive - I can’t seem to nut out the delimiter scheme to log just the file name (not dirve : filename)
- there is a colon appearing in my text file after the number count variable - I don’t know what’s generating this.
tell application "Finder"
--set fldr_list to every folder of disk "video"
set fldr_list to every folder of disk "Schools"
end tell
set jobsadd to ""
set daydate to (date string of (current date))
-- target file variable
set thelogfile to ((path to desktop as Unicode text) & "Job Log.txt")
set XLF to open for access thelogfile with write permission
try
write daydate & return to XLF starting at eof
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
repeat with afolder in fldr_list
set file_info to info for (afolder as alias)
set fldrname to afolder as alias
-- display dialog fldrname as string
-- Convert the full alias string to job name only
-- set AppleScript's text item delimiters to {":"}
-- set JobName to (text items 2 through -3 of fldrname) as string
-- set AppleScript's text item delimiters to {""}
-- display dialog JobName as string
set fldrcrtdate to (date string of (creation date)) of file_info
-- Find the job folders created today
if the fldrcrtdate is daydate then
set NewLine to (fldrname as string) & return
-- display dialog NewLine as string
set jobsadd to (jobsadd + 1)
end if
set XLF to open for access thelogfile with write permission
try
write NewLine to XLF starting at eof
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
end repeat
if jobsadd is greater than 0 then
display dialog "There has been " & jobsadd & " new jobs added today" as string giving up after 5
set XLF to open for access thelogfile with write permission
try
write return & {jobsadd, " new jobs added today" as text} to XLF starting at eof
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
else
display dialog "No new jobs, what have guys been doing ???" giving up after 5
end if
Text file returns :
Sunday, 6 April 2008 ← how do I add a return after this line in the code ??
Schools:1234 test:
Schools:1235 test:
Schools:3443 tet:
Schools:4534 test:
4: new jobs added today ← where is the “:” coming from ??
Any tips on script optimization greatly appreciated.
Hi,
try this
global thelogfile
set thelogfile to ((path to desktop as Unicode text) & "Job Log.txt")
tell application "Finder"
set fldr_list to every folder of disk "Schools"
end tell
set jobsadd to ""
tell (current date) to set daydate to (it - (its time))
report(date string of daydate & return)
repeat with afolder in fldr_list
tell afolder to set {fldrname, fldrcrtdate} to {it as text, creation date}
-- Find the job folders created today
if fldrcrtdate > daydate then
report(fldrname)
set jobsadd to jobsadd + 1
end if
end repeat
if jobsadd is greater than 0 then
display dialog "There has been " & jobsadd & " new jobs added today" as string giving up after 5
report(return & jobsadd & " new jobs added today")
else
display dialog "No new jobs, what have guys been doing ???" giving up after 5
end if
on report(theMessage)
try
set logStream to open for access file thelogfile with write permission
write theMessage & return to logStream starting at eof
close access logStream
on error errorText number errorNumber
try
close access file logFile
end try
end try
end report
The unexpected colon comes from “uncontrolled” text item delimiters
Thanks Stefan,
I need to spend some time on sub routines. This is much cleaner.
Back to the delimiter issue. The text file is still listing entries as : “: ”
Sample.
Sunday, 6 April 2008
Schools:1234 test:
Schools:1235 test:
Schools:3443 tet:
Schools:4534 test:
4 new jobs added today
Is there a way that I can correct this code :
set AppleScript's text item delimiters to {":"}
set JobName to (text items 2 through -3 of fldrname) as string
set AppleScript's text item delimiters to {""}
display dialog JobName as string
To strip out the drive name in the text file entry.
This code worked for me in another script where the folder was on the root level of the hard drive, but in this script I get an error "can’t get text item 2 of “schools:1234 test” (or similar).
Thanks again.
Matt.
sorry, I missed that, just replace
tell afolder to set {fldrname, fldrcrtdate} to {it as text, creation date}
with
tell afolder to set {fldrname, fldrcrtdate} to {name, creation date}
Here an optimized version of the script
global thelogfile
set thelogfile to ((path to desktop as Unicode text) & "Job Log.txt")
tell (current date) to set daydate to (it - (its time))
tell application "Finder" to set fldr_list to name of every folder of disk "Schools" whose creation date > daydate
if fldr_list is not {} then
set jobsadd to (count fldr_list) as text
set {TID, text item delimiters} to {text item delimiters, return}
set fldr_list to fldr_list as text
set text item delimiters to TID
report(date string of daydate & return & return & fldr_list & return & return & jobsadd & " new jobs added today" & return)
display dialog "There has been " & jobsadd & " new jobs added today" as string giving up after 5
else
display dialog "No new jobs, what have guys been doing ???" giving up after 5
end if
on report(theMessage)
try
set logStream to open for access file thelogfile with write permission
write theMessage & return to logStream starting at eof
close access logStream
on error errorText number errorNumber
try
close access file logFile
end try
end try
end report