Returning Multiple Values From An AppleScript

I'm tinkering with getting status messages from iChat, Adium, and Skype, but I'm running into a problem with getting all 3. Here's what I have:

1. An AppleScript with the following code:

tell application "System Events"
count (every process whose name is "Adium")
if result = 1 then
tell application "Adium" to get my status message
end if

count (every process whose name is "iChat")
if result = 1 then
tell application "iChat" to get status message
end if

count (every process whose name is "Skype")
if result = 1 then
tell application "Skype" to send command "GET PROFILE MOOD_TEXT " script name "test"
end if
end tell


This checks to see if any of those apps are running, and if they are, it fires the command.

What I'd like to do is set each returned string to a different variable, like AdiumStatus, iChatStatus and SkypeStatus -- but I don't know how to code this. I tried using "set AdiumStatus to my status message" (and the like), but that didn't work.

Any ideas? I know this should be simple, but out of practice with AS!

Dual 2.0GHz G5, Mac OS X (10.4)

Posted on Jan 25, 2007 12:27 PM

Reply
5 replies

Jan 25, 2007 1:13 PM in response to Maury Mccown

Your script is only querying the respective apps for their status. You don't do anything with the data, so it's lost.

You need to store the result somewhere and return it at the end of your script. Something like:

<pre class=command>on getStatus()
set adiumStatus to ""
set iChatStatus to ""
set skypeStatus to ""

tell application "System Events"
count (every process whose name is "Adium")
if result = 1 then
tell application "Adium" to set adiumStatus to (get my status message)
end if

count (every process whose name is "iChat")
if result = 1 then
tell application "iChat" to set iChatStatus to (get status message)
end if

count (every process whose name is "Skype")
if result = 1 then
tell application "Skype" to set skypeStatus to (send command "GET PROFILE MOOD_TEXT " script name "test")
end if
end tell

return {adiumStatus, iChatStatus, skypeStatus}
end getStatus</pre>

Now your script can call getStatus() and get back a list of the status settings of each app.

Alternatively you could run separate handlers, one per app, which would be a better approach if you wanted to add many more apps and/or wanted the ability to query them independently.

If you do maintain one handler you might be better off rewriting the code as:

<pre class=command>...
tell application "System Events"
set runningApps to (get name of every application process)
end tell</pre>

Then you can say:

<pre class=command>if runningApps contains "iChat" then
...</pre>

The advantage to this approach is that you only query the running application list (a fairly heavy query) once as opposed to the original method which asked three separate queries, one per app.

Jan 25, 2007 1:26 PM in response to Maury Mccown

I'm not quite sure what the problem is, since your title suggests that this is inside a function and you want to return multiple values from it (solution: return a list) while your post says something different. You say you had a problem with (basically) using "set iChatStatus to" instead of get. Okay, let's examine the middle code block with that substitution made, and the variable name at the end to check the results (in Script Editor, at least):

tell application "System Events"
count (every process whose name is "iChat")
if result = 1 then
tell application "iChat" to set iChatStatus to status message
end if
end tell
iChatStatus

If iChat is running, this works perfectly well. (At least, it does on my machine!)

And if iChat isn't running, this won't launch it. That's nice.

But if iChat isn't running, it also gives an error message: "The variable iChatStatus is not defined."

The solution? Define iChatStatus ahead of time. The following will NOT give an error if iChat isn't running:

set iChatStatus to missing value
tell application "System Events"
count (every process whose name is "iChat")
if result = 1 then
tell application "iChat" to set iChatStatus to status message
end if
end tell
iChatStatus

It has an extra advantage, too, in that you can test to see if iChat was running afterwards by
if iChatStatus is missing value then
-- iChat wasn't running
end if

Jan 25, 2007 8:34 PM in response to Maury Mccown

>I was going to simply do "display dialog AdiumStatus" right after "return {AdiumStatus, iChatStatus, SkypeStatus}" but nothing happens.

It won't. 'return' hands control back to the caller, so nothing after the 'return' will work. If you want to display dialog you'll need to do it before you return.

<pre class=command>...
display dialog adiumStatus
return {adiumStatus, iChatStatus, skypeStatus}
end getStatus</pre>

Alternatively, check the results handed back by the handler...

<pre class=command>set appsStatus to getStatus()
-- now appsStatus contains the returned value from getStatus(), of which 'adiumStatus' is the first value, so:
display dialog (item 1 of appsStatus</pre>

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Returning Multiple Values From An AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.