Using items chosen in a list

Hello, I am new to Apple scripting, and I enjoy very much your BBS.
I have some of you may think as a very simple problem, but I cannot come around.

set myList to {“Apple”, “Linux”, “Windows”}
set myDefault to item 1 of myList
set myPrompt to “Choose an OS”
set myChoice to choose from list myList with prompt myPrompt default items myDefault

if myChoice = {“Apple”} then
beep 1
else if myChoice = {“Linux”} then
beep 2
else if myChoice = {“Windows”} then
beep 3
else if myChoice is false then
beep 5
end if

In this applescript, I would like to refer to the results of the coice as items number, for instance : if myChoice is first item then … or if myChoice is item 3 of myList then … but this doesn’t work.

My objective is to be able to modify the list once and not to have to go to modifying every returned choice in the script.

Thank you for your help !

Sorry for my English :rolleyes:

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

Hi,

to retrieve the position of an item in a list, you can use this trick


set myList to {"1 Apple", "2 Linux", "3 Windows"}
set myDefault to item 1 of myList
set myPrompt to "Choose an OS"
set myChoice to choose from list myList with prompt myPrompt default items myDefault
if myChoice is false then beep 5
set myChoice to character 1 of item 1 of myChoice as integer
beep myChoice 

or you must use a repeat loop which compares the values

Thank you for your prompt answer (less than 3 mn, French time ! meaning no strike, no Sarkosy show or power failure !).

First how do you put your script in that nice box with the title OPEN THIS SCRIPLET IN YOUR EDITOR ?

Meanwhile I came up with this, since I do not like the idea of having the numbers (1, 2, 3 …) appearing in the dialog :


set A to “Apple”
set B to “Linux”
set C to “Windows”

set myList to {A, B, C}
set myDefault to item 1 of myList
set myPrompt to “Choose an OS”
set myChoice to choose from list myList with prompt myPrompt default items myDefault

if myChoice = {A} then
beep 1
else if myChoice = {B} then
beep 2
else if myChoice = {C} then
beep 3
else if myChoice is false then
beep 5
end if

I only have to change A, B, C then what if I have to add a fourth entry, I will have anyway to go down the script and add an Else If routine !!!
Not satisfying
:frowning:

use the Applescript tag above the message window.

Here is the example with the repeat loop


set myList to {"Apple", "Linux", "Windows"}

set myDefault to item 1 of myList
set myPrompt to "Choose an OS"
set myChoice to choose from list myList with prompt myPrompt default items myDefault
if myChoice is false then beep 5
repeat with i from 1 to count myList
	if item 1 of myChoice is item i of myList then exit repeat
end repeat

beep i

Thanks again StefanK, … :frowning: … BUT if I hit the Cancel button, the script carries on and then gives a message saying that “it is impossible to get item 1 of FALSE”. Is there a way of quitting the script after the Cancel ?

set A to “Apple”
set B to “Linux”
set C to “Windows”

set myList to {} & A
set myList to myList & B
set myList to myList & C

set myDefault to item 1 of myList
set myPrompt to “Choose an OS”
set myChoice to choose from list myList with prompt myPrompt default items myDefault
if myChoice is false then beep 5 – here something to prevent carrying through the rest ??

repeat with i from 1 to count of myList
if item 1 of myChoice is item i of myList then exit repeat
end repeat
beep i

if myChoice is false then return

aborts the script

Well, thanks to you, it works perfectly now :

set A to “Apple”
set B to “Linux”
set C to “Windows”

set myList to {} & A
set myList to myList & B
set myList to myList & C

set myDefault to item 1 of myList
set myPrompt to “Choose an OS”
set myChoice to choose from list myList with prompt myPrompt default items myDefault

if myChoice is false then
beep 5
return – aborts the script
end if

repeat with i from 1 to count of myList
if item 1 of myChoice is item i of myList then exit repeat
end repeat
beep i

I am now able to modify my list, add or delete entries, sort in alphabetical order, etc…
Of course the BEEP stuff was here for an example, I have pieces of script to send informations to friends in a list.
Thak you again for your availability and your expertise.

Jean-Marie (from Paris, France)

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

this


set myList to {} & A
set myList to myList & B
set myList to myList & C

is the same as


