Using items chosen in a list

Sorry I hadn’t seen the part on write-to-disk at the end.
It works perfectly.

Thanks again

Stefan

(* 1
Is there a way to have a list (choose from list) with more than two buttons ?
I would like to have a Cancel, a Modify and a OK button in my "ListeAmi"s in order to be able to go into a routine where I can add a new member (It’s done thanks to you) but also to remove one.

(*2
Do you see also a wat to sort alphabetically the fiste line of the text file (the names) and to have the eMails on the second line to follow that reordering ??

But that’s an other story.

Thank you for your help, my script is almost perfect now, I wonder if a version for Entourage 2008 would work also ?

Regards

Jean-Marie

Model: MacBook Pro Intel
AppleScript: 2.0
Browser: Safari 523.15
Operating System: Mac OS X (10.5)

No, but use a additional item (like “Autre”) instead of the button

there is a sorting subroutine, which sorts two lists simultaneously.
Here the complete script with the improvements


(* 
Script destiné à Safari et qui permet d'envoyer une référence à une URL à un ami choisi dans une liste pré-établie.
Ce script doit être installé dans users/yourself/Library/Scripts/Applications/Safari
© jmcalvat 2008-03 and StefanK of MacScripter BBS
*)
---------------------A-----------------
-- Variables globales (variables conservant leur valeur d'une routine à l'autre)
global laPage
global monTexte
global ListeAmis
global ListeMails
global leChoix
global monDestinataire
global pieceJointe
---------------------B-----------------
-- Mémorise dans la variable "laPage" l'URL de la page courante de Safari
tell application "Safari" to set laPage to URL of document 1
---------------------C-----------------
-- Texte par défaut du message à envoyer (devant apparaître plus loin)
set monTexte to "Ce lien va surement t'intéresser"
---------------------D-----------------
-- Construction des listes pour le choix du destinataire et du mail 
-- Liste à modifier dans le fichier "Amis.txt", qui doit rester impérativement à cet endroit !
-- Si à un autre endroit, modifier la ligne suivante
-- Première ligne : les noms séparés par tab
-- Deuxième ligne les mails séparés par tab
set textFile to ((path to desktop as text) & "MyScripts:~Originaux:Amis.txt")
set theText to read file textFile
set TID to text item delimiters -- sauvegarde le format courant des délimiteurs de texte dans la variable "TID"
set text item delimiters to tab -- met les délimiteurs de texte au format "tab", une tabulation sépare chaque item de texte
set ListeAmis to text items of paragraph 1 of theText
set ListeMails to text items of paragraph 2 of theText
set text item delimiters to TID -- rétablit le format par défaut des délimiteurs de texte
---------------------E-----------------
-- Affichage de la liste des amis pour choix
set monTitre to "Choix du destinataire :" -- le titre de la fenêtre de dialogue
set monDefault to item 1 of ListeAmis -- détermine le premier membre de la liste comme choisi par défaut
set leChoix to choose from list (ListeAmis & "Autre") with prompt monTitre default items monDefault --affiche le dialogue
if leChoix is false then -- on a appuyé sur le bouton Annuler ...
	beep
	display dialog "Vous avez annulé." buttons {"OK"}
	return -- ... et tout s'arrête
end if
---------------------F-----------------
-- Choisit l'email à utiliser en fonction du destinataire choisi dans la liste 
-- (utilise le même rang dans chaque liste, donc très important que les deux listes soient synchrones dans le fichier "Amis.txt" !)

-- Un nom et un email non présent dans la liste
if item 1 of leChoix = "Autre" then
	set maReponse to display dialog "Nouveau destinataire ?" & return & "(Inscrire le nom du destinataire ...)" default answer "JOHN DOE"
	set leChoix to text returned of maReponse
	set maReponse to display dialog "Nouvelle adresse email pour " & leChoix & " ?" & return & "(Inscrire sa nouvelle adresse ...)" default answer monDestinataire
	set monDestinataire to text returned of maReponse
	
	---------------------G-----------------
	-- Mise à jour de la liste type
	set message1 to "Modification de la liste d'amis"
	set message2 to "Voulez-vous conserver les coordonnées de " & leChoix & " ?" -- défini le texte du dialogue d'alerte
	set tempVar to display alert message1 message message2 buttons {"Non", "Oui"} default button 1 -- 3 bouttons maxi autorisés
	set leBouton to button returned of tempVar -- le bouton qui a été pressé
	if leBouton is "Oui" then -- seulement si le bouton est Oui mémorise les coordonnées
		set end of ListeAmis to leChoix -- met "Autre" en dernier
		set end of ListeMails to monDestinataire
		sort_items(ListeAmis, ListeMails)
		set {TID, text item delimiters} to {text item delimiters, tab}
		set ListeAmis to (ListeAmis as text) & return
		set ListeMails to ListeMails as text
		set text item delimiters to TID
		
		write_to_disk from ListeAmis & ListeMails into textFile without append
		if result then
			beep 3
		else
			say "error" -- or do some other error handling
		end if
	end if
else
	repeat with i from 1 to count of ListeAmis -- compte le nombre d'amis et repète autant de fois
		if item 1 of leChoix is item i of ListeAmis then
			set monDestinataire to item i of ListeMails
			exit repeat
		end if
	end repeat
	-- go on doing something
	
end if
--
my courrier() -- Envoit à la routine d'expédition du mail

on write_to_disk from theData into theFile given append:append
	try
		set ff to open for access file theFile with write permission
		if append is false then set eof of ff to 0
		write theData to ff
		close access ff
		return true
	on error
		try
			close access file theFile
		end try
		return false
	end try
end write_to_disk


to sort_items(sortList, SecondList)
	script L
		property srt : sortList
		property sec : SecondList
	end script
	tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
		set s to (L's srt)'s item i
		set r to (L's sec)'s item i
		
		repeat with i from (i + 1) to it
			tell (L's srt)'s item i to if s > it then
				set (L's srt)'s item (i - 1) to it
				set (L's sec)'s item (i - 1) to (L's sec)'s item i
			else
				set (L's srt)'s item (i - 1) to s
				set (L's sec)'s item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > (L's srt)'s end then
			set (L's srt)'s item it to s
			set (L's sec)'s item it to r
		end if
	end repeat
end sort_items

Stefan

to sort_items(sortList, SecondList)
   script L
       property srt : sortList
       property sec : SecondList
   end script
   tell (count L's srt) to repeat with i from (it - 1) to 1 by -1
       set s to (L's srt)'s item i
       set r to (L's sec)'s item i
       
       repeat with i from (i + 1) to it
           tell (L's srt)'s item i to if s > it then
               set (L's srt)'s item (i - 1) to it
               set (L's sec)'s item (i - 1) to (L's sec)'s item i
           else
               set (L's srt)'s item (i - 1) to s
               set (L's sec)'s item (i - 1) to r
               exit repeat
           end if
       end repeat
       if it is i and s > (L's srt)'s end then
           set (L's srt)'s item it to s
           set (L's sec)'s item it to r
       end if
   end repeat
end sort_items

I get that message (translated) :

Syntax Error
sortList is an illegal parameter !

Then I replaced ‘sortList’ by 'FirstLis’t and it is accepted!

Jean-Marie

Model: MacBook Pro Intel
AppleScript: 2.0
Browser: Safari 523.15
Operating System: Mac OS X (10.5)

Stefan

Last but not least :
Is it possible to “say” in French ?

if result then
				say " the list has been successfully modified " -- ou retour à la case départ
				display alert "Maintenance de la liste" message "La liste a bien été modifiée" & return & return & "Recommencez ..." buttons "OK"
				my start() -- permet de revenir au départ
			else
				beep 3
				say "error" -- ou une autre routine d'erreur
				my start() -- permet de revenir au départ
			end if

I have rather say " La liste a bien été modifiée" directly in French, for the fun of it !
I wonder if this is possible ?

Are you interested in getting my whole script completed ? Is there an email where I can send it to you ?

Thank you for your help through that script.
A question : bist du Deutsch ?

Regards

Jean-Marie

Model: MacBook Pro Intel
AppleScript: 2.0
Browser: Safari 523.15
Operating System: Mac OS X (10.5)

Yes, but you need a French voice.
Cepstral has at least French Canadian ones :wink:

This is very nice, but I think I won’t use it

you’re welcome :slight_smile:

Ja, aber ich lebe in der Schweiz