The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   AppleScript: Need help modifying script.. (http://hintsforums.macworld.com/showthread.php?t=37778)

yellow 04-12-2005 01:42 PM

AppleScript: Need help modifying script..
 
Greetings again true believers.. once again, I need to try and rely on AppleScript to get something accomplished. And once again, I must state that I know nothing about AppleScript and unfortunately, I'm under a huge time constraint. That means I won't have time to RTFM and learn it to get when I need done. That being said, I hope I can rely on someone else who has already forgotten more about it then I will be able to learn.

What am I doing?

Using the "Go To Server" facility to mount a set of SMB shares can be a bit cumbersome, and also I have some users who are.. shall we say.. not terribly computer literate? I want to create a double-clickable AppleScript.app that will mount the remote share and then open the requisite user folder.

So far I've got this part working:

Code:

tell application "Finder"
        try
                mount volume "smb://domain;username@server.foo.org/group_home/"
        end try
end tell

tell application "Finder"
        activate
        make new Finder window to disk "GROUP_HOME"
        make new Finder window to folder "username" of disk "GROUP_HOME"
end tell

What I want to know is.. is there a better way to do this? Cleaner "code"?

Thanks for all your help!

yellow 04-12-2005 01:56 PM

One thing I discovered while palying with it that I can strip out the line "make new Finder window to disk "GROUP_HOME"", since I don't need that open, just the user's home folder.

Code:

tell application "Finder"
        try
                mount volume "smb://domain;username@server.foo.org/group_home/"
        end try
end tell

tell application "Finder"
        activate
        make new Finder window to folder "username" of disk "GROUP_HOME"
end tell

How would I add a delay in between the first "end tell" and the next "tell application"? Is a delay even needed, or does AppleScript run like a shell script and wait on the previous thing to finish before it moved onto the next part?

yellow 04-12-2005 02:00 PM

Quote:

Originally Posted by yellow
How would I add a delay in between the first "end tell" and the next "tell application"?

Answered my own question..

Code:

tell application "Finder"
        try
                mount volume "smb://domain;username@server.foo.org/group_home/"
        end try
end tell

delay 5

tell application "Finder"
        activate
        make new Finder window to folder "username" of disk "GROUP_HOME"
end tell


AHunter3 04-12-2005 02:38 PM

This is all well fine and good AS practice, but why not just make an alias to the volume once mounted and then put it where they can double-click it?

guardian34 04-12-2005 02:42 PM

FWIW, you could shorten it:
Code:

tell application "Finder"
    activate
    try
        mount volume "smb://domain;username@server.foo.org/group_home/"
    end try
    delay 5
    make new Finder window to folder "username" of disk "GROUP_HOME"
end tell


NovaScotian 04-12-2005 02:43 PM

Quote:

Originally Posted by yellow
Answered my own question..

Code:

tell application "Finder"
        try
                mount volume "smb://domain;username@server.foo.org/group_home/"
        end try
end tell

delay 5

tell application "Finder"
        activate
        make new Finder window to folder "username" of disk "GROUP_HOME"
end tell


Instead of a simple delay (whose timing will inevitably screw up) you could do this:

Code:

repeat until (list disks) contains "GROUP_HOME"
end repeat

If there's a possibility that GROUP_HOME will never appear, a timeout escape and a dialog to say G_H isn't on line would bomb-proof it.

yellow 04-12-2005 03:09 PM

Quote:

Originally Posted by AHunter3
This is all well fine and good AS practice, but why not just make an alias to the volume once mounted and then put it where they can double-click it?

Because 50% of the time, the aliases "break" and the network volume cannot be found. Sometimes, depending on the Mac, it breaks 100% of the time. I cannot rely on this facility, remember, some of these users are "computer challenged". No matter how much documentation I provide, the users will call me and ask me to fix it, they would still rather just call me and have me deal with it.

I want to make an "idiot-proof" way of doing it. Hence, the need for an AppleScript.

yellow 04-12-2005 03:10 PM

OK, thanks guardian34 and NovaScotian, I will shorten it and bomb-proof it and see how that fares!

yellow 04-12-2005 03:16 PM

Quote:

Originally Posted by NovaScotian
Code:

repeat until (list disks) contains "GROUP_HOME"
end repeat


Just to make sure, is "list disks" an actual command, or is it a nudge to me to place something in place of "list disks"?

EDIT: it seems to work out of the box. thanks for the suggestion! :)