set myList to {A, B, C}

Hi again StefanK

Here is the “real” script I worked out thanks to your help. You know these guys, you give them a hand and they take the arm !! :lol:

I would like to put the name of my friends and their emails in a text file that I could read from the script instead of having them in the script itself - more flexible, no?.
It would be most convenient, if I want to add, delete or sort this list, but I want to keep the name and the email related (sync). I wonder if it is possible to read differently line 1 and line 2 of a same text file ?

Here is my script updated thanks to you (sorry for the french in the comments)

(* 
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 
*)
---------------------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"
	set laPage to do JavaScript "document.URL" in document 1
end tell
---------------------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 - attention de bien respecter les {"xxx","xxx"}
--- this is what I would like to read from a text file but I don't know how to keep the names and mails related within read commands
set A to "François"
set mailA to "@@@@wanadoo.fr" -- I masked the actual email for obvious reasons
set B to "Claude"
set mailB to "@@@@wanadoo.fr"
set C to "Luc"
set mailC to "l@@@@wanadoo.fr"
set D to "Tony"
set mailD to "@@@@hotmail.com"
--
set ListeAmis to {A, B, C, D} -- you were right, I save lines here
--
set ListeMails to {mailA, mailB, mailC, mailD}

--Affichage de la liste-----------------------------------------
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 with prompt monTitre default items monDefault --affiche le dialogue
if leChoix is false then -- on a appuyé sur le bouton Annuler ...
	beep 3
	return -- ... et tout s'arrête
end if
---------------------E-----------------
-- 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 !)
repeat with i from 1 to count of ListeAmis
	if item 1 of leChoix is item i of ListeAmis then
		set monDestinataire to item i of ListeMails
		exit repeat
	end if
end repeat
my courrier()
---------------------F-----------------
-- Routine pour envoyer le mail
on courrier()

etc…

So I would like to read my friends from a text file in line 1 to populate my list “ListeAmis” and their mails in line 2 to populate my list “monDestinataire”.
How can I read a specific line (1 or 2) in a text file ?

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

part B: the URL can be read directly without do javascript


-- 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

part D: assuming the text file is a plain text file and the entries are separated by tab (ASCII character 9)

-- Construction des listes pour le choix du destinataire et du mail -- Liste à modifier - attention de bien respecter les {"xxx","xxx"}
--- this is what I would like to read from a text file but I don't know how to keep the names and mails related within read commands

set textFile to ((path to desktop as text) & "textFile.txt")
set theText to read file textFile
set {TID, text item delimiters} to {text item delimiters, tab}
set {ListeAmis, ListeMails} to text items of paragraphs 1 thru 2 of theText
set text item delimiters to TID

Hi StefanK

---------------------D-----------------
-- Construction des listes pour le choix du destinataire et du mail -- Liste à modifier - attention de bien respecter les {"xxx","xxx"}
set textFile to ((path to desktop as text) & "Amis.txt")
set theText to read file textFile
set {TID, text item delimiters} to {text item delimiters, tab}
set {ListeAmis, ListeMails} to text items of paragraphs 1 thru 2 of theText
set text item delimiters to TID

--Affichage de la liste-----------------------------------------(si plus de 4 membres, il faudra rajouter un 'else if' plus loin qui est déjà prêt)
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 with prompt monTitre default items monDefault --affiche le dialogue
if leChoix is false then -- on a appuyé sur le bouton Annuler ...
	beep 3
	return -- ... et tout s'arrête
end if

It doesn’t work.

My text file “Amis.txt” is like that :

Francois Claude Luc Tony
@@@wanadoo.fr @@@wanadoo.fr @@@wanadoo.fr @@@hotmail.com

and what I get is not a list but a single entry text dialog with all my friends on the same line.

Should I put quote around each name : “Francois” “Claude” “Luc” “Tony” ??

Jean-Marie

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

this works, if the items are really separated with one tab character


set textFile to ((path to desktop as text) & "Amis.txt")
set theText to read file textFile
set {TID, text item delimiters} to {text item delimiters, tab}
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

Thank you for all, it works fine for me now.

Next steps I will write a routine to amend these lists (in order to pass the scripts to friends, so they can populate the lists with their own friends and mails).

