run script on mount volume

hi
here I am again with some new question
I have a firewire drive named ‘mac’ and as soon as I connect it to my computer it shows the icon on the desktop.
so is that possible to run a script to open a file as soon as I connect firewire drive to my computer.
I am doing this because I don’t want the other user to look into my drive and search for the file he wants open. So I want to give him what he wants just by connecting the drive
and just for your information the some of the users have access to just simple finder so they are not going to see the drive icon.

I was going to suggest having an applet that runs continuously in the background, idling and checking for the drive every 5-10 seconds. However, I don’t know whether an AppleScript applet running for a user with Simple Finder options would b able to see the drive. Anyone know?

I don’t think there’s any AppleScript-native way to have something activate automatically when a volume is mounted - that would probably introduce a security risk.

Make a script that gets the list of drives, and run it as your user. If it works, then my idea will, too. Let me know if you need more info.

In OS 9, it’s possible to do this quite effectively with a script action attached to the open window of a folder on the drive being mounted. (The attached script is also kept on the drive.) If the window’s open when the drive’s unmounted, it’ll be opened automatically when the drive’s remounted and will thus trigger the folder action. If the window’s closed for any reason, another action in the same script reopens it without triggering the ‘opening folder’ action. The trigger folder could be called something like “Please keep this window open”.

The OS 9 script is given below to illustrate the principle. Unfortunately, I can’t write and test an X version as I can’t get folder actions to work at all on my X system - which is no inconvenience at all, so I’ve never bothered to try and fix it. Being the sole user of my machine, I’m also not able to investigate the ramifications for a multi-user environment.

Maybe someone else out there is in a position to do something with this?

on opening folder thisFolder
  -- Action to be taken when the drive's mounted
end opening folder

(* Action to be taken should the window become closed for any reason *)
on closing folder window for thisFolder
  -- Unattach all attached scripts so as not to trigger any folder actions when
  -- the folder's reopened
  tell application "Folder Actions"
    -- 'attached scripts' returns a list *containing* a list of aliases
    -- ie. {{alias, alias, ...}}. Get the inner list.
    set attachedScripts to item 1 of (attached scripts thisFolder)
    repeat (count attachedScripts) times
      remove action from thisFolder using action number 1
    end repeat
  end tell
  
  -- Reopen the folder window
  tell application "Finder" to open thisFolder
  
  -- Reattach the scripts
  tell application "Folder Actions"
    repeat with thisScript in attachedScripts
      attach action to thisFolder using thisScript
    end repeat
  end tell
end closing folder window for

I’ve now managed to get folder actions working in 10.2.6 - but only if the scripts are in a folder called “Folder Action Scripts” in the Scripts folder of my Library folder, which is obviously severely limiting. Furthermore, folder actions start off disabled at each log-in - which is obviously severely useless, especially since, as here, the folder concerned may open before a log-in script can reenable the actions. It also seems that all folders and scripts have to be present at the time the actions are enabled and that you can’t guarantee that the correct window will open anyway when you plug in the external drive. All in all, a complete waste of time. Apologies for the suggestion. :evil:

Hi Nigel,

On my machine (running 10.2.6), folder actions remain enabled unless I disable them. I don’t know if it matters but I have System Events set as a login item.

– Rob

Hi, Rob.

Thanks for that. I’ll go away and experiment. I don’t know if it’ll retrieve the current situation, but the spirit of exploration and all that… :wink:

Am I by any chance missing a system preference somewhere?

None that I know of, although it would be nice if Apple would offer something other than a script to enable folder actions. There are a couple of AppleScript Studio apps that put a face on the folder action management scripts and I don’t know why Apple doesn’t include something like this with the OS.

Folder Action Manager
Action Helper

– Rob

Hi, Rob.

And so it turns out to be on my own machine. The problem seems to have been the folder action script I was trying to test by logging out and loggin in again! Its action for reopening an accidentally closed window without triggering the folder action normally associated with the opening of that window was:

on closing folder window for thisFolder
  tell application "System Events" to set folder actions enabled to false
  tell application "Finder" to open thisFolder
  tell application "System Events" to set folder actions enabled to true
end closing folder window for