NovaScotian 04-12-2005 03:31 PM

Quote:

Originally Posted by yellow
OK, thanks guardian34 and NovaScotian, I will shorten it and bomb-proof it and see how that fares!

Post it again in final form, yellow, just for the sake of completeness. I agree with your "alias" remarks by the way - I haven't found internet aliases to non-Macs to be very reliable either.

yellow 04-12-2005 03:39 PM

Keywords: applescript mount smb
 
OK, thanks again! Naturally the following is 'anonymoized' somewhat.

1) This one is for the user's networked home directory..

Code:

tell application "Finder"
        activate
        try
                mount volume "smb://domain;username@foo1.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

2) This one is for the user's group's networked shared folders...

Code:

tell application "Finder"
        activate
        try
                mount volume "smb://domain;username@foo2.org/group_shared/"
        end try
        repeat until (list disks) contains "GROUP_SHARED"
        end repeat
        make new Finder window to folder "SubGroup" of folder "GROUP_Private" of disk "GROUP_SHARED"
        make new Finder window to folder "GROUP_Public" of disk "GROUP_SHARED"
end tell

Now all I have to do is figure out who to "sanely" set the size of the 2 windows that I open above and put them next to each other. I say "sanely" because not everyone has as large a monitor as I..


EDIT: Incorporated guardian34's suggestion on script repetitiveness..

NovaScotian 04-12-2005 04:02 PM

list disks is an actual AppleScript Command - part of the Scripting Addition commands; so doesn't need a tell.

If you're not sure that the server is online, you can ping it first:

set myPing to do shell script "ping -c 2 192.169.1.109" -- or whatever

if the ping is successful myPing will be this string:
Code:

"PING 192.168.1.101 (192.168.1.101): 56 data bytes
64 bytes from 192.168.1.101: icmp_seq=0 ttl=255 time=1.272 ms
64 bytes from 192.168.1.101: icmp_seq=1 ttl=255 time=1.096 ms

--- 192.168.1.101 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 1.096/1.184/1.272 ms"

if the ping fails, myPing will be this string:
Code:

"PING 192.168.1.109 (192.168.1.109): 56 data bytes

--- 192.168.1.109 ping statistics ---
2 packets transmitted, 0 packets received, 100% packet loss"

Although others will no doubt suggest cleverer ways to do this, I just use:

count of paragraphs of (do shell script "ping -c 2 192.168.1.101). This pings the server twice, and if the server answers the result is 7, if not, the result is 4.

yellow 04-12-2005 04:07 PM

Well, let me modify it a bit further...

How can I add an "if/then" statement to these scripts as I have them above?

Basically (crudely) I want to have it be:

if volume "GROUP_HOME" isn't mounted, then do the mount part, elseif just open the folders.

But I think my map above is flawed, because I want to ensure that the folders get opened if the volume "GROUP_HOME" isn't mounted. I guess I'd put the "open the folders" part as part of the "then" statement as well?

Is that possible?

EDIT: I ask this because I want to put these scripts in the Dock so 1d10t-user-A can just click on the icon in the Dock and open their folders when they loose track of them. As it is, when the drives are mounted, if I click the icon again, it wants me to authenticate again.

NovaScotian 04-12-2005 04:27 PM

I'm not sure I understand your logic, but to the generic question, the structure you want is:

if BooleanExpression then
-- do your stuff for true here
else
-- do your stuff for false here
end if

You can nest these, i.e. else if BooleanExpression then, but I always end up struggling to recall when I need extra end ifs, and when not.

yellow 04-12-2005 04:43 PM

I guess the problem is, I don't know what the commands that AppleScript aknowledges are, and I'm having a bit of a time googling for them as a complete list. Anyway, what I'm trying to accomplish is akin to this (text in red is completle made up but illustrates my "logic" :) ):


Code:

if volume "group_home" is not mounted then
tell application "Finder"
        activate
          try
                mount volume "smb://domain;username@foo1.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
      else
tell application "Finder"
        activate
          make new Finder window to folder "username" of disk "group_HOME"
        end tell
end if

The reasoning behind it is this, I want to put these scripts in the Dock so 1d10t-user-A can just click on the icon in the Dock and open their folders when they loose track of them. As it is, when the drives are mounted, if I click the icon again, it wants me to authenticate again. So I want to check and see if the drive is already mounted, skip that part if it is, and just open the folder.

I found this POST that details how to check for a volume, but frankly, I don't fully understand it, but I'm going to try it.

mark hunte 04-12-2005 05:00 PM

I can not test this as i do not have the smb's
but this may work.

Code:

tell application "Finder"
        activate
        get list disks
       
        if (list disks) does not contain "GROUP_SHARED" then
                repeat until (list disks) contains "GROUP_SHARED"
                        my _Open_Mount()
                        if (list disks) contains "GROUP_SHARED" then
                                make new Finder window to folder "SubGroup" of folder "GROUP_Private" of disk "GROUP_SHARED"
                                make new Finder window to folder "GROUP_Public" of disk "GROUP_SHARED"
                               
                        end if
                end repeat
        end if
       
end tell


on _Open_Mount()
        tell application "Finder"
                try
                        mount volume "smb://domain;username@foo2.org/group_shared/"
                end try
        end tell
end _Open_Mount


yellow 04-12-2005 05:11 PM

Hmm so close!! That doesn't quite work.. it won't open the folder anew if it's been closed, by at least it's not asking for smb authentication again! Thanks for helping me get even closer to the perfect solution.

NovaScotian 04-12-2005 05:43 PM

Quote:

Originally Posted by yellow
I guess the problem is, I don't know what the commands that AppleScript aknowledges are, and I'm having a bit of a time googling for them as a complete list. Anyway, what I'm trying to accomplish is akin to this (text in red is completle made up but illustrates my "logic" :) ):


Code:

if (list disks) does not contain "GROUP_HOME" then
tell application "Finder"
        activate
          try
                mount volume "smb://domain;username@foo1.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
      else
tell application "Finder"
        activate
          make new Finder window to folder "username" of disk "group_HOME"
        end tell
end if

The reasoning behind it is this, I want to put these scripts in the Dock so 1d10t-user-A can just click on the icon in the Dock and open their folders when they loose track of them. As it is, when the drives are mounted, if I click the icon again, it wants me to authenticate again. So I want to check and see if the drive is already mounted, skip that part if it is, and just open the folder.

How about the same test as before?

yellow 04-12-2005 05:46 PM

I actually figured it out.. trial and error style:

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

This works. If the SMB share isn't mounted, it asks for crudentails to mount it and opens the folder. If the SMB share is already mounted, it opens the folder. Yay!

Now I need to figure out some error checking. I noticed that if I clicked on the "Cancel" button in the SMB share authentication window, the AppleScript would keep bringing it up forever until someone authenticated. I need to make sure that if someone clicks cancel, that it goes away.

Thanks for everyone's help so far!!

yellow 04-12-2005 06:42 PM

The saga continues..
 
Quote:

Originally Posted by yellow
Now I need to figure out some error checking. I noticed that if I clicked on the "Cancel" button in the SMB share authentication window, the AppleScript would keep bringing it up forever until someone authenticated. I need to make sure that if someone clicks cancel, that it goes away.

OK, (with help) I've sort of managed to fumble my way into something..

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

Now the problem is, if a user clicks on "OK" in the error dialog, then the script never ends/quits. If they click on "Cancel" in the error dialog, then it quits fine. Can I completely remove the "OK" button from the dialog box? Or is there something I'm missing to have the error message quit the script gracefully?

hayne 04-12-2005 06:52 PM

Quote:

Originally Posted by yellow
Or is there something I'm missing to have the error message quit the script gracefully?

I think you are missing a 'return' statement inside your "on error" clause.
I.e. after the dialog is dismissed, control gets back to the next line. That is where you want to tell AppleScript to go home - it's all over.

yellow 04-12-2005 06:57 PM

I'm not sure what the proper terms for an AS 'return' would be. I imagine a conditional 'end tell', but I got compilation errors when I tried that. But I know what you mean from what little C I've learned.

I removed the "OK" button from the dialog and that worked.

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." buttons "Cancel"
                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

All that remains now is working out how the Finder does it's position and boundry geometry! Thanks for all the help, everyone! There's no way in hell I could have gotten to this point without you!

NovaScotian 04-12-2005 07:01 PM

Don't know how to trap the "Cancel" problem - the AppleScript isn't doing it and don't have my own PC server running.

You might think about some means of escaping from the "repeat until .." loop too; suppose the damned server won't mount for some reason - the test will never be satisfied. The "with timeout" structure doesn't work within the AS itself - it applies when you are concerned that a "tell" segment will fail when the "told app" fails to respond. AppleScript and its Additions are not very good at timing in ticks or finer scales and I don't know what OSAXen you have. Another way is to increment an integer and stop it when it reaches some large number, i.e. limit the number of loops allowed.

An Afterthought Since Applescript isn't good at timing, you might just put a delay of 2 seconds in the repeat until loop so it will only execute every 2. Otherwise the loop will run as fast as it can and eat CPU time.

hayne 04-12-2005 07:14 PM

Quote:

Originally Posted by yellow
I'm not sure what the proper terms for an AS 'return' would be.

Just:
return
will return to the caller of a function. I believe that a simple 'return' at the top level in a script will exit the script.

O'Reilly book sample chapter on AppleScript control-flow keywords:
http://www.oreilly.com/catalog/aplsc...pter/ch07.html

yellow 04-12-2005 07:46 PM

Sweet! Thanks! Trying my hand at XCode now to build a cocoa app to do this (yeah right, good luck yellow! :))!

