I’m attempting first script to do much of anything. I have a background in php and am tryinmgt o pick up applescript. What I need to do is copy files from a server to another server based on a modification date. The files must be files modified any time the previoius day. The script will be called from cron 5 minutes after midnight each night. My biggest hurdle has been that the weekday and month are not string data. Here’s where I am…
tell application "Finder"
set todaysDate to (get the ((current date)) - (1 * days)) as string
-- set W to weekday of todaysDate as string
-- set M to month of todaysDate as string
-- set D to day of todaysDate as string
-- set Y to year of todaysDate as string
-- set copyDate to W & M & D & Y as string
-- set filesToCopy to every file of folder "OSX:Users:mhardman:Desktop:test" whose modification date > copyDate
-- set filesToCopy to every file of folder "OSX:Users:mhardman:Desktop:test" whose modification date is copyDate
-- set filesToCopy to every file of folder "ADCOMP:Old-Way-Ads" whose modification date is todaysDate
-- duplicate filesToCopy to folder "OSX:Users:mhardman:Desktop:test2"
end tell
I’ve looked in coercion but have found nothing suited to what I’m after. Perhaps I’m REALLY missing the boat. Any help or nudge in the right direction appreciated.
Regards,
Mark
Model: 17" Powerbook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
In your script you already made the current date ( todaysDate ) into a string, so set W to weekday as string will not work. remember weekday is a date and because of your conversion, current date held in todaysDate is a string so you can not get the dateweekday from the stringtodaysDate
If you had done set W to weekday of (current date) as string then this would have worked.
In your script Just take out the as string in set todaysDate to (get the ((current date)) - (1 * days)) as string
This leaves todaysDate as a date..
Or just use the (current date) in each line. set M to month of (current date) as string
tell application "Finder"
set todaysDate to (get the ((current date)) - (1 * days))
set W to weekday of todaysDate as string
-- set M to month of todaysDate as string
-- set D to day of todaysDate as string
-- set Y to year of todaysDate as string
-- set copyDate to W & M & D & Y as string
-- set filesToCopy to every file of folder "OSX:Users:mhardman:Desktop:test" whose modification date > copyDate
-- set filesToCopy to every file of folder "OSX:Users:mhardman:Desktop:test" whose modification date is copyDate
-- set filesToCopy to every file of folder "ADCOMP:Old-Way-Ads" whose modification date is todaysDate
-- duplicate filesToCopy to folder "OSX:Users:mhardman:Desktop:test2"
end tell
if you only want to determine “yesterday”, never mind of the weekday.
This gets you the date yesterday 0:00:00
set todaysDate to date (date string of ((current date) - 1 * days))
For date comparison no coercion is needed. The following code copies every file
whose modification date is yesterday from 0:00 to 23:59
set todayDate to date (date string of (current date))
set yesterdayDate to todayDate - days
tell application "Finder"
set filesToCopy to (files of folder "OSX:Users:mhardman:Desktop:test" whose modification date > yesterdayDate and modification date < todayDate) ¬
& (files of folder "ADCOMP:Old-Way-Ads" whose modification date > yesterdayDate and modification date < todayDate)
duplicate filesToCopy to folder "OSX:Users:mhardman:Desktop:test2"
end tell
Mark and Stefan, thanks for the insight, info. and examples. This is making more sense to me now. I realize I have a long way to go to master this. I figured I was trying to over “engineer” this. Works like a charm now.
Just for academic interest on a Sunday afternoon, here are some marginally more efficient alternatives, in increasing order of speed:
-- Date subtraction + time set (+ list overhead).
tell ((current date) - days) to set {time, yesterdaysDate} to {0, it}
-- Or: tell ((current date) - days) to set {yesterdaysDate, time} to {it, 0}
-- Time get + two date subtractions.
tell ((current date) - days) to set yesterdaysDate to it - time
-- Or: tell (current date) to set yesterdaysDate to it - days - time
-- Time get + integer addition + date subtraction.
tell (current date) to set yesterdaysDate to it - (days + time)