PDA

View Full Version : AppleScript: checking for mounted volume




yellow
Apr 12, 2005, 04:36 PM
Please help an AppleScript retard! I know next to nothing about AppleScript, and unfortunately, I'm not able to adequately take the time to learn it to solve the problem myself, and goggling isn't being much help:

What I wanted to accomplish:

We've moved to Active Directory, with it comes networked home folders and networked shared folders. Let's say that most of my users are.. less then adept at doing simple tasks, so rather then repeatedly teaching them to mount the SMB shares by hand (which WILL be forgotten), or trying to use a double-clickable alias of the mount (which WILL break), I'm trying to use AppleScript and make an 1d10t-proof .app that can exist as a Dock icon. User-A clicks on the Dock icon, and they are prompted for their password to mount the SMB share, and their home folder is opened automatically.

What I want to accomplish now:

That part was easy enough and works fine. But if User-A closes/looses their home folder window, and clicks on the Dock icon again, even though the SMB share might be mounted, it will run and ask them for their crudentials. What I want to do is have the script check, if the share is not mounted, go through the steps to mount it, and then open the user's home folder, otherwise just open the user's home folder. Or something to that effect. Here's what I have so far:

tell application "Finder"
activate
try
mount volume "smb://domain;username@foo.org/group_home/"
end try
repeat until (list disks) contains "GROUP_HOME"
end repeat
make new Finder window to folder "username" of disk "GROUP_HOME"
end tell


Hopefully this is clear enough to explain what I want to do and where I want to go from the AppleScript above. How would you solve this, you AppleScripting Guru, you?

Thanks!!!!



yellow
Apr 12, 2005, 04:44 PM
Well.. I just answered my own question, by trial and error. The end solution (that works, so far..)

tell application "Finder"
activate
if exists disk "GROUP_HOME" then
make new Finder window to folder "username" of disk "GROUP_HOME"
else
try
mount volume "smb://domain;username@foo.org/group_home/"
end try
repeat until (list disks) contains "GROUP_HOME"
end repeat
make new Finder window to folder "username" of disk "GROUP_HOME"
end if
end tell

Sweet!

Now, can anyone tell me how to put some error checking in this? I noticed if I try and click "Cancel" on the SMB share authentication Window, the AppleScript keeps bringing it up. I need to make sure that clicking "Cancel" is aknowledged and respected by the AppleScript.

robbieduncan
Apr 12, 2005, 05:06 PM
Does mount return anything? i.e. can you say something like

set returnValue to mount ....


And see what the returnValue is when it works and you cancel? Otherwise I'm not sure

robbieduncan
Apr 12, 2005, 05:18 PM
Better answer (i.e. this one works)


tell application "Finder"
try
mount volume "smb://192.168.0.11/"
on error
display dialog "Error"
end try
end tell


Error will be displayed if the users cancels (at either dialog here as I am not supplying a username and password). If the share mounts Error is not displayed.

yellow
Apr 12, 2005, 05:39 PM
Thanks! Sweet! That does work, except..

I can't put the "end tell" in right after the "end try", I get errors in the script, as it's trying to quit early.

If I click "OK" in the called dialog display, the script never quits, I have to actually stop it in the Script Editor. However, it does quit properly if I click "Cancel". I suppose I could tell it to click cancel automatically, but is there a way I can remove the "OK" button from the dialog box?

Sorry for all the inane questions!

Here's how I implmented your suggestion:

tell application "Finder"
activate
if exists disk "GROUP_HOME" then
make new Finder window to folder "username" of disk "GROUP_HOME"
else
try
mount volume "smb://domain;username@foo.org/GROUP_HOME/"
on error
display dialog "Enterprise authentication failed, exiting."
end try
repeat until (list disks) contains "GROUP_HOME"
end repeat
make new Finder window to folder "username" of disk "GROUP_HOME"
set current view of Finder window 1 to list view
end if
end tell

robbieduncan
Apr 12, 2005, 05:44 PM
display dialog "My Text" buttons "Cancel"


I'm not sure AppleScript is sensible enough to use this as a cancel button as opposed to using it as an OK button, but displaying it as Cancel. Wow that doesn't make a lot of sense! What I mean is that behind the scenes the button is returning something. What the text is on the button does not necessarily relate to what gets returned!

Anyway, give it a quick try...

Edit to add: p.s. you've misspelled Enterprise in your error message :p

yellow
Apr 12, 2005, 05:50 PM
You ****** RULE!!!!!!

That worked! It canceled/quit the script gracefully! Thanks so much for your help!!

Next step, working out the Finder does it's Window position and boundry geometry!

Thanks again!!

EDIT: LOL, thanks, fixed typo in script.

robbieduncan
Apr 12, 2005, 05:55 PM
You ****** RULE!!!!!!

That worked! It canceled/quit the script gracefully! Thanks so much for your help!!

Next step, working out the Finder does it's Window position and boundry geometry!

Thanks again!!

EDIT: LOL, thanks, fixed typo in script.

