Folder Action won't work - why?

Hi,
I have a folder action tied to a script that renames files upon entry. Basically my hope was to have any new file containing the word “tahoe” that was moved into the appropriate folder on the desktop become renamed to “Forecast_” + theDateString.

If I run the script on it’s own - all is well. However, dropping the files into the folder in question does NOTHING. What am I missing here?

Here’s the script that the Folder Action is calling:

on zeroPad(theNumber)
	set theString to ""
	if theNumber is less than 10 then set theString to "0"
	set theString to theString & theNumber
	return theString
end zeroPad

set theDate to current date
set dayString to zeroPad((day of theDate) as integer)
set monthString to zeroPad((month of theDate) as integer)
set yearString to zeroPad((year of theDate) mod 100)

set theDateString to yearString & monthString & dayString

--should rename the "tahoe" file with "Forecast" + theDateString

set this_folder to ("Macintosh HD:Users:kevin:Desktop:Rename upon entry")

tell application "Finder"
	if exists (files of folder this_folder whose name contains "tahoe") then
		set name of (files of folder this_folder whose name contains "tahoe") to "Forecast_" & theDateString & ".pdf"
	end if
end tell

Any help is welcome. Thanks!

hi kevin,

you should post the actual script you are using so that people can test it. if you did post the actual script, you need to read this:

http://www.apple.com/applescript/folderactions/03.html

and use a handler like:

-- Folder Actions handler for when a folder is opened
on opening folder this_folder
	
end opening folder

cheers.

waltr,

thanks for the input. oddly enough i did just read the apple document you linked me to. so i’ll post the entire code for you. again, when ran stand-alone, the script does what it’s supposed to do. however, dropping a new file into the folder still does nothing. I just don’t know what i’m missing here.

OK. I shouldn’t say nothing happens…the first part of the following script does run: it presents me with the applescript dialog box. however, the file does NOT get renamed as i would like.

Here’s the entire script…only addition here is the first block:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set this_name to the name of this_folder
	end tell
	display dialog ((the count of these_items) as string) & ¬
		"items have been added to folder "" & this_name & ¬
		""." buttons {"OK"} default button 1
end adding folder items to

on zeroPad(theNumber)
	set theString to ""
	if theNumber is less than 10 then set theString to "0"
	set theString to theString & theNumber
	return theString
end zeroPad

set theDate to current date
set dayString to zeroPad((day of theDate) as integer)
set monthString to zeroPad((month of theDate) as integer)
set yearString to zeroPad((year of theDate) mod 100)

set theDateString to yearString & monthString & dayString

--should rename the "tahoe" file with "Forecast" + theDateString

set this_folder to ("Macintosh HD:Users:kevin:Desktop:Rename upon entry")

tell application "Finder"
	if exists (files of folder this_folder whose name contains "tahoe") then
		set name of (files of folder this_folder whose name contains "tahoe") to "Forecast_" & theDateString & ".pdf"
	end if
end tell

hi kevin,

you need to include everything in your ‘on adding folder’ handler. i changed it for testing, but you should be able to figure this out:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set this_name to the name of this_folder
	end tell
	display dialog ((the count of these_items) as string) & ¬
		"items have been added to folder "" & this_name & ¬
		""." buttons {"OK"} default button 1
	set theDate to current date
	set dayString to zeroPad((day of theDate) as integer)
	set monthString to zeroPad((month of theDate) as integer)
	set yearString to zeroPad((year of theDate) mod 100)
	
	set theDateString to yearString & monthString & dayString
	
	--should rename the "tahoe" file with "Forecast" + theDateString
	
	set this_folder to ("Macintosh HD:Users:walt:Desktop:changeName")
	
	tell application "Finder"
		if exists (files of folder this_folder whose name contains "tahoe") then
			set name of (files of folder this_folder whose name contains "tahoe") to "Forecast_" & theDateString & ".pdf"
		end if
	end tell
end adding folder items to

on zeroPad(theNumber)
	set theString to ""
	if theNumber is less than 10 then set theString to "0"
	set theString to theString & theNumber
	return theString
end zeroPad

works great!

cheers.

EDITED to add: this does not ‘create’ a pdf file out of another type of file, but if the file passed in is already a pdf, this is a good renamer.

Hi Kevin,

If there is more than one item that matches, then this will error:

set name of (files of folder this_folder whose name contains “tahoe”) to “Forecast_” & theDateString & “.pdf”

You can’t mass name items in the same folder. You need to repeat through the list of files with that name and you can’t give them all the same name.

gl,

Hi kevin,

your script as a folder action can’t work, because
everything must be within the on adding… end adding… block.
Try this (I shortened the date math a bit ;))

on adding folder items to this_folder after receiving these_items
	set dest_folder to "Macintosh HD:Users:kevin:Desktop:Rename upon entry" as alias
	tell (current date) to set theDateString to (text 3 thru 4 of (year as string)) & (text -2 thru -1 of ("0" & (its month as integer) as string)) & (text -2 thru -1 of ("0" & day as string))
	repeat with this_item in these_items
		tell application "Finder"
			set {the_Name, ext} to {name, name extension} of this_item
			if ext is not "" then set the_Name to text 1 thru ((count the_Name) - (count ext) - 1) of the_Name
			move this_item to dest_folder
			if the_Name contains "tahoe" then
				set x to 1
				repeat
					try
						set name of file the_Name of dest_folder to "Forecast_" & (x as string) & "_" & theDateString & ".pdf"
						exit repeat
					on error
						set x to x + 1
					end try
				end repeat
			end if
		end tell
	end repeat
end adding folder items to

Edit: Thanks, kel, for mentioning the renaming problem. I tried to solve it :wink:

hi again,
I just wanted to come back to say thank you for all the help. I have it up & running now with no issues. i too modified the date thing as well. It wasn’t likely to be a big issue because the file going into the folder would only appear once a day. so each file would have a unique name (dated) anyway. but i went 1 step further and added the time to the end of the date as well. furthermore i added to the script so that after the initial renaming, the PDF gets opened in adobe reader. using ‘extra suites’ i am able to print it too. then reader quits and the just-printed file gets moved into a folder inside of the original folder too.

so my whole flow is: use apple mail rule to trigger a script so that any attachments from a specified sender will be downloaded into a precise folder. (specifically, the folder with the action attached.) then the file is renamed, opened & printed. reader quits and the newly printed file is “archived” into another folder, inside the original one. i’m pretty stoked on the whole process.

again, thanks so much for all the help !! :smiley: