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)

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.


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.