guardian34 04-12-2005 08:41 PM

Does this work any better?
Code:

property user : "bruce"
property theHost : "192.168.34.99"
property diskName : "TEST"

tell application "Finder"
        activate
        if (exists disk "TEST") is false then
                try
                        mount volume "smb://" & user & "@" & theHost & "/" & diskName & "/"
                on error
                        display dialog "Enterprise authentication failed." buttons "Cancel" default button "Cancel"
                end try
        end if
       
        repeat until (list disks) contains diskName
        end repeat
       
        with timeout of 60 seconds
                try
                        tell application "Finder" to (make new Finder window to folder user of disk diskName)
                on error
                        display dialog "Unable to display folder." buttons "Cancel" default button "Cancel"
                end try
        end timeout
       
        set current view of Finder window 1 to list view
        -- set bounds of Finder window 1 to {20, 84, 600, 400}
end tell

I can't test the timeout statement. If I rename the user's folder, Finder returns an error.

The last line is an example of how to size a Finder window. If you're trying to determine a good size, move the window yourself, then run this statement in a new Script Editor window:
Code:

tell application "Finder" to return bounds of front Finder window

bramley 04-13-2005 05:23 AM

There are a couple of scripts here that give the full works with regard to window sizes etc. They are compiled ones for some reason. The scripts are called 'Tile Front Windows' and 'Open Tandem Browser Windows'

If the only reason for wanting to use AS Studio is to allow the user to enter their user name/passwords, bear in mind that 'normal' AS will allow you to enter stuff into the script with 'display dialog'

Code:

set response to display dialog "What is the unladen weight of a sparrow?" default answer "African or European"
display dialog text returned of response
display dialog button returned of response

The use of 'default answer' ensures that a record is returned containing the ID of the button pressed and the text entered by the user.

On the other hand, if yellow just wants to use AS studio because he can ...... go for it!

yellow 04-13-2005 07:17 AM

Thanks, I'll look into both of those last 2 posts when I hit work today.

I actually bit the bullet and picked up an AppleScript book last night at my local book gigantomart. I figured I better start somewhere, I can always be begging others to do my work for me.

I think that I want to use ASS (heehee!) mainly because it'll give me a nice polished looking app that I can use to gather username, domain, the user's view preferences, and save all that in a .plist file. Then give them 3 1d10t buttons at the bottom of the app window that allows them to mount/open their Home, Public Shared, and Private Shared. One polished app, incorporating all that they need. I'm also hoping that I can figure out how to get this to log to /var/log/system.log.

