Folder Action won't cease

I have developed a Folder Action with one real purpose. To print the PDF that will get dropped into it.

I will receive an e-mail, daily, with a PDF attached. I simply want the PDF attachment to be dropped into this folder, then I want the document to Print. So far, I actually have a working model. Although, there’s one “gotcha”, and maybe it stems from the “always-on” nature of the Folder Action. They’re so diligently looking to complete the task on the items that come their way, and I realize that with some actions this can be troublesome.

The issue isn’t the functionality. It’s that it doesn’t seem to stop. Preview, the app. used for the printing, comes back to life asking for a file that’s not there, after running successfully once. Here’s the flow:

1.) Drop PDF from mail to Folder, with Folder Action attached.
2.) Script will rename the dropped file to something predictable.
3.) The newly named PDF gets called to open by Preview.
4.) I use System events to conjure the Print function.
5.) I delete the file from the Folder.
6.) I quit Preview.

Maybe I should swap #5 and #6, though I can tell you that, even when I’ve done so, the “error” still occurs.

What happens is this: everything works brilliantly!! Then about 15 seconds AFTER you’re done and think you’re home free, Preview launches again and says it can’t open the file. So I’m not sure what’s going on. Maybe it’s the nature of the Folder Action.

Any simplification of this, probably overly-wordy script, would be greatly appreciated too!

Here’s what I have so far. Like I said, it works great! But the problem is it doesn’t want to STOP working…


on adding folder items to this_folder after receiving these_items
	
	set mySpot to (path to desktop as Unicode text) & "Heavenly Conditions - Grooming Reports:"
	set myFile to (path to desktop as Unicode text) & "Heavenly Conditions - Grooming Reports:Today's Grooming Report.pdf"
	
	--Look for file whose name contains Forecast & rename it to Today's Grooming Report
	tell application "Finder"
		if exists (files of folder mySpot whose name contains "Forecast") then
			set name of (files of folder mySpot whose name contains "Forecast") to "Today's Grooming Report.pdf"
		end if
	end tell
	
	--Preview opens the newly named Grooming Report
	delay 5
	tell application "Preview"
		open file myFile
	end tell
	
	--System Events prints the document
	tell application "System Events"
		tell process "Preview"
			set frontmost to true
			delay 5
			keystroke "p" using {command down}
			delay 5
			keystroke return
		end tell
	end tell
	
	--delete the Grooming Report
	delay 15
	tell application "Finder"
		delete (every file of folder mySpot)
	end tell
	
	--quit Preview
	delay 15
	tell application "Preview"
		quit
	end tell
	
end adding folder items to

FWIW, the machine that needs to run this successfully is not the machine listed in my profile. It’s an old 450 G4 running 10.4.11 and the corresponding versions of the Script Editor, Preview, etc.

Model: 2008 Mac Pro
AppleScript: 2.2.1
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

I would say you’re not taking advantage of the variables that you are passed with the folder action. First you get the folder that’s running the folder action (this_folder) and second you get a list of the files that are passed (these_items). Using them you can simplify your script. I would write your task like this, maybe it will help. Note that I haven’t tried this script…

on adding folder items to this_folder after receiving these_items
	
	repeat with anItem in these_items
		set shouldPrint to false
		tell application "Finder"
			if name of anItem contains "Forecast" then set shouldPrint to true
		end tell
		
		if shouldPrint then
			tell application "Preview"
				activate
				open anItem
			end tell
			
			--System Events prints the document
			tell application "System Events"
				tell process "Preview"
					delay 5
					keystroke "p" using command down
					delay 5
					keystroke return
					delay 5
					keystroke "w" using command down -- close the file
				end tell
			end tell
		end if
	end repeat
	
	tell application "Preview" to quit
	tell application "Finder" to delete files of this_folder
end adding folder items to

Looks promising!

I will test tomorrow morning & report back. Thanks for the input.

Best I can tell, I am up & running at the location where this script will serve. Will know more after additional testing. But preliminarily, we’re looking good.

Thanks for cleaning up. Doesn’t seem as though the “repeating” issue is present now.

Glad to hear it. Good luck. :slight_smile: