Beginner Project-Need a bit of help please

Hello all,

I just realized the amazing ability that applescript has for getting rid of mundane tasks.

At work i have to perform a weekly “Recap” of what was created over the past week. I have been trying to make this a one click operation.

I need the script to copy all files (in a specific folder) modified or created over the past week and that are certain file types (aif,mp3,wav,mov etc).

Since I only understand the basic basics i have been looking to other peoples scripts for specific lines. I used parts of Jeff Fischer’s “Copy folder tree” to help me create this so far. I have also demolished some scripts for sorting emails by date.

since i am still new to this i will explain it in straight english first cause its easier for me and then ill put the code thurr too.

so in my script i have it set up that it
1-sets the date to my format,
2-makes a new folder,
3-begins the copy folder tree,
4-asks user to set the source folder with a prompt,
5-sets the destination to the prechosen directory,
6-copies all files that meet the requirements of filetype and that were modified within
8 days of the current date,
7-copies the selections to the Destination,
8-changes the name of the destination folder to “Recap-(todaysDate)”
9-Tells me it Recap’d the Folder

here are my problems then ill paste the code–
1-anything selected on the desktop gets copied into the destination
2-when i pick big directories to copy the selected filetypes from, it either doesnt work or copies the selected files over and over into every folder within the entire directory.

How can i make it so it only copies folders in the source that have these modified files in them?? and so it doesnt paste the modified files into every other folder of the destination? the source folder i have is a big folder, fluctuating bwtn 20-40GB and even if it finds one files modified within in it, when it copies that and puts it in every empty folder at the destination it gets messy (and big).

anyone who can help at all with any of this is awesome and i appreciate you making it this far in my post!

k, here is the script-im sure it looks messy and awful but thank you-

elijah b torn
http://theycontrol.us/sarge

property gFileTypes : {“aif”, “mov”, “mp3”, “mpg”}

tell application “Finder”

set todaysDate to current date
set {m, d, y} to {month, day, year} of todaysDate
set monthList to {January, February, March, April, May, June, ¬
	July, August, September, October, November, December}
repeat with i from 1 to 12
	if m = (item i of monthList) then
		set monthString to text -2 thru -1 of ("0" & i)
		exit repeat
	end if
end repeat
set dayString to text -2 thru -1 of ("0" & d)
set yearString to text -2 thru -1 of (y as string)
set myDate to monthString & "." & dayString & "." & yearString
set olddate to ((current date) - (8 * days))



make new folder of "Macintosh HD:Users:Peoples:holdingtank:Recap"

(*begin Copy Folder Tree*)

set tSourceFolder to choose folder with prompt "Choose a folder structure to duplicate"
if tSourceFolder is false then return "User Cancelled"
set tDestFolder to "Macintosh HD:Users:Peoples:holdingtank:Recap:untitled folder"
my copyItems(tSourceFolder, tDestFolder)

end tell

on copyItems(pSourceFolder, pDestFolder)
tell application “Finder”
set olddate to ((current date) - (8 * days))

	set tSourceFolderName to name of pSourceFolder as string
	make new folder at pDestFolder with properties {name:tSourceFolderName}
	set tNewDestFolder to (folder tSourceFolderName of folder pDestFolder) as alias
	
	-- copy or move over files of the specified type(s)
	set tSelectedFiles to {every file of folder pSourceFolder whose name extension is in gFileTypes and modification date is greater than olddate}
	if tSelectedFiles ? {} then
		select {every file of folder pSourceFolder whose name extension is in gFileTypes and modification date is greater than olddate}
		copy the selection to folder tNewDestFolder
		
	end if
	
	-- copy over the folders, calling this handler recursively
	
	set tFolders to (every folder of pSourceFolder)
	repeat with tThisFolder in tFolders
		my copyItems(tThisFolder as alias, tNewDestFolder as alias)
	end repeat
	
end tell

end copyItems

tell application “Finder”

close windows


open folder "Macintosh HD:Users:Peoples:holdingtank:Recap:untitled folder"


