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)
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
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, … … 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
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)
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)
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 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
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
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).
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
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
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%)
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)
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”
Model: MacBook Pro Intel
AppleScript: 2.0
Browser: Safari 523.15
Operating System: Mac OS X (10.5)