Pages

6 juil. 2008

DataBase (5/5)

on read_file(thePath)
try
set thePath to thePath as string
set idFile to open for access file thePath
set fileData to read idFile as list
close access idFile
on error errMsg number errNum
close access file thePath
return false
end try
return fileData
end read_file

on write_file(theText, thePath)
try
set thePath to thePath as string
set idFile to open for access file thePath with write permission
write theText to idFile starting at 0 as list
close access id_file
return true
on error
close access file thePath
return false
end try
end write_file

-- Check the entry in a dialog box who must not be empty
on checkEmptyDialog(theText, defaultAnswer)
repeat
set theAnswer to text returned of (display dialog theText default answer defaultAnswer with icon 1)
if (count theAnswer) > 0 then exit repeat
end repeat
return theAnswer
end checkEmptyDialog

-- Return the number of one item in a list
on ItemNumber(theItem, theList)
repeat with NumItem from 1 to count theList
if theItem = item NumItem of theList then return NumItem
end repeat
return 0
end ItemNumber

on ASTID(x) -- set  item delimiter to x
set AppleScript's text item delimiters to x
end ASTID

>>Ouvrir le script dans l'éditeur

DataBase (4/5)

on createCard(a, b, c, d, e)
script
property item1 : a -- Auteur
property item2 : b -- Titre
property item3 : c -- Sujet
property item4 : d -- Edition
property item5 : e -- critere tri
on getInfos()
if item5 is "Auteur" then
return item1 & " • " & item2 & " • " & item3 & " • " & item4
else if item5 is "Titre" then
return item2 & " • " & item1 & " • " & item3 & " • " & item4
else if item5 is "Sujet" then
return item3 & " • " & item1 & " • " & item2 & " • " & item4
else
return item4 & " • " & item1 & " • " & item2 & " • " & item3
end if
end getInfos
on getInfos2()
if item5 is "Auteur" then
return item1
else if item5 is "Titre" then
return item2
else if item5 is "Sujet" then
return item3
else
return item4
end if
end getInfos2
end script
end createCard

>>Ouvrir le script dans l'éditeur

DataBase (3/5)

on newCard()
set item1 to text returned of (display dialog "Auteur :" default answer "" with icon 1)
set item2 to text returned of (display dialog "Titre :" default answer "" with icon 1)
set item3 to text returned of (display dialog "Sujet :" default answer "" with icon 1)
set item4 to text returned of (display dialog "Edition :" default answer "" with icon 1)
set item5 to "Auteur" -- tri par defaut
set the end of allCards to createCard(item1, item2, item3, item4, item5)
end newCard

>>Ouvrir le script dans l'éditeur

DataBase (2/5)

on CardList()
if allCards = {} then
display dialog "DataBase empty." with icon 0
return {}
end if
set btn to button returned of (display dialog "DataBase" buttons {"Search...", "Show cards"} default button 2 with icon 1)
set SortBy to choose from list {"Auteur", "Titre", "Sujet", "Edition"} with prompt "Sort by :" cancel button name "Retour"
if SortBy is false then return
set SortBy to item 1 of SortBy
set listeInfos to {}
if btn is "Show cards" then
-- afficher les cartes
repeat with theCard in allCards
set (item5 of theCard) to SortBy
copy (getInfos() of theCard) to the end of listeInfos
end repeat
set cardToDelete to (choose from list listeInfos with prompt "DataBase" OK button name "Delete" cancel button name "Back")
-- effacer une carte
if cardToDelete is not false then
set cardToDelete to item 1 of cardToDelete
ASTID(" • ")
set newCardList to {}
repeat with theCard in allCards
if (text item 1 of cardToDelete item1 of theCard) ¬
or (text item 2 of cardToDelete item2 of theCard) ¬
or (text item 3 of cardToDelete item3 of theCard) ¬
or (text item 4 of cardToDelete item4 of theCard) ¬
then
copy theCard to the end of newCardList
end if
end repeat
ASTID("")
set allCards to newCardList
end if
else -- chercher une carte
set stringToSearch to checkEmptyDialog("Search " & SortBy & " :", "")
repeat with theCard in allCards
set (item5 of theCard) to SortBy
if stringToSearch is in (getInfos2() of theCard) then
copy (getInfos() of theCard) to the end of listeInfos
end if
end repeat
if listeInfos is {} then
display dialog myQuote & stringToSearch & myQuote & "is not found." buttons {"•"} default button 1 with icon 0
else
choose from list listeInfos with prompt "Result :"
end if
end if
end CardList


>>Ouvrir le script dans l'éditeur

DataBase (1/5)

property allCards : {} -- liste des scripts objets  
property myQuote : ASCII character (34)
on run
set dataFilePath to ((path to me) as string) & "Contents:Resources:data.txt" -- fichier data
if allCards is {} then
set theData to read_file(dataFilePath as alias)
if theData is in {false, "list"} then
set allCards to {} -- fichier data.txt vide
else
set allCards to theData -- chargement scripts objets
end if
end if
repeat
set btn to button returned of (display dialog "DataBase" buttons {"Quit", "New card...", "All cards"} default button 3 with icon 1)
if btn = "Quit" then
write_file(allCards, dataFilePath as alias)
exit repeat
else
if btn = "New card..." then
newCard()
else if btn = "All cards" then
CardList()
end if
write_file(allCards, dataFilePath as alias)
end if
end repeat
end run

>>Ouvrir le script dans l'éditeur
Exemple de base de données qui gère une bibliothèque illustrant la methode objet. Le script doit être enregistré au format "Application Bundle" pour contenir un fichier texte "data.txt" stockant les données. Le fichier "data.txt" est accessible par ctrl-clic ou clic droit sur le script puis menu contextuel "Show Package Contents".

1/5) Le script principal on run/end run gère l'interface de saisie des fiches de la bases de données. La base de données est chargée au lancement du script dans la variable "theData". Chaque fiche est une instance du script objet contenu dans le handler "createCard". Les fiches sont stockées dans la variable "allCards" qui est une liste des instances du script objet. Les differents items de chaque fiche (auteur, titre, sujet, édition) sont stockés dans les propriétés (property) de chaque instance du script objet.

2/5) Le handler CardList gère l'affichage des fiches, l'effacement d'une fiche et la recherche d'une fiche dans la base de données.

3/5) Le handler newCard crée une nouvelle fiche.

4/5) Le handler createCard contient le script objet proprement dit. Les fiches sont des instances de ce script objet qui sert de modèle. Les differents items de chaque fiche (auteur, titre, sujet, édition) sont stockés dans les propriétés de l'instance du script objet. Les deux handlers "getInfos" et "getInfos2" servent respectivement à l'affichage des fiches et à la recherche d'une fiche.

5/5) Les handlers read_file, write_file, checkEmptyDialog, ItemNumber et ASTID gèrent "les affaires courantes" comme dans un script classique, à savoir : Lecture du contenu d'un fichier, écriture dans un fichier, test de saisie d'une boite de dialogue, numéro d'item dans une liste, fixation du délimitateur d'item dans un script.