PolyMac
LSI AppleScript
français | english
Navigation
Home
Sitemap
This wiki
This page

display dialog "aujourdhui: " & (current date) as string

 

set turtle to (load script file (CurrentFolder() & "turtleLib.scpt"))

set graphlib to (load script file (CurrentFolder() & "graphLib.scpt"))

set textLib to (load script file (CurrentFolder() & "textLib.scpt"))

set mathlib to (load script file (CurrentFolder() & "mathLib.scpt"))

 

-----------------------------------------------

 

set modele to "Titre=\"SpiraleRectangle2x\"

x0=0

y0=0

angle=0

delta=90

step=12

Axiom=\"[S]++[S]\"

S=\"F-[S]\"

n=32"

 

editerFormules("LS-data:", modele)

 

-----------------------------------------------

 

set n to lireTable("n")

set code to generation(n) -- generates n-th turtle code from tableDeSymboles starting with Axiom

 

-----------------------------------------------

 

set titre to lireTable("Titre")

tell turtle to create(titre)

tell graphlib to paintText(my regles(), {-100, 40}, 40, {0, 0, 0})

 

-----------------------------------------------

 

set x0 to lireTable("x0")

set y0 to lireTable("y0")

set angle to lireTable("angle")

set penColor to lireTable("penColor")

-- valeur par défaut de penWidth = 10 (pixels)

tell turtle to start(x0, y0, angle, penWidth, penColor)

 

-----------------------------------------------

 

set step to lireTable("step")

set delta to lireTable("delta")

turtlePoints(code) -- creates wayPoints

-----------------------------------------------

 

set biseau to lireTable("biseau")

tell turtle to draw() -- draws wayPoints

 

-------------------------------------------------------------------------------

 

on Branch() -- routine exécutée au début de chaque embranchement []

global turtle, x, y, xp, yp, alpha, c, s, d, step, delta, penIsDown, penWidth, penColor

set {step, penWidth} to {step * 0.9, penWidth * 0.7 + 0.5}

tell turtle to set penColor to changeColor()

end Branch

 

 

on turtlePoints(code)

global turtle, x, y, alpha, c, s, step, delta, step, penIsDown, penWidth, penColor, biseau, currentway, wayPoints -- état de la tortue (alpha est redondant)

-- result is in turtle's wayPoints

if code = "" then return

tell turtle

repeat with i from 1 to count code

set symbol to character i of code

----if symbol = "F" then ---  symbol = "F" etc.

if (offset of symbol in " A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ") > 0 then ---  symbol = "F" etc.

forward(step)

else if symbol = "+" then

turn(-delta)

else if symbol = "-" then

turn(delta)

else if symbol = "[" then

push()

my Branch()

start(x, y, alpha, penWidth, penColor)

else if symbol = "]" then

pop()

end if

end repeat

end tell

end turtlePoints

 

on generation(n)

if n = 0 then return "F"

set code to lireTable("Axiom")

repeat n - 1 times

set code to substitution(code)

end repeat

return code as text

end generation

 

on substitution(chaine) -- utilise la tableDeSymboles

if chaine = "" then return ""

set code to {}

repeat with i from 1 to count chaine

set symbol to character i of chaine

if (offset of symbol in " + -  [ ] F ") > 0 then

copy symbol to end of code

else

copy lireTable(symbol) to end of code

end if

end repeat

set AppleScript's text item delimiters to {""}

return code as text

end substitution

-------------------------------------------------------------------------------

on lireTable(symbole)

global TabledeSymboles

repeat with i from 1 to count TabledeSymboles

set paire to (item i of TabledeSymboles)

if symbole = first item of paire then return unquote(second item of paire)

end repeat

display dialog "donnez une valeur au symbole " & symbole default answer ""

set valeur to (text returned of result) --as number

copy {symbole, valeur} to end of TabledeSymboles

return valeur

end lireTable

 

on unquote(symbole)

global textLib

if class of symbole is string then tell textLib to return subst(symbole, "\"", "")

return symbole

end unquote

 

on regles()

global TabledeSymboles

set res to ""

repeat with i from 1 to count TabledeSymboles

set {mg, md} to item i of TabledeSymboles

if (mg = "Axiom") or (mg = "delta") or (mg = "biseau") or (length of mg) = 1 then ¬

set res to res & return & mg & " = " & unquote(md)

end repeat

end regles

 

on editerFormules(titre, modele)

-- initialise tabledeSymboles (une pairlist) avec les règles et paramètres édités

-- les données sont formées de plusieurs lignes avec chacune une initialisation ou une règle

-- le format des initialisations de paramètre et des règles LS est une affectation: "symbole = expression"

global mathlib, textLib, TabledeSymboles

 

 

-- initialiser les paramètres par défaut, ils peuvent être modifiés ensuite par evaluer(expression)

set TabledeSymboles to {{"Titre", "sansTitre"}, {"x0", 0}, {"y0", 0}, {"angle", 0}, {"delta", 90}, ¬

{"step", 1}, {"penwidth", 10}, {"penColor", {50, 200, 50}}, {"biseau", 0}, {"n", 1}}

tell textLib

set textRef to showText(titre, modele)

activate

-- temps pour éditer

repeat

set answer to display dialog titre buttons {"choisir.txt", "quitter", "vérifier/tracer"} default button "vérifier/tracer"

activate

 

if button returned of answer is "vérifier/tracer" then

try

set modele to getText(textRef)

repeat with ligne in paragraphs of modele

tell mathlib to set affectation to syntaxe(ligne) -- liste des arbres syntaxiques

if affectation = {} then -- skip

else if first item of affectation = "=" then

tell mathlib to eval(affectation) -- met x0, y0, angle, delta, step etc. dans la tableDeSymboles

else

error

end if

end repeat

return modele

on error mess

display dialog mess & linefeed & linefeed & "Corrigez la ligne:  " & ligne

end try

activate

 

else if button returned of answer is "choisir.txt" then

tell application "TextEdit" to close textRef

set textRef to chooseText()

set titre to name of textRef

setTextAttributes(textRef, 0)

activate

 

else if button returned of answer is "quitter" then

return ""

end if

end repeat

end tell

end editerFormules

 

 

 

-----------------------------------------------------------------------------------------------

 

-- utilitaire pour chercher le dossier des lib (le même que ce script)

on CurrentFolder()

tell application "Finder" to set sel to (selection as string)

set AppleScript's text item delimiters to {":"}

return ((items 1 through -2 of (text items of sel)) as text) & ":"

end CurrentFolder

 
Search
Share