Definitely not necessary, but something I'd like to learn.

guardian34 04-13-2005 09:16 AM

yellow, would you be using OS X 10.3?

yellow 04-13-2005 10:37 AM

Yes.. all the Macs this is destined to end up on will be running Panther (10.3.8).

Raven 04-13-2005 10:40 AM

Normally those commands should work on other versions too though... Its all pretty much standard OS X applescript stuff.

yellow 04-13-2005 11:33 AM

Quote:

Originally Posted by guardian34
Does this work any better?

Actually no, for some reason it doesn't. Even if I provide the correct password, it continually fails. It seems to be attemoting to capitalize the "user" args, which fails. But there are some very good pieces, like the time out, that I will add to the process.

guardian34 04-13-2005 01:51 PM

Quote:

Originally Posted by Raven
Its all pretty much standard OS X applescript stuff.

I asked because I wanted to point out that, if you were to use AppleScript Studio, and you have OS X 10.3 or later, you could use Cocoa Bindings. (In my experience, not many people know about that feature.) That link is geared toward Cocoa/ObjC, but you can also use Cocoa Bindings in AppleScript Studio. That said... I don't want to distract from the case at hand, so I'll just set this idea the on shelf, and you can look into it if you want to.

yellow 04-13-2005 01:55 PM

Cool, thanks! I'll definitely look into it.

NovaScotian 04-13-2005 02:58 PM

Is there an example anywhere of Cocoa Bindings in use in an AppleScript App?

guardian34 04-13-2005 04:12 PM

Quote:

Originally Posted by NovaScotian
Is there an example anywhere of Cocoa Bindings in use in an AppleScript App?

I don't know of any myself. I won't try to explain the concepts, but I can give you an example. Let's base it off of yellow's situation.

Frist, in Xcode's main menu, goto Project > Edit Active Target 'name', then, in the resulting window, choose Info.plist Entries > Simple View > Basic Information. Fill in the Identifier box. This will be used by the user defaults system. Apple's programs typically use com.apple.name. You might enter something like "yellow.OpenFolders" (sans quotes).

Now, move over to Interface Builder. Let's say you have a text box where the user enters the domain. With said text box selected, open the Info panel and choose Bindings from the menu. You can bind any of the listed properties. For now, click on "value". Enter a name in the "Model Key Path" box, and press enter. (The "Bind" checkbox will automatically be checked.) This is the key that will be stored in the preference list (e.g. yellow.OpenFolders.plist). Of course, you will also use this name to get the key's value out of the preference list. We'll assume you entered "Domain".

While we're in Interface Builder, you need to set "File's Owner" to trigger the "will finish launching" event handler. To do this, select "File's Owner" from the instances tab, open the Info window, and choose AppleScript from the menu. Expand the Application list, and check "will finish launching". Assign this event to a script in the box below.

[Still with me? :)]

Move back over to Xcode. Open the script that will handle the "will finish launching" event. Assuming you have nothing else in this handler, it needs to look like this:
Code:

on will finish launching theObject
        make new default entry at end of default entry of user defaults with properties {name:"Domain", contents:"example"}
end will finish launching

This creates a default value, ensuring that there is something to be read later. You should also enclose this, and similar commands, in a try statement.

Let's see how things work so far. Build and run your application. The domain text box should contain the default value you entered in the "will finish launching" handler. Change the value of the text box, then quit and relaunch the application. The text box should contain the new value you entered.

Well, that should be enough for now. Thanks for reading!

yellow 04-13-2005 04:40 PM

Quote:

Originally Posted by guardian34
[Still with me? :)]

Hell no. Sorry, couldn't resist! :)

yellow 04-13-2005 05:51 PM

Just an update..
 
As I read through the AppleScript book, I've learned a little bit and added some stuff and cleaned up some stuff. Just though I'd update with where I'm at currently (if you care :)):

Code:

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 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 ¬
                        "Enterprise 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 Help Desk 555-1212" buttons "Cancel" default button 1 with icon stop
        end try
end tell


NovaScotian 04-13-2005 06:11 PM

Looking rather pro, yellow, for a guy who just picked up AS the other day for the first time.

