Using message box to write script to image

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Using message box to write script to image

Post by josh@ACP » Tue Feb 17, 2015 7:40 pm

Hello,

I am trying to add script to an image (q1a1f.jpg) using the message box and can't figure out where I am messing up. The first quote below is in theory what the script should look like for the image (q1a1f.jpg). The second quote below is what I am typing into the message box to get to the script in quote 1 (but it's not working) . I am getting this error "Script compile error: Error description: Expression: double binary operator"

global gPath
on mouseUp
set the PlayLoudness to 100
if the environment <> "mobile" then
put "aq1ctest.wav" into tSndPath
play tSndPath
else
put specialFolderPath("engine") into gPath
play audioClip (gPath &"/Audio/aq1ctest.wav")
end if
end mouseUp
set the script of image "q1a1f.jpg" to “global gPath” & CR & “on mouseUp” & CR & “set the PlayLoudness to 100” & CR & “if the environment <> & quote & "mobile" & quote & then” & CR & “put & quote & "aq1ctest.wav" & quote & into tSndPath” & CR & “play tSndPath” & CR & “else” & CR & “put specialFolderPath(& quote & "engine" & quote &) into gPath” & CR & “play audioClip (gPath & & quote & "/Audio/aq1ctest.wav" & quote &) & CR & “end if” & CR & “end mouseUp”;
Any help would be great!


thanks!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Using message box to write script to image

Post by dunbarx » Tue Feb 17, 2015 7:51 pm

Hi.

I have never used the "play" command, but is your filePath a valid one? It would default to the, er, default folder the way you have it written, that is, without a full pathname.

Craig Newman

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using message box to write script to image

Post by Klaus » Tue Feb 17, 2015 8:02 pm

Hi Josh,

1. welcome to the forum! :D

2. There are a lot of QUOTES missing in your script to set the script:
Look out for:
... & quote &) &...
... & “if the environment <> & quote & "mobile" & quote & then”...

Should be:
... & quote & QUOTE & ") " & QUOTE &...
... & “if the environment <> " & quote & "mobile" & quote & "then”...

I recommend to create little function in your stack script to save a lot of typing :D

Code: Select all

function q theString
  return QUOTE & theString & QUOTE
end q
That will solve the problem of missing quotes, use it like this:
... & q(")")...
& “if the environment <> " & q("mobile") & "then”...

Hint:
Instead of setting a script this way, why not use a pre-made behavior?
Know what I mean?


Best

Klaus

josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Re: Using message box to write script to image

Post by josh@ACP » Thu Feb 19, 2015 3:33 pm

Thanks Klaus!

So I tried following you directions and even created the function but then got confused and took a day off. I just tried looking at it again and am now even more confused... I am still getting stuck on the quotes and how it should be written.

I also have no idea what you mean by a 'pre-made behavior' unless you are referring to the function.

I will be working on creating possibly 400 apps (over a few years) with 50 or so images in each app. The apps will be based off of the same template design app but with different information/iamges/sounds/etc. I plan to use the message box function to simplify programming once I figure this question out.

thanks,

Josh

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using message box to write script to image

Post by Klaus » Thu Feb 19, 2015 5:52 pm

Hi Josh,

yep, working with lots of neccessary quotes can be a PITA! :D

OK, the "behavior" concept has been introduced a couple of versions ago and is a wonderful thing!
Basically you can tell any LC object to use a premade script of a button, so the actual object does NOT have any script!

Try this:
1. Create a button with e.g. your complete "mouseup" script as in your first posting, then you can hide the button.
2. Create any other object e.g. an image
3. Use the message box (or any other script):
set the behavior of img "your image without script yet here" to the long ID of btn "the button with the complete mouseup script"

Now click your image and the buttons script will be executed.
This is it basically :D

As you can see no more problems with missing quotes when (not) "setting the script" of any (new or old) LC control. :)


Best

Klaus

josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Re: Using message box to write script to image

Post by josh@ACP » Thu Feb 19, 2015 6:27 pm

thanks!

