AppleScript: checking for mounted volume

Discussion in 'Mac Programming' started by yellow, Apr 12, 2005.

  1. Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #1
    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:

    Code:
    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!!!!
     
  2. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #2
    Well.. I just answered my own question, by trial and error. The end solution (that works, so far..)

    Code:
    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.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Does mount return anything? i.e. can you say something like
    Code:
    set returnValue to mount ....
    
    And see what the returnValue is when it works and you cancel? Otherwise I'm not sure
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Better answer (i.e. this one works)

    Code:
    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.
     
  5. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #5
    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:

    Code:
    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
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Code:
    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
     
  7. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #7
    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.
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    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
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    The ASS docs are just that, ass. They only work in ASS as you are connecting to Cocoa over AppleScript.

    This works though :)

    Code:
    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
    
     
  10. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #10
    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! :)
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    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)

    Code:
    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
    
     
  12. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #12
    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.
     
  13. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    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...
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    I've placed an example XCode project here. It's not complete but shows how to load/save user defaults, respond to button clicks and so on.
     
  15. thread starter Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #15
    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":

    Code:
    -- 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
     
  16. macrumors newbie

    Joined:
    Sep 30, 2006
    #16
    Open iTunes only if an external hard drive containing your music library is mounted.

    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.

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

Share This Page