It’s getting late here now, I have to dinner now … “bon appétit” :stuck_out_tongue:

Thanks again

Jean-Marie

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

Dear Stefan

After a night over our work, I still do not understant this part :

set {TID, text item delimiters} to {text item delimiters, tab}

For the rest of my mind, could you tell me what it does?

Thank you :smiley:

Jean-Marie

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

It’s the same as


set TID to text item delimiters -- save the current text item delimiter into a variable
set text item delimiters to tab
.

.
set text item delimiters to TID

just written in one line.
Using text item delimiters it’ s recommended to save the delimiter before changing it and restore it afterwards

For your information Stefan, this doesn’t work anymore if I modify the path and put my file in a folder “MyScripts” on the desktop :

set textFile to ((path to desktop as text) & "MyScripts:Amis.txt")

Then in “ListeMails”, I only get the 4 first letters of paragraph 2 !

It’s OK for me, I’ll keep my file “Amis.txt” on the desktop, but I remain intellectually puzzled ! :rolleyes:

Jean-Marie

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

This is rather impossible, your code and the path is correct.
You could get the result only, if the code looks like


set textFile to ((path to desktop as text) & "MyScripts:Amis.txt")
set theText to read file textFile
set {TID, text item delimiters} to {text item delimiters, tab}
set ListeAmis to text items of paragraph 1 of theText
set text item delimiters to "" -- !
set ListeMails to text items of paragraph 2 of theText
set text item delimiters to TID

Hello Stefan

I worked out a way of modifying my list of friends, but I come to a dend end when I have to write the new lists into my text file !?
Could you help ?

Here is my script (I took the privilege of putting down your name in the credits at the beginning for you took participation in that for more than 50%) :wink:

(* 
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 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" !)
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
-- 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 long to length of ListeAmis -- nombre d'items dans la liste
		set item long of ListeAmis to leChoix -- remplace le dernier item par leChoix (le nom saisi)
		set end of ListeAmis to "Autre" -- met "Autre" en dernier
		set item long of ListeMails to monDestinataire -- remplace le dernier item par monDestinataire (le mail saisi)
		set end of ListeMails to "xxx@wanadoo.fr" -- met un email bidon en dernier
		
		-- Here a routine to write in paragraph 1 of my text document the new list of friends : "ListeAmis"
		-- and in paragraph 2 the new liste of corresponding mails : "ListeMails"
		set textFile to ((path to desktop as text) & "MyScripts:~Originaux:Amis.txt")
		
		beep 3
	end if
end if
--
my courrier() -- Envoit à la routine d'expédition du mail

the “beep 3” is here to be sure it goes through the path.

My text file reads like that (the &&& are there for confidentiality purpose :
CLAUDE FRANCOIS LUC TONY Autre
&&&&&@wanadoo.fr &&&&&&@wanadoo.fr &&&&&&@wanadoo.fr &&&&&&@hotmail.com abcd@wanadoo.fr

The idea is to replace these two lines by the new lists created in the script; but they are in the form {“CLAUDE”, “FRANCOIS”, “LUC”, “TONY”, “Autre”}, so how do I change them in the form aaa tab bbb tab ccc …
And how can I replace a whole line, I keep on appending instead of replacing !!

Regards

Jean-Marie

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

you need a simple write to file routine


.
	---------------------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 long to length of ListeAmis -- nombre d'items dans la liste
		set item long of ListeAmis to leChoix -- remplace le dernier item par leChoix (le nom saisi)
		set end of ListeAmis to "Autre" -- met "Autre" en dernier
		set item long of ListeMails to monDestinataire -- remplace le dernier item par monDestinataire (le mail saisi)
		set end of ListeMails to "xxx@wanadoo.fr" -- met un email bidon en dernier
		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
		
		
		-- Here a routine to write in paragraph 1 of my text document the new list of friends : "ListeAmis"
		-- and in paragraph 2 the new liste of corresponding mails : "ListeMails"
		-- set textFile to ((path to desktop as text) & "MyScripts:~Originaux:Amis.txt")  -- textFile is already defined
		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
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

Stefan


	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

I get that error :

“script ne comprends pas le message write_to_disk”, which means “script doesn’t understand the message write_to_disk”
:frowning:

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