need some help how to do this, new to applescript

Hello,

i was wondering if someone can point me to the right direction. Basically i want to do create script that will move a files from one folder to another in different one. I have folder call Backup within that folder, i another folder call Daily. I want to move the content of Daily to the folder call Daily Archive but i want to create new folder using the date, like 3/06/06 and the next day, 3/07/06. etc.

I started recording some of the step using the apple scripts but i am not sure how do i go about creating the current dates.

thanks
victor

You can get the sections of the date like this

get day of (current date)
get month of (current date)
get year of (current date)

I can’t recall off the top of my head whether or not those return as just the numbers or not. I’ll try and remember to do a quick little script when I get home (I’m at work at the moment on a Dell machine blech)

A quick example would be to tell the finder to create a new folder with name:

((month of (current date)) & “/” & (day of (current date)) & “/” & (year of (current date))

Forgive my sloppy coding, I’m tired and in pain because I hurt my back at work yesterday.

Also note that there is a scripting addition in this board’s archives that will return the month as a number instead of a name so that you can avoid all those “if” statements I have.

Also, there IS a way to tell the finder to name a folder a specific way WHEN it’s created instead of afterwards but I haven’t scripted in a long while and I couldn’t remember the syntax off the top of my head.

[code]set monthname to month of (current date) as text
if monthname = “January” then set monthnum to 1 as text
if monthname = “February” then set monthnum to 2 as text
if monthname = “March” then set monthnum to 3 as text
if monthname = “April” then set monthnum to 4 as text
if monthname = “May” then set monthnum to 5 as text
if monthname = “June” then set monthnum to 6 as text
if monthname = “July” then set monthnum to 7 as text
if monthname = “August” then set monthnum to 8 as text
if monthname = “September” then set monthnum to 9 as text
if monthname = “October” then set monthnum to 10 as text
if monthname = “November” then set monthnum to 11 as text
if monthname = “December” then set monthnum to 12 as text
set daynum to day of (current date) as text
set yearnum to year of (current date) as text

tell application “Finder”
make folder at desktop
select folder “untitled folder”
set name of selection to (monthnum & “/” & daynum & “/” & yearnum)
end tell[/code]

Hi guys.

The following works here (OS X 10.4.5) and, if memory serves, should run OK in OS 9, too. (Just yell if it falls over - and I’ll try to tweak it.)

While there are fancier ways of parsing date elements to produce a short version, the short_date handler should perform well enough. I’ve configured it so that the resulting date string is formatted as “mm/dd/yy”. (The double-character elements should result in more logical Finder sorting.)

Since the script doesn’t attempt to create more than one folder for each date, it should allow archiving to take place several times a day (if required). In other words, once the folder for the current date exists, it will be used for the rest of that day. If an attempt is made to archive an item with the same name as a previously archived file, the script will error. (This behaviour could be changed, but you’d need to specify what you want to happen in such circumstances.)

You’ll obviously need to modify the two folder paths (labelled dailyFolder and archiveFolder) before trying to run the script.

on short_date from d
	set {day:d, year:y, month:t} to d
	tell {January, February, March, April, May, June, July, August, September, October, November, December} to ¬
		repeat with m from 1 to 12
			if item m is t then exit repeat
		end repeat
	tell (1000000 + m * 10000 + d * 100 + y mod 100 as string) to text 2 thru 3 & "/" & text 4 thru 5 & "/" & text 6 thru 7
end short_date

set dailyFolder to "full path:to your:daily folder:" (* modify path as required *)
set archiveFolder to "full path:to your:archive folder:" (* modify path as required *)
set currentName to short_date from current date
tell application "Finder"
	set currentItems to items in folder dailyFolder
	tell folder archiveFolder to if exists folder currentName then
		move currentItems to folder currentName
	else
		move currentItems to (make new folder at it with properties {name:currentName})
	end if
end tell

Thank you, it is what i was looking for. it work great. i just need to figure out how i can schedule to run in the morning at 6:00am.

thanks
victor

Hi Victor. If you can get your computer to startup at that time, you could probably save the script as an application and put it in your startup items folder. :slight_smile:

Kai,

thanks. i found a utility that will run the script for me without rebooting the computer. it is call iDo Script Scheduler, i am testing right now and i will purchase it soon.

thanks for all the help.

victor

Kai,
i was wondering if you can help me with this. i was trying to use that script on someone computer to do daily back up but i get the error message “Ox//” out of memory. i was thinking that i might have to do with the name of files in the folders. is there a way to tell the script to ignore charecters like _ or filename-1-4-06?. it work fine as long there are not those kind of charecters in the files name.

thanks
victor

You can also execute a routine at a certain time by using a stay-open script application that contains an idle handler, Victor.

Not quite sure why you should be getting that error in those particular circumstances - but one way to ignore such errors would be to move the items, one at a time, in a try block. (Obviously, items that cause an error will not be moved.)

The following script (which should be saved as a stay-open application) incorporates both of these suggestions:

property dailyFolder : "full path:to your:daily folder:" (* modify path as required *)
property archiveFolder : "full path:to your:archive folder:" (* modify path as required *)
property processTime : 6 * hours
property processNow : false

on short_date from d
	set {day:d, year:y, month:t} to d
	tell {January, February, March, April, May, June, July, August, September, October, November, December} to ¬
		repeat with m from 1 to 12
			if item m is t then exit repeat
		end repeat
	tell (1000000 + m * 10000 + d * 100 + y mod 100 as string) to text 2 thru 3 & "/" & text 4 thru 5 & "/" & text 6 thru 7
end short_date

to processFiles()
	set currentName to short_date from current date
	tell application "Finder"
		tell folder archiveFolder to if exists folder currentName then
			set targetFolder to folder currentName
		else
			set targetFolder to make new folder at it with properties {name:currentName}
		end if
		repeat with currentItem in (get items in folder dailyFolder)
			try
				move currentItem to targetFolder
			end try
		end repeat
	end tell
end processFiles

on idle
	if processNow then
		processFiles()
		repeat until ((current date)'s time) > processTime
			delay 1
		end repeat
	else
		set processNow to true
	end if
	(86400 + processTime - ((current date)'s time)) mod 86400
end idle

set processNow to false