eject volume and log off

I am new to Applescript and the learning curve was shortened to 1 month by administration. I have written a script to start an app and mount a smb volume that is successful, the issue now is to unmount the volume and log off when the app closes. I thank anyone who has this script and will share or can lead me in the right direction for I have about pulled all my hair out!

Welcome!


tell application "Finder"
	eject disk "Terror Twilight"
end tell


tell application "System Events"
	shut down
end tell

You can find system events.app and look at it’s dictionary in System:Library:CoreServices:System Events.app

Oh! And RTFM:
https://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/introduction/ASLR_intro.html

Hi, MtnMan60. Welcome to MacScripter.

You could use a stay-open script applet containing code like that below. A stay-open applet stays open until explicitly quit and is periodically sent ‘idle’ commands by the operating system. The script’s ‘idle’ handler responds to these commands and carries out whatever periodical task it’s supposed to do. Each time it finishes, it should return a number telling the system how many seconds to wait before sending it the next ‘idle’ command.

The first time the ‘idle’ handler executes is immediately after the initial ‘run’ handler. In the script below, the ‘idle’ handler does nothing the first time round but asks to be called again after an initial waiting period (which you can set in a property at the top of the script.) On subsequent calls, it checks for the existence of the specified running application and does nothing as long as that application is open. If the application’s not open, the script ejects the specified disk and starts the log-out process.

Set your own values for the properties and save the script as an application, checking the “Stay Open” option in the “Save.” dialog.

-- Adjust these values to your own requirements:
property initialInterval : 5 * minutes -- The initial time to wait before starting the checks to see if the app has quit.
property checkInterval : 10 -- The time (in seconds) between checks after that.

property appName : "My App" -- The name of the app to watch.
property diskName : "My Disk" -- The name of the disk to eject.

global justOpened

on run
	set justOpened to true -- Tells the 'idle' handler that this script applet's just started running.
end run

on idle
	if (justOpened) then
		-- Reverse the flag for next time and ask for the next 'idle' to come after the intial wait period.
		set justOpened to false
		return initialInterval
	else
		-- Otherwise check if the application is (still) running.
		tell application "System Events" to set appRunning to (application process appName exists)
		if (appRunning) then
			-- If it is, request the next 'idle' to come after the specified check interval.
			return checkInterval
		else
			-- Otherwise eject the disk (if it's mounted).
			tell application "Finder"
				if (disk diskName exists) then eject disk diskName
			end tell
			-- Issue the log out command. (There'll be a 1-minute count-down dialog).
			tell application "System Events" to log out
			-- Tell this stay-open script application to quit. (It will only do so on the next 'idle'.)
			quit
			-- Request the next 'idle' to come after the shortest possible time.
			return 1
		end if
	end if
end idle

Hello Nigel, thanks for the response! I added my start up script to the beginning of your script, what happens now is it starts the app, mounts the volume but does not eject and log off when the app ends. I feel I have not grasp the concept just yet. The script follows:

tell application "Finder"
	mount volume "smb://nwea:Testing2011@pccs-sec-app/Network Test Environment"
end tell 
tell application "TestTaker"
	activate
end tell 

-- Adjust these values to your own requirements:
property initialInterval : 5 * minutes -- The initial time to wait before starting the checks to see if the app has quit.
property checkInterval : 10 -- The time (in seconds) between checks after that.

property appName : "TestTaker" -- The name of the app to watch.
property diskName : ""smb://nwea:Testing2011@pccs-sec-app/Network Test Environment" -- The name of the disk to eject.

global justOpened

on run
   set justOpened to true -- Tells the 'idle' handler that this script applet's just started running.
end run

on idle
   if (justOpened) then
       -- Reverse the flag for next time and ask for the next 'idle' to come after the intial wait period.
       set justOpened to false
       return initialInterval
   else
       -- Otherwise check if the application is (still) running.
       tell application "System Events" to set appRunning to (application process appName exists)
       if (appRunning) then
           -- If it is, request the next 'idle' to come after the specified check interval.
           return checkInterval
       else
           -- Otherwise eject the disk (if it's mounted).
           tell application "Finder"
               if (disk diskName exists) then eject disk diskName
           end tell
           -- Issue the log out command. (There'll be a 1-minute count-down dialog).
           tell application "System Events" to log out
           -- Tell this stay-open script application to quit. (It will only do so on the next 'idle'.)
           quit
           -- Request the next 'idle' to come after the shortest possible time.
           return 1
       end if
   end if
end idle

[coloration wrong because I didn’t compile it – I just enclosed it in tags. ACB]

Hi.

Your code needs to go inside my ‘run’ handler. Also, ‘mount volume’ isn’t a Finder command and the Finder only needs the disk name (more properly here the name of the mount point) to go with its ‘disk’ specifiers for the ‘exists’ and ‘eject’ commands.

-- Adjust these values to your own requirements:
property initialInterval : 5 * minutes -- The initial time to wait before starting the checks to see if the app has quit.
property checkInterval : 10 -- The time (in seconds) between checks after that.

property appName : "TestTaker" -- The name of the app to watch.
property diskName : "Network Test Environment" -- The name of the disk to eject.

global justOpened

on run
	mount volume ("smb://nwea:Testing2011@pccs-sec-app/" & diskName)
	tell application appName
		activate
	end tell
	set justOpened to true -- Tells the 'idle' handler that this script applet's just started running.
end run

on idle
	if (justOpened) then
		-- Reverse the flag for next time and ask for the next 'idle' to come after the intial wait period.
		set justOpened to false
		return initialInterval
	else
		-- Otherwise check if the application is (still) running.
		tell application "System Events" to set appRunning to (application process appName exists)
		if (appRunning) then
			-- If it is, request the next 'idle' to come after the specified check interval.
			return checkInterval
		else
			-- Otherwise eject the disk (if it's mounted).
			tell application "Finder"
				if (disk diskName exists) then eject disk diskName
			end tell
			-- Issue the log out command. (There'll be a 1-minute count-down dialog).
			tell application "System Events" to log out
			-- Tell this stay-open script application to quit. (It will only do so on the next 'idle'.)
			quit
			-- Request the next 'idle' to come after the shortest possible time.
			return 1
		end if
	end if
end idle

The property values could actually be written directly into the script where they’re used. They’re just assigned to properties at the top to make it easier to insert your own values.

By the way, this BBS has [applescript] and [/applescript] tags, which allow scripts to be shown in clickable form as above. You can write them around the script code in your posts or use the “Applescript” button just above the posting window to enter them for you. (Click the button and paste the script between the tags it enters, or select the already pasted script code and then click the button.)

Nigel, this works great!!! I would like to know if this could be adapted to not give the user the ability to cancel the log off? Can the confirmation be bypassed and go straight to log off?

                                 Thanks.

Well, this is unofficial, but it came from AppleScript’s Chris Nebel on the AppleScript-Users list a few years ago. Change this .

 -- Issue the log out command. (There'll be a 1-minute count-down dialog).
tell application "System Events" to log out

. to this:

-- Issue loginwindow's "real logout" event for an immediate log-out.
ignoring application responses
	tell application "loginwindow" to «event aevtrlgo»
end ignoring

The ‘quit’ and "return 1’ lines after it may turn out to be superfluous, but I haven’t tried the loginwindow line in this context.