set temp to "Recap-"
set textEntered to temp & myDate
set newfoldername to textEntered
set name of folder "Macintosh HD:Users:Peoples:holdingtank:Recap:untitled folder" to textEntered




display dialog "Recap'd the Holding Tank."

beep

end tell

(SHGb02+14a-)

Your script is a good idea and it gave me an idea for a similar task. However, I ran into the same problems that you described. Fortunately I think I found the fix.

the statement
set tSelectedFiles to {every file of folder pSourceFolder whose … }

has incorrect syntax which unfortunately Applescript doesn’t catch. The {} are wrong. It has to be () to bracket an expression in which whose-type ‘filter’ logic is used.

Here’s the quote from the AppleScript Language Guide (not that it is particularly understandable to anyone who isn’t already heavy duty into AppleScript)
“To specify a container after a filter, you must enclose the filter and the reference it applies to in parentheses.”

With the incorrect {} used then the subsequent logic
if tSelectedFiles not {}
operates incorrectly because the test is always true (why I don’t know)

When () are used i.e.
set tSelectedFiles to (every file of folder pSourceFolder whose … )

then tSelectedFiles becomes a proper and well behaved list and the subsequent if logic works as expected.

That ‘if’ logic failing would allow the subsequent select every file… and the copy the selection… to run on an empty directory. In that case Finder apparently decides to use something else that just happens to be selected. Again why, I don’t know.

Copied below is my version of your script. My tweeks besides fixing the {} to () problem
are to look for Finale files, only copy Finale files, and omit in the destination any leaf node subdirectories that contain no Finale files. The debugging ‘log’ lines could be removed.

** AppleScript source follows **

– duplicate at destination the source folder’s tree structure, omitting ‘empty’ leaf node folders, and copy all Finale files.
– Finale Mac files have file type FIN3. Finale PC created files have name extension MUS.

property gFileTypes : {“mus”}

tell application “Finder”
– (begin Copy Folder Tree)
set tSourceFolder to choose folder with prompt “Choose a folder structure to duplicate and copy Finale files”
if tSourceFolder is false then return “User Cancelled”
set tDestFolder to “Work3:XX”
my copyItems(tSourceFolder, tDestFolder)
log {“Finished”}
end tell

on copyItems(pSourceFolder, pDestFolder)
tell application “Finder”
close windows
set tSourceFolderName to name of pSourceFolder as string
make new folder at pDestFolder with properties {name:tSourceFolderName}
set tNewDestFolder to (folder tSourceFolderName of folder pDestFolder) as alias
– copy or move over files of the specified type(s) note parens () of set…to are critical syntax
set tSelectedFiles to (every file of folder pSourceFolder whose name extension is in gFileTypes or file type is “FIN3”)
log {“Class”, class of tSelectedFiles, “length”, length of tSelectedFiles}
if length of tSelectedFiles is not 0 then
set inhere to true
select (every file of folder pSourceFolder whose name extension is in gFileTypes or file type is “FIN3”)
copy the selection to folder tNewDestFolder
else
set inhere to false
end if
log {“inhere”, inhere}

	-- copy over the folders, calling this handler recursively 
	set insub to false
	set tFolders to (every folder of pSourceFolder)
	repeat with tThisFolder in tFolders
		set rresult to my copyItems(tThisFolder as alias, tNewDestFolder as alias)
		log {"rresult", rresult}
		if rresult then
			-- that folder or some sub folder of it is non empty
			set insub to true
		else
			-- that folder is empty and it has no non empty subfolder(s) so delete it
			log {tNewDestFolder, "is empty", tThisFolder}
			set delpath to ((tNewDestFolder as string) & (name of tThisFolder as string)) as alias
			delete delpath
		end if
	end repeat
	log {"inhear", inhere, "insub", insub}
	-- return status of this folder and its sub folders if any
	if (insub or inhere) then
		return true
	else
		return false
	end if
end tell

end copyItems

tell application “Finder”
close windows
open folder “Work3:XX”
end tell

cant even begin to thank you enough!

i had given up on anyone responding and on a whim decided to check it out.

thanks so much!

Elijah