Open an Application from within RR-Andre's a Genius

Ken Ray kray at sonsothunder.com
Tue Sep 28 10:46:53 EDT 2004


On 9/28/04 12:17 AM, "Kathy Jaqua" <kgjaqua1 at sbcglobal.net> wrote:

> Thanks Ken,
> 
> You guys are so bright! This genius list is growing.

<blush>

> So where should I place your learned following script:
> (which I might add is just a tiny bit over my head ;)

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




More information about the use-livecode mailing list