Thor Correia
Thor Correia

Reputation: 1588

How to run script upon opening folder?

I want to run an applescript when a specific folder of mine is opened. Then, depending on the input, close the folder, or leave it open. All that without opening the folder. So, basically:

1) Try to open folder

2) Folder doesn't open, but window pops up

3-A) If user clicks ok folder opens

3-B) If user clicks cancel, it just exits out of script, leaving the folder unopened.

How can I do this? Remember: The folder CAN'T open in the background, it can ONLY open if the user presses OK. Help?

Upvotes: 1

Views: 3874

Answers (2)

fireshadow52
fireshadow52

Reputation: 6516

For this task, you'll want to use Folder Actions, which trigger script files. Below are the events involving a folder that could trigger a script, and the special corresponding handlers.

  • A folder is opened — on opening folder window for this_folder
  • A folder is closed — on closing folder window for this_folder
  • One or more items are added to a folder on adding folder items to this_folder after receiving these_items
  • One or more items are removed from a folder — on removing folder items from this_folder after losing these_itmes
  • A folder window is moved — on moving folder window for this_folder

The first bullet is what you'll be focusing on in this case. As the bullet implies, the script is triggered only when the folder actually opens. However, you can hack your way around this issue simply by adding this line at the very beginning of your script:

tell application "Finder" to close first window whose target is this_folder

And here is the full script:

on opening folder window for this_folder
    tell application "Finder"
        close first window whose target is this_folder
        -- the window may appear briefly, but at least you've accomplished your goal

        display dialog "Open folder " quoted form of the name of this_folder & "?" buttons{"Yes,"No"} default button 1 cancel button 2
        -- pressing the "No" button is exactly the same as pressing the "Cancel" button on a regular dialog, and the script terminates

        make new Finder window with properties {target:this_folder}
    end tell
end opening folder window

SAVE YOUR SCRIPT FILE IN THE FOLDER ACTIONS FOLDER OF YOUR LOCAL SCRIPTS FOLDER. Create the folder yourself if it doesn't already exist. Doing so will allow your script to even run. Now, for this to function properly, you will need to attach the saved script file—not an application/application bundle—to your desired folder. To do this...

  1. Perform a right-click on your desired folder
  2. Click the menu item "Folder Actions Setup" at the bottom
  3. Locate your saved script in the dialog that appears
  4. Click "Attach"
  5. Close the "Folder Actions Setup" window

Once you've done this, your script is ready to go.

Addendum: If your folder contains confidential information, beware that, as stated by user57368, "it is probably not possible to make the system even remotely secure with AppleScript."

Upvotes: 2

user57368
user57368

Reputation: 5765

Instead of an ordinary folder, you want an application bundle containing a folder or disk image. If you use an encrypted disk image, it will be more difficult for the user to access the contents without following your preferred procedure, but beware that it is probably not possible to make the system even remotely secure when you're using AppleScript.

If you just want to present a license agreement or other static notice before allowing the user to read the data, just read the hdiutil man page for how to create a disk image that presents a license agreement upon mounting.

Upvotes: 0

Related Questions