Page 1 of 1

Problem invoking AppleScript

Posted: Tue Jul 10, 2012 1:52 pm
by grovecat
My app needs to be brought to the front when a timed event occurs and I have been doing some very simple tests to try to get an AppleScript to do that.

The following works OK in AppleScript

Code: Select all

tell application "Finder"
	set the frontmost of application process "Mail" to true
end tell
I tried to incorporate it into an app with the following:

Code: Select all

on mouseUp
   if the platform is "MacOS" then
      wait 5 secs
      put "tell application " Finder" " & cr \
            & "set the frontmost of application process "Mail" to true" & cr \
           & "return (the result)" & cr & \
      "end tell" into tScript
      do tScript as AppleScript
      if the result is "execution error" then answer "AS Error" & OK
      answer "Mail should be on top" with OK
   else answer "Wrong Platform" with OK
end mouseUp
The ID gives a syntax error:
button "Button": compilation error at line 5 (Commands: missing ',') near "Mail", char 18

I don't see what the problem there is, and the quotes balance, but I must be missing something.

So I decided to avoid quoting the Finder and Mail as follows:

Code: Select all

on mouseUp
   put "Finder" into tFinder
   put "Mail" into tMail
   if the platform is "MacOS" then
      wait 5 secs
      put "tell application " & tFinder & cr \
            & " set the frontmost of application process " & tMail & " to true" & cr \
           & "return (the result)" & cr & \
      "end tell" into tScript
      do tScript as AppleScript
      if the result is "execution error" then answer "AS Error" & OK
      answer "Mail should be on top" with OK
   else answer "Wrong Platform" with OK
end mouseUp
In the above case I get the "Mail should be on top" message but it isn't.

I'm not getting anywhere with this so help would be appreciated.

TIA
Don

PS I have not used AppleScript except as above.

Re: Problem invoking AppleScript

Posted: Tue Jul 10, 2012 2:13 pm
by FourthWorld
I wonder to what degree AppleScript will continue to be supported now that Apple prohibits its use in sandboxed apps.

Does anyone here have a definitive word from Apple on interoperability options in the sandbox?

Re: Problem invoking AppleScript

Posted: Tue Jul 10, 2012 3:49 pm
by bn
Hi Don,

you forgot to quote Finder and Mail for applescript. Try this

Code: Select all

on mouseUp
   if the platform is "MacOS" then
      wait 5 secs
      
      put quote &  "Mail" & quote into tMail
      put  "tell application " & tMail & cr \
            & "activate" & cr \
            & "end tell" into tScript
      
      do tScript as AppleScript
      
      if the result is "execution error" then answer "AS Error" with OK
      answer "Mail should be on top" with OK
   else 
      answer "Wrong Platform" with OK
   end if
end mouseUp
This script runs on MacOSX 10.6.8. If Mail is not open it will start Mail and bring it to front otherwise it will just bring Mail to front. Mind you it takes 2 to 3 seconds to bring Mail to front.

You don't have to explicitly ask for the result in Applescript. If you want to return a variable just return it. It shows up in the result on the LiveCode side. If anything goes wrong with applescript Applescript will return an error which will be in the result. So it is good to check the result after do tScript as Applescript.

I often find it easier to put an applescript that I have tested in either ScriptEditor or another Applescript Editor into a field as it is and just say:
do field "myField" as applescript. All these quotes etc get confusing.
If you have further questions feel free to ask.

Kind regards



Kind regards

Re: Problem invoking AppleScript

Posted: Wed Jul 11, 2012 5:20 am
by grovecat
The idea of using a field is brilliant and works perfectly.

Many thanks for that.

Don