Re: how?
Re: how?
- Subject: Re: how?
- From: Adam Morris <email@hidden>
- Date: Tue, 12 Jan 2010 06:52:16 -0500
FYI this is a programmer's way of writing code that's partially a comment. Take a look at the repeat statement:
repeat until ValidInput = true
That means: "I'm going to keep asking the user for valid input." More experience in scripts will show you that you use this trick often: "Repeat forever until I get valid input."
Usually you'll just see "repeat until ValidInput"
It's the return statement that ends the otherwise endless loop.
-brainysmurf aka Adam
On Sun, Jan 10, 2010 at 4:17 PM,
<email@hidden> wrote:
Send AppleScript-Users mailing list submissions to
email@hidden
To subscribe or unsubscribe via the World Wide Web, visit
http://lists.apple.com/mailman/listinfo/applescript-users
or, via email, send a message with subject or body 'help' to
email@hidden
You can reach the person managing the list at
email@hidden
When replying, please edit your Subject line so it is more specific
than "Re: Contents of AppleScript-Users digest..."
Today's Topics:
1. how? (Jack Schwart)
2. RE: Snow Leopard osax security and 'run script' with
parameters (Scott Babcock)
3. Re: how? (Yvan KOENIG)
----------------------------------------------------------------------
Message: 1
Date: Sun, 10 Jan 2010 15:55:09 -0500
From: Jack Schwart <email@hidden>
Subject: how?
To: email@hidden
Message-ID: <email@hidden">email@hidden>
Content-Type: text/plain; charset="iso-8859-1"
Hi, I am trying to teach myself applescript. I went through Jerry Lee Ford, Jr.'s book, Applescript for the Absolute Beginner, and understood everything except this following code block. Can any one help me figure out how the variable "ValidInput" switches to "true" and ends the loop?
Thanks, Jack
--******** GetPlayerInput handler ******************
--This handler prompts the player to tell the game the range of
--numbers from which lottery ticket numbers should be selected
on GetPlayerInput()
--This variable is used to control loop execution
set ValidInput to false
repeat until ValidInput = true -- loop until valid input is collected
--Question,:how does ValidInput switch to true???
--Prompt the player to specify the range of numbers to be used
set NoRange to text returned of (display dialog ¬
"What is the highest number that can be selected when " & ¬
"creating a lottery ticket?" default answer ¬
"44" buttons {"OK"} with title GameTitle)
--The range must be at least 3 and no larger than 59
if (NoRange > 2) and (NoRange < 60) then
set NoRange to NoRange as integer --Convert the player input
--Question: Why do I need this step???? A: see page 101 "Coercion"
return NoRange -- Return the player's input
else -- Display an error message if the input is not valid
display dialog "Error: You must enter an integer value " & ¬
"between 3 and 59" with title GameTitle
end if
end repeat
end GetPlayerInput
=
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.apple.com/mailman/private/applescript-users/attachments/20100110/b40014c2/attachment.html
------------------------------
Message: 2
Date: Sun, 10 Jan 2010 21:13:54 +0000
From: Scott Babcock <email@hidden>
Subject: RE: Snow Leopard osax security and 'run script' with
parameters
To: "email@hidden"
<email@hidden>
Message-ID:
<email@hidden">email@hidden>
Content-Type: text/plain; charset="us-ascii"
For me, the only reason I need this sort of trick is to work around the fact that AppleScript doesn't allow you to access record properties by their names. If you receive an arbitrary record, the only way that I know of to pull it apart is to convert it to a string to get the property names (thru a different sort of trickery) and assemble script objects on the fly to obtain the values.
-----Original Message-----
Date: Sat, 9 Jan 2010 14:01:40 +0000
From: has <email@hidden>
Subject: Re: Snow Leopard osax security and 'run script' with
parameters
To: email@hidden
Message-ID: <email@hidden">email@hidden>
Content-Type: text/plain; charset=us-ascii
Stockly, Ed wrote:
>> To be fair, there are a few situations where dynamic code generation in AS
>> _is_ the best (or only) way to go.
>
> Many of the do shell script solutions ...
Many 'do shell script' solutions involve dynamic code generation, yes. As soon as you build up the shell script string by concatenating it with paths strings, etc, you have to consider what could happen if the shell script is wrongly formed. That's why it is so very important to sanitise all of your inputs (hint: 'quoted form of some_text' is your friend); forget, and anything could happen from an unexpected error to a hosed system when that script runs.
> ... and solutions using script objects posted on this list are, technically, examples of dynamic code generation.
No, not unless you're fabbing the source code for those objects on the fly, whether using 'run script "script ... end script"' as shown earlier, or using the command-line 'osacompile' tool, or even by scripting Script Editor itself. Once again, the same serious warnings about sanitising your inputs apply.
Using script objects as libraries or in object-oriented programming does not constitute dynamic code generation, however, so just the usual concerns about running any piece of pre-written code apply there.
Regards,
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
------------------------------
_______________________________________________
AppleScript-Users mailing list
email@hidden
http://lists.apple.com/mailman/listinfo/applescript-users
End of AppleScript-Users Digest, Vol 7, Issue 18
************************************************
------------------------------
Message: 3
Date: Sun, 10 Jan 2010 22:15:34 +0100
From: Yvan KOENIG <email@hidden>
Subject: Re: how?
To: liste AppleScript US <email@hidden>
Message-ID: <email@hidden">email@hidden>
Content-Type: text/plain; charset="utf-8"
Le 10 janv. 2010 à 21:55, Jack Schwart a écrit :
> Hi, I am trying to teach myself applescript. I went through Jerry
> Lee Ford, Jr.'s book, Applescript for the Absolute Beginner, and
> understood everything except this following code block. Can any
> one help me figure out how the variable "ValidInput" switches to
> "true" and ends the loop?
>
> Thanks, Jack
>
> --******** GetPlayerInput handler ******************
>
> --This handler prompts the player to tell the game the range of
> --numbers from which lottery ticket numbers should be selected
> on GetPlayerInput()
>
> --This variable is used to control loop execution
> set ValidInput to false
>
> repeat until ValidInput = true -- loop until valid input is
> collected
>
> --Question,:how does ValidInput switch to true???
>
> --Prompt the player to specify the range of numbers to be used
> set NoRange to text returned of (display dialog ¬
> "What is the highest number that can be selected when "
> & ¬
> "creating a lottery ticket?" default answer ¬
> "44" buttons {"OK"} with title GameTitle)
> --The range must be at least 3 and no larger than 59
> if (NoRange > 2) and (NoRange < 60) then
> set NoRange to NoRange as integer --Convert the player
> input
> --Question: Why do I need this step???? A: see page
> 101 "Coercion"
> return NoRange -- Return the player's input
> else -- Display an error message if the input is not valid
> display dialog "Error: You must enter an integer value "
> & ¬
> "between 3 and 59" with title GameTitle
> end if
>
> end repeat
>
> end GetPlayerInput
In fact the variable ValidInput is not changed.
It's absolutely useless in the handler.
The loop is exited when the instruction
return NoRange is executed.
The handler may be edited as:
on getplayerinput()
repeat -- loop until valid input is collected
--Prompt the player to specify the range of numbers to be used
set NoRange to text returned of (display dialog ¬
"What is the highest number that can be selected when " & ¬
"creating a lottery ticket?" default answer ¬
"44" buttons {"OK"} with title GameTitle)
--The range must be at least 3 and no larger than 59
if (NoRange > 2) and (NoRange < 60) then
set NoRange to NoRange as integer --Convert the player input
--Question: Why do I need this step???? A: see page 101 "Coercion"
return NoRange -- Return the player's input
else -- Display an error message if the input is not valid
display dialog "Error: You must enter an integer value " & ¬
"between 3 and 59" with title GameTitle
end if
end repeat
end getplayerinput
or :
on getplayerinput()
repeat -- loop until valid input is collected
--Prompt the player to specify the range of numbers to be used
set NoRange to text returned of (display dialog ¬
"What is the highest number that can be selected when " & ¬
"creating a lottery ticket?" default answer ¬
"44" buttons {"OK"} with title GameTitle)
--The range must be at least 3 and no larger than 59
if (NoRange > 2) and (NoRange < 60) then
set NoRange to NoRange as integer --Convert the player input
--Question: Why do I need this step???? A: see page 101 "Coercion"
exit repeat
else -- Display an error message if the input is not valid
display dialog "Error: You must enter an integer value " & ¬
"between 3 and 59" with title GameTitle
end if
end repeat
return NoRange -- Return the player's input
end getplayerinput
In this late version, the 'exit repeat' is explicit.
It was implicit in the original code and in the 1st edited version.
Yvan KOENIG (VALLAURIS, France) dimanche 10 janvier 2010 22:14:54
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.apple.com/mailman/private/applescript-users/attachments/20100110/8700c9ee/attachment.html
------------------------------
_______________________________________________
AppleScript-Users mailing list
email@hidden
http://lists.apple.com/mailman/listinfo/applescript-users
End of AppleScript-Users Digest, Vol 7, Issue 19
************************************************
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
- Follow-Ups:
- Re: how?
- From: Chris Page <email@hidden>