AppleScript to write an AppleScript in Script Editor

Talk about recursive…

background:

My company has an online store. My department works on the web end of the store. Each week new products come in, photos are taken, cleaned up and then posted to the store web site. Part of the production process involves me assigning who does what. I wrote an AS to help manage this process, it automates selecting who is going to work on the next item, creates the various storage folders writes a record to our production database (MySQL) and then jets off an email to the entire production staff (photographers, project managers, designers) letting them know the storage folders are ready and who is doing what.

production happens. sometimes things are fast and furious, sometimes slow and boring. In either instance the designers need to know when files are being dumped into the storage folders and available to be worked on. I want to add a segment to my existing AppleScript which will attach a folder action to these storage folders that would then send an email to the individual designer letting them know product images are ready to be worked on.

OK, attaching the folder action through a script is easy, you can do that with System Events.

I’m assuming, because I want this to be dynamic (different mail recipients, different content) that I have to write a new script each time to attach to the folder. I can get Script Editor to write the script but it doesn’t seem to want to save it.

set FAPath to "Macintosh HD:Library:Scripts:Folder Actions Scripts:"

if not (exists FAPath) then
	display dialog "WTF"
else
	tell application "Script Editor"
		make new document
		tell front document
			set contents to "on adding folder items to this_folder after receiving these_items" & return & "display dialog \"Items added\"" & return & "end adding folder items to"
			compile
			try
				save as script in FAPath
			on error errMsg
				display dialog errMsg
			end try
		end tell
	end tell
end if

As a follow-up to this question - if this is feasible and I attach a script to mail folks when their products are available will that mail be generated from me or is there a way with AS to do this through the mail server. I don’t really care, I’ll just write the script to create the mail, send it and then delete the sent message so my mailbox doesn’t get clogged up but it’d be easier (and albeit cooler) if I could do this through the server or something.

hi,

recently i needed a similar script - I did it like so:

set scriptname to (do shell script "date \"+custom_script_%Y-%m-%d-%H-%M-%S.scpt\"")
set thePath to "Macintosh HD:Library:Scripts:Folder Actions Scripts:"
set FAPath to thePath & scriptname

if not (exists thePath) then
	display dialog "WTF"
else
	tell application "Script Editor"
		set thisDoc to (make new document)
		set text of thisDoc to "on adding folder items to this_folder after receiving these_items" & return & "display dialog \"Items added\"" & return & "end adding folder items to"
		compile -- not necesary in my opinion
		try
			close thisDoc saving in file FAPath
		on error errMsg
			display dialog errMsg
		end try
	end tell
end if

You can also use AppleScript/OS’s built-in capabilities. Ie:

set theScript to "on adding folder items to this_folder after receiving these_items" & ¬
	return & "display dialog \"Items added\"" & return & "end adding folder items to"

--> enclose inside script object
set theScript to run script "script o" & return & theScript & return & ¬
	"end script" & return & "return o"

set scriptLocation to (path to scripts folder as text) & "Folder Action Scripts:foo.scpt"
store script theScript in scriptLocation replacing yes

Or type “man osacompile” in a Terminal window.

Yeah, there are many ways a folder action can get info.

What info does the folder action need?

Edited: there must be a way to make the folder action modular.

gl,