Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 12, 2005, 04:36 PM   #1
yellow
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
AppleScript: checking for mounted volume

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!!!!
yellow is offline   0 Reply With Quote
Old Apr 12, 2005, 04:44 PM   #2
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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.

Last edited by yellow; Apr 12, 2005 at 05:10 PM.
yellow is offline   0 Reply With Quote
Old Apr 12, 2005, 05:06 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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
robbieduncan is offline   0 Reply With Quote
Old Apr 12, 2005, 05:18 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   1 Reply With Quote
Old Apr 12, 2005, 05:39 PM   #5
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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

Last edited by yellow; Apr 12, 2005 at 05:51 PM.
yellow is offline   0 Reply With Quote
Old Apr 12, 2005, 05:44 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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

Last edited by robbieduncan; Apr 12, 2005 at 05:46 PM.
robbieduncan is offline   0 Reply With Quote
Old Apr 12, 2005, 05:50 PM   #7
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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.
yellow is offline   0 Reply With Quote
Old Apr 12, 2005, 05:55 PM   #8
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by yellow
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
robbieduncan is offline   0 Reply With Quote
Old Apr 12, 2005, 06:05 PM   #9
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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
robbieduncan is offline   0 Reply With Quote
Old Apr 12, 2005, 06:06 PM   #10
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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!
yellow is offline   0 Reply With Quote
Old Apr 12, 2005, 06:11 PM   #11
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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

Last edited by robbieduncan; Apr 12, 2005 at 06:16 PM.
robbieduncan is offline   0 Reply With Quote
Old Apr 12, 2005, 06:43 PM   #12
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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.
yellow is offline   0 Reply With Quote
Old Apr 13, 2005, 03:59 AM   #13
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by yellow
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 is offline   0 Reply With Quote
Old Apr 13, 2005, 01:28 PM   #14
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Apr 14, 2005, 11:38 AM   #15
yellow
Thread Starter
Moderator
 
yellow's Avatar
 
Join Date: Oct 2003
Location: Portland, OR
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
yellow is offline   0 Reply With Quote
Old Sep 30, 2006, 03:24 PM   #16
mistere2
macrumors newbie
 
Join Date: Sep 2006
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
mistere2 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
NTFS-3G volume not cleanly mounted lpearce1 Windows, Linux & Others on the Mac 0 Jan 29, 2014 08:51 AM
Incremental volume adjustments with AppleScript? wrignj08 Mac Programming 5 Jul 28, 2013 05:41 AM
Issues Lowering Volume of Spotify with AppleScript macman7002 Mac Programming 0 Mar 18, 2013 08:07 PM
Mount ssh volume with Applescript MacPC Windows, Linux & Others on the Mac 2 Feb 18, 2013 05:36 AM
AppleScript to zip sub folders on a volume kevinless Mac Basics and Help 2 Nov 11, 2012 05:37 PM

Forum Jump

All times are GMT -5. The time now is 03:14 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC