• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script Objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script Objects


  • Subject: Re: Script Objects
  • From: Michael Terry <email@hidden>
  • Date: Wed, 16 Oct 2002 15:48:27 -0700

On 10/16/02 6:58 AM, "Steve Suranie" <email@hidden> wrote:

> I'm trying to run this script:
>
> script checkServerStatus
>
> property serverName : ""

You aren't using this property so no need to define it. Properties are
useful for packaging data into a script object that its handlers will always
need, like an alphabet lookup table, or for giving a script object initial
values for each new instance created from it. Here you're just passing the
server name to the script object's handler, which is fine, it's all you need
to do, and so no need for the property.

> to checkThisServer to thisServer
>
> set serverName to thisServer

Again, no need to set the value of any properties, unless you were planning
on adding handlers to the script object and using the properties value for
other functions later in the script.

> if not (disk thisServer exists) then
>
> beep
> display dialog "The " & thisServer & "needs to be mounted in order for this
> script to function."

It would be better not to have the user interaction in this object, and
instead return a value indicating the result of the server check. How it is
now, the rest of the script will continue to run after this dialog.

> end if
>
> end checkThisServer
>
> end script
>
> tell checkServerStatus
> set thisServer to "StorageServer"

> tell checkServerStatus to checkThisServer to thisServer
> end tell

The second tell block nested within the first is redundant. You could say
'checkThisServer to thisServer'.

> but I keep getting the following error message:
>
> <<checkServerStatus>> doesn't understand the disk message

Probably because disk is an application keyword understood by the Finder,
but you forgot to put that statement in a Finder tell block.

Here's how I would do it:

script checkServerStatus

to checkThisServer to thisServer

tell application "Finder"

if not (disk thisServer exists) then
return false
else
return true
end if

end tell

end checkThisServer

end script

set myServer to "Font HD"
tell checkServerStatus to set serverCheck to checkThisServer to myServer

if not serverCheck then
beep
display dialog "The " & myServer & " needs to be mounted in order for
this script to function."
return
end if

Note that the handler simply returns true or false, and the program logic at
the top level decides what to do with it and what type of user interaction
to have. This is useful because you can only exit the script immediately
from the top level. That's what the return statement does, making sure none
of the rest of your script is run.

In reality, this handler need not be in a script object if you include it
with the rest of your script. It would be simpler to just call it as a
handler. But it's closer to what you want to do, which is load the handler
from another file--see below.

> Any help would be appreciated. I'm trying to check if certain servers are
> mounted before I continue with a script. Since on most of the scripts I write
> I have to check servers I thought I would just write a script object and pass
> along to it which server(s) I am checking. Am I not understanding the concept
> of the script object? Is there another way to do this? Also, how would I call
> the script object from another script - do you use the path to the script
> something like this:

> tell desktop folder:AppleScripts:scriptObjects:checkServerStatus
> set thisServer to "StorageServer"
> tell checkServerStatus to checkThisServer to thisServer
> end tell

You're on the right track. In order to load handlers from another file do
this: Strip off the "script/end script" wrapper from the handler you choose
to use and save it as a compiled script somewhere. Then call it with this
syntax:

property checkServerStatus: load script alias "disk:folder:folder:fileName"

If you keep the file somewhere within one of the folders with constants
defined by the 'path to' command in the Standard Additions dictionary then
you can write it like this, as an example:

property checkServerStatus: load script (path to scripts folder from user
domain)

or relative to the folder:

property checkServerStatus: load script alias ((path to scripts folder from
user domain as text) & "ScriptLibs")

Then you'd call the script object, which has been loaded into the property,
thusly:

tell checkServerStatus to checkThisServer to myServer
set serverCheck to result

> Thanks
>
> Steve

Mike
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: How to list local volumes ...
  • Next by Date: Re: Pie chart importing script-success!!!!
  • Previous by thread: Re: Script Objects
  • Next by thread: Re: Script Objects
  • Index(es):
    • Date
    • Thread