This works - often - if you don’t close the window again too soon afterwards (thereby retriggering the action before its finished). If the action’s attached when I log out, folder actions are disabled when I log in again. If the action’s not attached, folder action enablement (?) remains as it was. It seems that the action’s triggered when logging out and only gets as far as the first line, which disables the folder actions.

However, this has proved no help at all in getting an action to trigger when the window opens on log-in or on plugging in the drive. :frowning: In fact, if I start up the machine without the drive plugged in, another window opens instead!

Nigel, I wonder if it would be better to detach and then re-attach the folder action normally associated with the opening of that window instead of completely disabling folder actions.

– Rob (who hasn’t been awake long enough to think this through) :wink:

wow
I can never think of those things that you guys are doing.
I am a newbie and it hard for me to understand what you are doing.
I can manipulate and change the script according to my needs but I can’t write my own script. So do you have some suggestion for my issue.

Thanks
Guys

Hi, Rob.

… as in the original OS 9 version. Yes, I tried that too, but found that when the script’s reattached, it reacts to the fact that folder just opened anyway, so the action takes place that I was trying to prevent. BUT… I’ve just come up with a delightfully convoluted solution that seems to work very well on my X system. It involves two scripts: :slight_smile:

(Pseudo-code actually)

Script 1 ('on closing folder window for'):
  1) Detach the original 'on opening folder' script.
  2) Attach script 2 instead.
  3) Open the folder.

Script 2 ('on opening folder'):
  1) Detach this script.
  2) Reattach the original script.

So although I can’t stop the reaction to the opening of the folder, I can make that reaction the reattachment of the original script rather than the running of it. Assuming here that only one script is normally attached to the folder, the actual code for the two actions in the normal script would be:

-- What to do normally when the window first opens
on opening folder thisFolder
  beep 3 -- or something more interesting
end opening folder

-- How to reopen the window automatically without triggering
-- the action should it be closed accidentally
on closing folder window for thisFolder
  tell application "System Events"
    -- Detach this script
    remove action from thisFolder using action number 1
    -- Attach Script 2 instead
    attach action to thisFolder using ((path to scripts folder as string) & "Folder Action Scripts:Script 2") as alias
  end tell
  -- Reopen the window
  tell application "Finder" to open thisFolder
end closing folder window for

Then the other script would be:

on opening folder thisFolder
  tell application "System Events"
    -- Detach this script
    remove action from thisFolder using action number 1
    -- Reattach Script 1
    attach action to thisFolder using ((path to scripts folder as string) & "Folder Action Scripts:Script 1") as alias
  end tell
end opening folder

Still no good for alnoor’s purposes - nor perhaps for anyone else’s - but a nice trick to know. :wink:

Sorry, alnoor. When I barged into this thread a few messages back, I was hoping that a Folder Action method - invented by someone else when Folder Actions first appeared a few years ago - might be suitable for your purposes. Since then, Rob and I have been having a very interesting discussion about why it doesn’t work in OS X.

Krioni suggested a script that runs continuously and checks for the sudden appearance of a particular drive. This would be a “stay open” script using an ‘idle’ handler. ‘Idle’ handlers are run by the system itself at an interval that’s written into the script. This sort of thing:

 -- The name of a volume on the expected drive
property diskName : "LaCie 3"
-- A flag indicating whether the drive was present at the last check
property notFoundSoFar : true

-- When the script first runs, ensure that notFoundSoFar is true
set notFoundSoFar to true

on idle
  if notFoundSoFar then
    if (list disks) contains diskName then
      -- The volume has just been mounted. Reset the 'not found' flag.
      set notFoundSoFar to false
      -- Whatever you want to happen when the drive's plugged in should go here.
      beep 3
      --------------
    end if
  else if (list disks) does not contain diskName then
    -- The volume has just been removed. Reset to await its reappearance.
    set notFoundSoFar to true
  end if
  -- Run this idle handler again in 15 seconds' time
  return 15
end idle

Save this as an application, checking the ‘Stay Open’ box in the Save dialog. If you make it a log-in item in the System Preferences, it’ll run when you log in and keep running in the background until you quit it or log out. The action that you write into it will be carried out once only after the drive is found to be mounted. When the drive’s unmounted, the script will reset itself to await the next remounting.

Hope this gets you started. :slight_smile: