AppleScript from RunRev, An Example (Andrew and Ken)
Dan Shafer
revdan at danshafer.com
Tue Sep 28 20:21:47 EDT 2004
I got this already, thanks to the list.
I'm not sure this subject will come up in my books any time soon
because it's platform-specific and I don't know how many folks are
interested in using AppleScript with Rev in any case.
Dan
On Sep 28, 2004, at 4:27 PM, Kathy Jaqua wrote:
>
> An example of using appleScript from Run Rev to
> open an Application and check the result (in case it
> was not installed is the Mac OS 9, OS X)
>
> Ken this is a great example of RunRev's ability to
> use appleScript. This line by line explanation was of
> great help to me on many levels. If you don't mind I
> am going to forward this to Dan Shafer for future
> publication inclusion considerations. It will help
> many others like myself who are new to appleScript
> calls using the Transcript Language. I know it took
> your valuable time to carefully explain each step.
> It's also a good example of offsets and function
> calls. (It's been years sense hyperCard and a lot of
> us are a little rusty.) We need more of these
> examples and I hope runRev is listening :)
>
> Again many Thanks.
> Kathy Graves Jaqua
> A Wildest Dream Software
> kgjaqua1 at sbcglobal.net
>
> The scripts to which I have been referring are as
> follows:
>
> A. From Andrew Garzia (using appleScript only)
>
> B. From Ken Ray (calling appleScript from RunRev or
> MetaCard)
> --------------------------------
>
> A. Opens the Application "iCal" (Traditional
> appleScript)
>
> Kathy try putting this on a field called "myscript":
>
> tell application "iCal"
> activate
> end tell
>
> put a button with the following script
>
> on mouseUp
> do field "myscript" as applescript
> end mouseUp
> ---------------------------------------
>
> B. Opens Application "iCal" and checks to see if it is
> open (Using Transcript to call Applescript)
>
> OK, Kathy, no problem. You already know from Andre's
> script that you can use
> Rev to run an AppleScript, just as if you'd done it in
> Apple's Script Editor
> application. The AppleScript is executed by Rev using
> the syntax:
>
> do <scriptText> as AppleScript
>
> where <scriptText> is the script you want to execute.
> As you are currently
> doing, you can provide the script by using the
> contents of a field. But you
> can also provide using a variable. So your "Open iCal"
> script (which is
> currently sitting in a field) could also be executed
> like this:
>
> on mouseUp
> put "tell application" && quote & "iCal" & quote &
> return & \
> "activate" & return & "end tell" into tScript
> do tScript as AppleScript
> end mouseUp
>
> This would mean you could remove that extra field if
> you wanted to because
> it's all in the script.
>
> The nice thing about Rev is that you can do certain
> things to shorten the
> script and/or make it more readable. For example,
> "return" can be replaced
> with "cr" (which does the same thing), and I
> personally don't like typing
> "quote & <quotedString> & quote" in a script
> (especially if I'm doing it a
> lot), so I created a function called "q" that puts
> quotes around things:
>
> function q pWhat
> return quote & pWhat & quote
> end q
>
> (BTW: I forgot to include this function with my last
> email - sorry...)
>
> Additionally, you can use the backslash character to
> break script lines for
> readability. I do this with things like AppleScript so
> that one visible line
> in Transcript corresponds to one visible line in
> AppleScript. You don't have
> to do this, but I happen to like it. So your "Open
> iCal" script can now look
> like this:
>
> on mouseUp
> put "tell application" && q("iCal") & cr & \
> "activate" & cr & \
> "end tell" into tScript
> do tScript as AppleScript
> end mouseUp
>
> So the code I'd sent you:
>
> function isAppRunning pAppname
> replace ".app" with "" in pAppName
> put "tell application " & q("Finder") & cr & \
> "return the processes" & \
> cr & "end tell" into tAS
> do tAS as AppleScript
> put the result into tProcs
> return (offset("process" && q(pAppName),tProcs) <>
> 0)
> end isAppRunning
>
> Would be called like this:
>
> on mouseUp
> if isAppRunning("iCal") then
> answer "iCal's running!
> else
> answer "iCal's not running."
> end if
> end mouseUp
>
> And the code does this (I'm adding line numbers for
> clarity):
>
> 1: function isAppRunning pAppname
> 2: replace ".app" with "" in pAppName
> 3: put "tell application " & q("Finder") & cr & \
> "return the processes" & \
> cr & "end tell" into tAS
> 4: do tAS as AppleScript
> 5: put the result into tProcs
> 6: return (offset("process" && q(pAppName),tProcs)
> <> 0)
> 7: end isAppRunning
>
>
> In line 1, we pass in the name of the application we
> want to check as a
> parameter to the isAppRunning function. The name can
> either be the short
> name of the application (like "iCal") or the name of
> the application with
> its extension ("iCal.app").
>
> In line 2, since the AppleScript that runs returns a
> list of running
> applications *without* their extensions, I want to
> eliminate the ".app" if
> for some reason it was passed to the function, so I
> call the "replace"
> command to do that job.
>
> Lines 3 to 4 you are more familiar with, in that they
> run an AppleScript
> that returns the list of running processes
> (applications).
>
> Whenever AppleScript returns a value, Rev can retrieve
> that value by
> checking "the result" right after the AppleScript is
> called. Line 5 does
> this, and puts the list of running applications into
> the local variable
> 'tProcs'.
>
> In line 6 we check to see if the application name that
> was passed into the
> function exists in the list of currently running
> applications that
> AppleScript returned. This line is a "combination"
> line of code; that is, it
> is the collapsed form of:
>
> if offset("process" && q(pAppName),tProcs) <> 0 then
> return true
> else
> return false
> end if
>
> Since AppleScript returns the list in its own format,
> the variable tProcs
> looks something like this:
>
> {application process "loginwindow" of application
> "Finder", application
> process "Dock" of application "Finder", application
> processs "iCal" of
> application "Finder"}
>
> So to determine if "iCal" is in this list, I'm
> checking to see if the
> phrase:
>
> process "iCal"
>
> exists in the list of processes using the offset()
> function. If it does, it
> returns a value that corrresponds to the location in
> the string where I can
> find it; if not, it returns 0. For the purposes of my
> code, all I care about
> is whether it returns 0 or not; 0 is "false", anything
> else is "true".
>
> Then we end the function in line 7 and we're done! So
> if you include the
> isAppRunning() function along with the q() function in
> your script, and call
> it like this:
>
> if isAppRunning("iCal") then ...
>
> you should be in good shape. :-)
>
> Hope this helps,
>
>
> Ken Ray
> Sons of Thunder Software
> Web site: http://www.sonsothunder.com/
> Email: kray at sonsothunder.com
>
>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
More information about the use-livecode
mailing list