Folder Action Problem

Regarding Folder Actions in OS 9,

I am having a problem for which I am guessing there is a workaround or fix, but cannot come up with one on my own.

The problem is this: I have written a folder action script and attached it to a folder. It works fine in that when I add a file to the folder, the script executes. (Some copying commands take place.) However, when my script is done executing, the Finder is active instead of the application I was working in. For example, if I am in BBEdit, and use that app to save a text file into the folder which has the folder action attached, the folder action script will execute, but when it’s done, the Finder will be active instead of BBEdit. My script contains no “activate” commands and no user interaction. Is there any way to prevent the Finder from becoming active, or to return to the original app by dynamically capturing the name of the app that was active before the script was triggered, or something similar?

Help would be greatly appreciated. This might be a deal-breaker for a project I am working on for someone.

Thanks.

Hi,

If you are using a folder action, which requires the folder to be open, and if you are adding a file then the Finder is the last application active prior to adding the file. Unless you are using some special software that lets you bypass that requirement.

I’m assuming, (and correct me if I’m wrong), that you are using BBEdit to write to a log file. You have two options I guess.

  1. use the system to write to the text file instead of BBEdit. See below for how to have the system write to a file.
set pathToFile to "Macintosh HD:Desktop Folder:Some Folder:log.txt" --this is the path to the text file, it does not have to exist
set logFile to open for access file pathToFile with write permission --this command creates the file if it isn't there, opens it if it is (with permission to write to it)
set fileEOF to get eof logFile --get the end of the file so we don't overwrite anything

--once you have some text to write to it do this
set currDate to the current date --I am putting this in just as an example of something to log
set currDate to currDate as text
set logText to "------------------------------" & return & currDate & return & "Here is some text to log" & return & "------------------------------" as string --this is just example log text
write return & logText to logFile starting at fileEOF --write a carraige return & your data to the file starting at the end of the file
close access logFile --close access to the file so it isn't tied up

or, if I am off base and you really need to use BBEdit…

  1. activate BBEdit before doing anything with it.

Tell application "BBEdit"
activate
--do your stuff here
end tell

If you don’t do anything else after bossing around BBEdit, it should be the active app when your script finishes.

Hope I was able to help.

Thanks for your reply. While it does not address my particular issue, your code is interesting and could be useful to me in the future.

Not using special software. Simply “Saving” files into actionable folder from applications such as BBEdit. Thus the Finder is NOT the last application active prior to adding the file. The Saving application is. By the way, I understand what you are saying about the requirement that the Finder window of the actionable folder be open, and let me tell you that that I have tested it both ways: (1) with the Finder window open, (2) with the Finder window closed and the Folder Actions Plus Extension installed. And my experience is the same in both cases. The action script IS triggered when it should be. But the Finder pops to the front (activates) when the action script completes.

You were indeed assuming wrong, I used BBEdit as an example. My question was more general in that I expect multuple applications to be saving to the actionable folder at various times. If it was always going to be BBEdit, I could have added the following code to the end of my script and been done with it:

tell application "BBEdit" to activate

All that said, I did find a solution. To my script, I added the following code:

property frontmost_application : ""

on PreserveFrontmost()
	set frontmost_application to path to frontmost application as text
end PreserveFrontmost

on RestoreFrontmost()
	tell application frontmost_application to activate
end RestoreFrontmost

And then by calling PreserveFrontmost at the top of the run of the script and RestoreFrontmost at the bottom, I was able to totally solve my issue.

Again, though, thanks for your response. I learned from your code.