I have a system whereby folders are created on my Appleshare Server (OS 10.4) from within FileMaker Pro, via Applescript.
I have problems with users manually renaming folders, creating a situation whereby the folder is called something different to what my field in FileMaker Pro says it should be called.
Is there any way of attaching an Applescript to a folder (through a folder action perhaps?) which would prevent that folder from being renamed?
Two things: firstly, I can’t achieve this with access privileges as I need people to have read/write access, and secondly, I would only want the action to apply to folder created from within FileMaker Pro - if someone then creates a folder inside that folder, I want them to be able to rename that folder to their heart’s content.
Can anyone help me with a piece of code to do this?
Here’s a trick with alias references. First make a folder named “TestFolder” at the root level of your startup disk. If you run the app it displays the AppleScript path to the script.
set f to alias “Macintosh HD:TestFolder:”
display dialog (f as string)
Now if you rename it or move the folder, its alias reference miraculously changes. You can use this to rename your folder to its original name by storing the original name and comparing with its current name at the time the script checks it. Right off the bat, I would say you can monitor the folder name with an idle handler. That’s all I can think of right now.
But if you have a folder on your desktop say, named “Test”, and you compile and save this little script as an application somewhere, then as long as you don’t recompile the script applet, it will open the folder whenever it runs, and as Kel points out, that is true even if someone changes the name of the folder after the applet is compiled.
property f : (path to desktop as string) & "Test:" as alias
tell application "Finder" to open f
I don’t think that will work, because the alias reference is created at run time.
I wasn’t sure if it would work with a folder action, but it does on my machine running Jaguar. Here’s the FA Script:
property folder_ref : alias “Macintosh HD:Users:kel:Desktop:Main:TestFolder:”
property original_name : “TestFolder”
on adding folder items to this_folder after receiving these_items
set n to name of (info for folder_ref)
if n is not original_name then
tell application “Finder”
set name of folder_ref to original_name
end tell
end if
end adding folder items to
I attached this folder action to a folder named “Main” on my desktop. If thecontained folder “TestFolder” is renamed, then the folder action fires and renames it back to the orginal name. Note that the script fires again when its name is changed back to the original.
Edited: if you don’t like the flashing then there are workarounds I think.
It actually did work when I tested it in Tiger, Kel. I saved the script as an applet, quit it, changed the name of my test folder, double-clicked my applet, et voila - the correct folder opened. Are you saying that if I shut down the machine or logged out and then came back it wouldn’t work? I didn’t try that.
Ahh, the trick is nesting the folder to be controlled in another one - excellent! I couldn’t get it to work on the folder itself.
I got this to work, but the problem is that the folder which is being “watched” is named in the script. My situation is that I am running a server on which dozens of folders per day are being created (one every time a new job is raised on my system in FileMaker Pro). I assume therefore that this would mean that the FA script would have to change each time a new folder is created?
What I really need is a way of attaching a script to every new folder which is created (presumably I can find a way of attaching a folder action at the time of folder creation) which prevents it from being renamed. Any further thoughts, guys?
There’s no folder action that would run when the folder it is attached to is renamed. You might eventually need to resort to an idle handler to monitor the folder, because weird things can happen when fodlers are added while a folder action is running. Here’s another try at folder action.
You need to store the list of folders and their original names seperately. One way is to query a script server. I named the following stay open script application “ScriptServer1”:
on run
set fa_folder to (choose folder with prompt “Choose the folder to monitor:”)
tell application “Finder”
try
set folder_list to (every folder of fa_folder as alias list)
on error
set folder_list to (first folder of fa_folder) as alias as list
end try
set original_names to (name of every folder of fa_folder)
end tell
end run
on reopen
run
end reopen
When it is run, it saves a list of the sub folders of the folder (you choose) as the one that has the folder action attached to it. The folder action script gets its info from this stay open script app. The following folder action seems to work, but might need testing and error checking:
on adding folder items to this_folder after receiving these_items
tell application “ScriptServer1”
set folder_list to folder_list of it
set folder_names to original_names of it
end tell
repeat with this_item in these_items
if (contents of this_item) is in folder_list then
– it was renamed
– get original name
set item_count to (count folder_list)
repeat with i from 1 to item_count
set list_item to (item i of folder_list)
if list_item is (contents of this_item) then exit repeat
end repeat
set original_name to (item i of folder_names)
– rename it
tell application “Finder” to set name of this_item to original_name
else – new folder
– update script server lists
tell application “ScriptServer1”
set (folder_list of it) to (folder_list of it) & {contents of this_item}
set (original_names of it) to (original_names of it) & {(name of (info for this_item))}
end tell
end if
end repeat
end adding folder items to
Hope it works for you or we might have to use an idle handler.