And Guardian34 - thanks for the very cogent explanation. Now it's clear to me what "binding" is. In languages like SuperCard, handlers are part of GUI objects themselves. From your explanation and Apple's, Cocoa Binding is the means of linking the pieces of the GUI to the handlers that deal with them, i.e., creating the event loop linkages for you in essence. Have I got the general picture? Having saved your explanation, I'll try it (and I'll bet yellow will too).

Thanks.

guardian34 04-13-2005 08:39 PM

Quote:

Originally Posted by NovaScotian
Binding is the means of linking the pieces of the GUI to the handlers that deal with them

No. Perhaps this page will give you a better picture: Binding your Preferences in Cocoa

Quote:

Originally Posted by NovaScotian
In languages like SuperCard, handlers are part of GUI objects themselves.

In that respect, Visual Basic [:eek:] works the same way. However, AppleScript Studio does not. In AppleScript Studio, you choose which of an object's events can be triggered, as well as which scripts will handle those events.

NovaScotian 04-14-2005 10:47 AM

Quote:

Originally Posted by guardian34
No. Perhaps this page will give you a better picture: Binding your Preferences in Cocoa

In that respect, Visual Basic [:eek:] works the same way. However, AppleScript Studio does not. In AppleScript Studio, you choose which of an object's events can be triggered, as well as which scripts will handle those events.

A subtle conceptual difference, but then I still don't get it completely. Thanks.

guardian34 04-14-2005 11:02 AM

Quote:

Originally Posted by yellow
As I read through the AppleScript book, I've learned a little bit and added some stuff and cleaned up some stuff. Just though I'd update with where I'm at currently (if you care :))

Looks good yellow.

bramley 04-14-2005 01:01 PM

Looks fine, yellow.

Some changes you could make. If you check to see if the network share doesn't exist - and do that first, you can eliminate the code duplication. Also assigning a pointer to the new Finder window makes it clear which window will change. Users may already have a 'Finder window 1' open.

Code:

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


hayne 04-14-2005 02:09 PM

In general in programming it is a bad idea to loop while doing nothing unless you are deliberately trying to consume CPU power.
Thus I would prefer to see some sort of 'delay' inside the repeat loop that waits for the disk to mount. Something like this:
Code:

repeat until (list disks) contains "GROUP_HOME"
    delay 2  -- wait 2 seconds before checking again
end repeat

And it is also a bad idea in general to have a loop which may not terminate. The above loop will go on forever if the disk doesn't mount for some reason.
It would be better to have it exit the loop after a while - e.g. after a fixed (large) number of iterations. Something like:
Code:

set timeWaited to 0
repeat until (list disks) contains "GROUP_HOME"
    delay 2  -- wait 2 seconds before checking again
    set timeWaited to timeWaited + 2
    if timeWaited > 60 then
        beep
        display dialog "GROUP_HOME disk failed to mount"
        return
    end if
end repeat


yellow 04-14-2005 03:20 PM

Thanks for the continued input, I really appreciate it!

I took (parts) of the your suggestions and put them in the code. I think the removal of the code redundancy will wait until v2 (Cocoa version?), as the script/app is very small, it doesn't really bother me at the moment.. even though it's probably "good form" programatically. I just need to get something workable 'out-the-door' (so to speak) and then I can concentrate on cleaning it up and stuff like asking for/using values like 'username', so I don't have to recreate the wheel for each user.

You're quite right about the 'Finder window 1' (I actually took that from recording some actions in the Finder to see what the associated AppleScript would be), so I've implemented your use of the mountFoo variables.

And the addition of the delay and associated sane exiting on failure is also a great help and has made it's way into the script.

Thanks again!

EDIT: I'm experimenting with:

set _username_ to "yellow" as string

With hopes that this will populate _username_ throught the script. I'm just not sure how it will be handled in the "mount volume" line.

RE-EDIT: I used guardian's "&_username_&" suggestion and it worked!

bramley 04-14-2005 03:35 PM

Quote:

Originally Posted by hayne
In general in programming it is a bad idea to loop while doing nothing unless you are deliberately trying to consume CPU power.
Thus I would prefer to see some sort of 'delay' inside the repeat loop that waits for the disk to mount.