I would still be interested in getting the 'message' script for my original post figured out. I can then use excel (and some tricks i've learnt) to mass edit my scripts when I am working with more detailed apps. I will though compare both methods to see which is most efficient in the long run.

regards,

Josh

josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Re: Using message box to write script to image

Post by josh@ACP » Thu Feb 19, 2015 9:58 pm

Can someone help me with the original post? I still can't figure the quotes out. I tried the behavior as suggested by Klaus but it is not ideal for what I need to do.

thanks!

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Using message box to write script to image

Post by SparkOut » Thu Feb 19, 2015 10:28 pm

I am really at a loss to see how setting a script in one message box line can simplify the programming. You obviously have something in mind, but could you elaborate to explain how you want to do this? I am feeling that a different approach may get you lots better results, much easier, and much easier to maintain, but I don't know enough about your ideas for the code adjustment to be fully confident of that. I might suggest you make use of behaviors too, or even making your script permanent, but generic so that instead of setting the filename of the sound to be played in the script, the script will play the filename resolved from a custom property, say "the cSoundFile of this stack".
Then instead of editing the code and trying to reset the script from the message box, all you have to do is the clean one-liner : set the cSoundFile of this stack to "noise.wave" and the same effect will be achieved. There are undoubtedly other suggestions that will crop up, given more understanding of your expectations.

josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Re: Using message box to write script to image

Post by josh@ACP » Thu Feb 19, 2015 11:22 pm

I have possibly 400 apps to build. Each app averages over 50+ objects (images, buttons) and over 50+ sounds. Each of these is distinct to the app and then each card within the app. The app i'm using as the template is actually on the google play store MTB - Amon Plonjon.

Going to each individual card and using the IDE is not ideal for me but livecode is what my company uses. I'm used to writing syntax for data analysis and can write mass amounts of script/syntax lines using excel in a very short amount of time. My plan was to use the multi-line feature of the message box in combination with an excel spreadsheet (mixed with file names, quotes, commands, etc and the concatenate function giving me what I need to put into the multi-line message box). Once I get the one thing I'm requesting in this post I can replicate it in a matter of minutes.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using message box to write script to image

Post by Simon » Thu Feb 19, 2015 11:57 pm

Hi josh@ACP,
One thing, audioClip is not supported in mobile.


Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Using message box to write script to image

Post by Simon » Fri Feb 20, 2015 12:08 am

Here is the modified script to use the message box

Code: Select all

 set the script of img 1 to "global gPath" & cr & \
"on mouseUp" & cr & \
"set the PlayLoudness to 100" & cr & \
"if the environment <> "&quote&"mobile"&quote&" then" & cr & \
"put "&quote&"aq1ctest.wav"&quote&" into tSndPath" & cr & \
"play tSndPath" & cr & \
"else " & cr & \
"put specialFolderPath("&quote&"engine"&quote&") into gPath" & cr & \
"play audioClip (gPath &"&quote&"/Audio/aq1ctest.wav"&quote&")" & cr & \
"end if" & cr & \
"end mouseUp" 
Simon
Edit; oops change the name of the image :)
Edit 2; You have curly quotes in your script, no good. “global gPath” vs "global gPath"
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Using message box to write script to image

Post by bn » Fri Feb 20, 2015 12:13 am

Hi Josh,
Here is your script:

Code: Select all

on mouseUp
   put "global gPath" & cr & "on mouseUp" & cr & "set the playLoudness to 100" & cr & "if the environment <> " & quote & "mobile" &  quote && "then" & cr & "put" & quote && "aq1ctest.wav" && quote & "into tSndPath" & cr & "else" & cr & "put specialFolderPath(" & quote & "engine" & quote & ")" && "into gPath" & cr & "play audioClip (gPath" & quote & "/Audio/aq1ctest.wav" & quote & ")" & cr & "end if" & cr & "end mouseUp"  into tScript
   set the script of button 2  to tScript
end mouseUp
there was a multitude of missing quotes and so on in it.

And there were some curly quotes when I copied from the forum, I don't know where they came from. Just watch out for them it is hard to debug because they are often hard to see in case they come from your data source.

Kind regards

Bernd

Edit: Simon was faster :)

josh@ACP
Posts: 6
Joined: Wed Jan 28, 2015 3:26 pm

Re: Using message box to write script to image

Post by josh@ACP » Fri Feb 20, 2015 6:15 pm

You all are awesome. thank you!!!!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Using message box to write script to image

Post by Klaus » Sat Feb 21, 2015 6:20 pm

Hi Josh,

one of the many other possible approaches is this:
1. Create this handler in your stack script:

Code: Select all

## Not sure if gPath needs to be global or is needed at all, if you only use it in this handler!?
global gPath
on play_sound tSndPath
   set the PlayLoudness to 100
   if the environment <> "mobile" then
      play tSndPath
   else 
      put specialFolderPath("engine") into gPath
      play (gPath &"/Audio/" & tSndPath)
   end if
end play_sound
Then you can do something like this in the message box, which then will only need to pass the wav filename as a parameter:

Code: Select all

set the script of img "xyz" to ("on mouseup;play_sound" && QUOTE & "your_wave_file_here.wav" & QUOTE & ";end mouseup")
Less typing and less "missing quotes error" prone :D

Hint: A semicolon ; is a valid "line delimiter" replacement in scripts!


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Using message box to write script to image

Post by jacque » Sun Feb 22, 2015 8:38 pm

I had to do exactly the same thing with a very large project. We rebuild hundreds of stacks, sometimes more than once a day. It is much easier to do this task from within LC, as Klaus says. What I'd do here is put the same generic script into every button. It would be a permanent script so that you don't need to ever touch it again. Instead of using a hard-coded sound file name, it should retrieve a card property whose value is the name of the sound. Then when you need to update, you just need to run an automated script that sets the value of that custom card property.

So your permanent button script would be similar to the one Klaus posted, but without the parameter. In the handler itself, use this line to play a sound:

Code: Select all

play (gPath &"/Audio/" & the cSndName of this cd)
You will never need to touch the script again, which means no copy/pasting or messing with the message box or the script editor. Now in your database, you generate a tab-deimited text file. Each line of the file contains the long name of the card, a tab, and the name of the sound file asscoiated with that card.

Now create a tools stack that will do the update. A button script reads in the text file, repeats through all the lines, and for each line it goes to the correct card (the first tabbed item) and sets the custom property "cSndName" to the sound name in the text file (the second tabbed item.)

We do this all the time, and updating hundreds of stacks takes under a minute. Once it is set up, all you need to do is generate a new text file and click a button in your tools stack.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply