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)

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.