My next suggestion was going to be removing the repeat loop altogether. :)
The previous 'on error' statement should catch any mounting issues. Yellow, you can get error codes sent to the 'on error' handler so that you can handle different errors in the same handler. Bet it's in your book.

I'd replace the loop with a single delay statement. The second error handler would deal with any problems associated with being unable to get the correct full path to the SMB share.

yellow 04-14-2005 03:39 PM

Quote:

Originally Posted by bramley
Bet it's in your book.

Hehe, yep, 6 chapters to go before "Error handling"

I find the evolution of the script quite interesting and wonder what the final will look like once I reach the end of the book and you all get tired of helping me. :)

mark hunte 04-14-2005 04:17 PM

try using

Code:

  tell application "System Events"
                set _username_ to get name of current user
end tell

EDIT>>

this gets the OSX user name. Not sure if you are using the same user name on the smb

yellow 04-14-2005 04:45 PM

Quote:

Originally Posted by mark hunte
try using

Code:

  tell application "System Events"
                set _username_ to get name of current user
end tell


That's freaking sweet! Thanks!

guardian34 04-14-2005 05:07 PM

Quote:

Originally Posted by bramley
Users may already have a 'Finder window 1' open.

In that statement, 1 is an index value, which is "the number of the window in the front-to-back layer ordering". The front window will always have an index of 1. (An alternative way to address the front window is to say "front Finder window".) However, it would be better to address a window by name or id (just in case).


Quote:

Originally Posted by hayne
In general in programming it is a bad idea to loop while doing nothing unless you are deliberately trying to consume CPU power.

repeat until (list disks) contains "GROUP_HOME"
delay 2
end repeat

The delay command is a CPU hog (check Activity Monitor sometime). If you are going to delay for more than a couple seconds, I would try to use this instead:
Code:

repeat until (list disks) contains "GROUP_HOME"
    do shell script "sleep 2"
end repeat

Quote:

Originally Posted by hayne
It would be better to have it exit the loop after a while

Definitely! Good solution.


Quote:

Originally Posted by mark hunte
set _username_ to get name of current user

For reference, you can also get "full name" and "home directory".

hayne 04-14-2005 05:24 PM

Quote:

Originally Posted by guardian34
The delay command is a CPU hog (check Activity Monitor sometime).

I had heard this in the past and so before recommending the use of 'delay', I checked the CPU usage of the simple script below and found it to be minimal on my 1.2GHz iBook with 10.3.8.
Does this script take much CPU on your system?
Code:

tell application "Finder"
    repeat
        delay 60
    end repeat
end tell


yellow 04-14-2005 05:38 PM

Quote:

Originally Posted by hayne
Does this script take much CPU on your system?

Yes it does.. (I have a dual processor 1.33GHz G4), I'd say the average CPU% usage was at ~100%.

guardian34 04-14-2005 06:51 PM

Quote:

Originally Posted by hayne
Does this script take much CPU on your system?

On my Mac mini, total CPU usage was ~100%, with Script Editor using ~70%.

hayne 04-14-2005 07:57 PM

Quote:

Originally Posted by guardian34
On my Mac mini, total CPU usage was ~100%, with Script Editor using ~70%.

I think that's the problem.
Script Editor isn't a good test environment for determining deployment considerations.
When I run the above script from Script Editor on my iBook, it also shows CPU usage approaching 100%. But if I save it as a compiled Application, and run that, the CPU usage averages less than 1 %.

yellow 04-14-2005 08:47 PM

Same here, saved as an app, it maxed out at 1%. Larger RAM usage then with Script Ed, though.. about 50% larger.

guardian34 04-14-2005 09:01 PM

I don't save everything as a compiled application though.

NovaScotian 04-15-2005 07:45 AM

Most of my AppleScripts are inserted as scripts into QuicKeys X3 so I can run them with a hotkey or easily call them from another QK sequence. Run in that way, a delay is not the CPU eater I thought it was; QK uses less than 4% of CPU time, declining to 1 at the point that the delay times out. Perhaps an AS delay is now using the sleep shell function.


All times are GMT -5. The time now is 06:03 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.