No problem mate :) Always glad to help. I'm not sure you can set the geometry in "normal" AppleScript (although I am far from expert so wait to be proved wrong). If you use the GUI scripting stuff (it's an optional install iirc on Panther, I think it's part of the default Tiger install) you can probably do this by pretending to drag the mouse.

OK I might be wrong. Just had a quick look. This is ASS documentation (haha AppleScript Studio...) but might well work if you can get a window reference ASS Window Docs (http://developer.apple.com/documentation/AppleScript/Reference/StudioReference/sr3_app_suite/chapter_3_section_18.html)

robbieduncan
Apr 12, 2005, 06:05 PM
The ASS docs are just that, ass. They only work in ASS as you are connecting to Cocoa over AppleScript.

This works though :)


tell application "Finder"
make new Finder window
set position of front window to {16, 150}
set bounds of front window to {16, 150, 400, 400}
-- You can set whether the window is minimized via this:
set collapsed of front window to true
end tell

yellow
Apr 12, 2005, 06:06 PM
I know I can "set bounds of Finder window 1 to {4, 82, 564, 438}" and "set position of Finder window 1 to {412, 192}" to set the size of the window and the position on the screen (respectively), I just have to figure out what the "arbitrary" numbering it's using is. ("Arbitrary" because I'm ignorant! :)) Pixels, based on resolution? Who knows.

I need to ensure that whatever I set it to works on little screens as well as large screens.

But I will tackle that tomorrow. Thanks again!

EDIT: LOL, you beat me to it! :)

robbieduncan
Apr 12, 2005, 06:11 PM
Seems to set the content area of the window in screen pixels. I tried creating a window at 0,0 the size of my screen and it fitted perfectly with a little of the window border outside the screen on all sides. I think that this is because my default finder window are in metal mode. In Aqua mode you don't have the same borders on the windows so the size would be perfect.

Note that a window at 0,0 is really there and is under the menu bar!

Further experimentation proves this. This code creates a finder window as big as the screen on my PowerBook (OK it's too big as it's behind the menu bar)


tell application "Finder"
make new Finder window
-- I like column view
set current view of window 1 to column view
-- Switch to aqua mode
set toolbar visible of window 1 to false
set bounds of window 1 to {0, 0, 1280, 854}
end tell

yellow
Apr 12, 2005, 06:43 PM
OK, then.. for bounds it's x/y of the upper left hand corner and x/y of the lower right hand corner. Cool. I can experiment to make the position.

Now if I could figure out how to use XCode to build an cocoa applescript app that allows the user to choose (and have it remember) what type of "views", etc they want.

robbieduncan
Apr 13, 2005, 03:59 AM
OK, then.. for bounds it's x/y of the upper left hand corner and x/y of the lower right hand corner. Cool. I can experiment to make the position.

Now if I could figure out how to use XCode to build an cocoa applescript app that allows the user to choose (and have it remember) what type of "views", etc they want.

This is actually pretty easy to do. Start XCode, choose new project then AppleScript Application. Design the UI in Interface Builder (remember to set AppleScript properties) and fill in the skeleton code that IB will put into XCode. I migh post an examlpe when I get home...

robbieduncan
Apr 13, 2005, 01:28 PM
I've placed an example XCode project here (http://www.robbieduncan.net/examples/SMB%20Opener.zip). It's not complete but shows how to load/save user defaults, respond to button clicks and so on.

yellow
Apr 14, 2005, 11:38 AM
Thanks RD. I took a look at it and my head is spinning.. I actually broke down and bought a book on AppleScript and am making headway on it. Just reading the first 3.5 chapters helped me clean up the scripts. I'm waiting on Chapter 14 before I jump into AppleScript Studio. There are definitely a lot of concepts in it that are completely over my head at the moment! :) But I will DEFINITELY be creating an ASS app from this.

Here's my current script, "version 1.2":

-- Version 1.2
-- By: yellow
-- 4/13/05

tell application "Finder"
activate
if exists disk "GROUP_HOME" then
try
make new Finder window to folder "_username_" of disk "GROUP_HOME"
on error
beep
display dialog
"User folder cannot be found, please contact the Help Desk 555-1212" buttons "Cancel" default button 1 with icon stop
end try
return
end if
try
mount volume "smb://domain;_username_@foo.org/GROUP_HOME/"
on error
beep
display dialog
"DHE authentication failed." buttons "Cancel" default button 1 with icon caution
end try
repeat until (list disks) contains "GROUP_HOME"
end repeat
try
make new Finder window to folder "_username_" of disk "GROUP_HOME"
set current view of Finder window 1 to list view
on error
beep
display dialog
"User folder cannot be found, please contact the Help Desk 555-1212" buttons "Cancel" default button 1 with icon stop
end try
end tell

mistere2
Sep 30, 2006, 03:24 PM
Thanks for the great tips in this thread. I thought I would share how I made use of them to write a very simple script that will launch iTunes only if the external hard drive that contains my iTunes music library is mounted. This way the iTunes library location is not reset when I accidentally trigger iTunes with Quicksilver without the drive mounted.

tell application "Finder"
activate
if exists disk "Music" then
tell application "iTunes"
activate
end tell
end if
end tell