Getting first few characters of a string with applescript - applescript

I wish to keep only the first 13 caracters of a string in an applescript. I think I didn't understand how to use the "characters" function.
Can someone help me ?
choose from list {"0041325677667-pharmacie 1", "0041325677557-pharmacie 2",
"0041325677447-pharmacie 3", "0041325677337-pharmacie 4"} with prompt
"Thanks to select" without multiple selections allowed and empty selection allowed
return the result as string
return characters 1 thru 13 of result
Thanks in advance for your help

characters x thru y returns a list of characters, in your example item 1 returns
{"0", "0", "4" ,"1" ,"3" ,"2" ,"5" ,"6" ,"7" ,"7" ,"6" ,"6", "7"}
text x thru y returns a string as requested.
without multiple selections allowed and empty selection allowed is the default so it can be omitted. But you should consider the case when the user presses "Cancel", then the expression return boolean false.
set chosen to choose from list {"0041325677667-pharmacie 1", "0041325677557-pharmacie 2", "0041325677447-pharmacie 3", "0041325677337-pharmacie 4"} with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
return text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened

Related

Applescript using if then statements and choose from list

I'm working on a course selection script and each person should only be able to select three courses. I am having issues figuring out how to repeat choose from list if the user selects less than or more than three courses, and only proceed if three choices are selected. Converting the choices from the list to string should work however nothing happens after choosing from list
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
if words in theClassString ≠ 3 then
display dialog "Please select three courses"
exit repeat
else if words in theClassString = 3 then
display dialog "Ok"
end if
end repeat
It's a clever idea to count the words in the theClassString (which would be done using number of words in theClassString, instead of simply words in theClassString). This would work the majority of the time, until a user includes "I&S" as one of their options, which sadly counts as two words, "I" and "S", since the ampersand is not a word character.
You also had your exit repeat in the wrong half of the if...then...else block, since you want to break the loop when the user selects 3 courses, not when they fail to select 3 courses.
Rather than attempting to coerce the result of the list selection into a string, you ought to just count the number of items in the result, which can be done in one of three ways:
count theClass
length of theClass
number in theClass
Here's a reworked version of your script:
property theClassList : {"Math ", "English ", "Science ", "I&S ", "Design "}
property text item delimiters : linefeed
set choices to missing value
repeat
if choices ≠ missing value then display dialog ¬
"You must select precisely three courses"
set choices to choose from list theClassList with prompt ¬
"Select three courses" with multiple selections allowed
if choices = false or the number of choices = 3 then exit repeat
end repeat
if choices = false then return
display dialog choices as text
...And here's a version that uses a recursive handler instead of a repeat loop:
property theClassList : {"Math", "English", "Science", "I&S", "Design"}
property text item delimiters : linefeed
to choose()
tell (choose from list theClassList ¬
with prompt ("Select three courses") ¬
with multiple selections allowed) to ¬
if it = false or its length = 3 then ¬
return it
display dialog "You must select precisely three courses"
choose()
end choose
display alert (choose() as text)
to make it easier by respecting the fluidity of your script. It would be better to declare your "theClass" as a rather string list and declare n as count of list. Below is your modified script or the following that takes your own but declaring n to count of words in "theClassString".`
set theClassList to {"Math", "English", "Science", "I & S", "Design"}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as list
set n to count of theClassString
set n to do shell script "echo" & n
if n ≠ "3" then
display dialog "Please select three courses"
exit repeat
else if n = "3" then
display dialog "Ok"
end if
end repeat
Below
By declaring String
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
set n to count of words in theClassString
set n to do shell script "echo " & n
if n ≠ "3" then
display dialog "Please select three courses"
exit repeat
else if n = "3" then
display dialog "Ok"
end if
end repeat
um maybe like this
set theClassList to {"Math ", "English ", "Science ", "I&S ", "Design "}
repeat
set theClass to choose from list theClassList with prompt "Select three courses" with multiple selections allowed
set theClassString to theClass as string
set n to count of words in theClassString
set n to do shell script "echo " & n
if n ≠ "3" then
display dialog "Please select three courses" buttons {"ok"}
else if n = "3" then
display dialog "Ok"
exit repeat
end if
end repeat

How to update AppleScript List "Forever"

Look at the following code:
set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
set theResponse to the text returned of (display dialog "" default answer "" giving up after 5)
if TheStringsQ1Happy contains theResponse then
display dialog "That's Great!"
else
say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
set theResponseNotInVocabulary to text returned of (display dialog "" default answer "" giving up after 5)
if theResponseNotInVocabulary is "Yes" then
set end of TheStringsQ1Happy to theResponse
return TheStringsQ1Happy
end if
Although I can update TheStringsQ1Happy, this update only lasts the span of the script. How can I change the code so that every time I run the script, it also contains the updated vocabulary?
For example, if I said "All Good", the computer would recognize that the vocabulary is not on the list, and would later update this list only for that instance. How can I make it so that "All Good" stays for every instance from now on?
The following is strictly an example to help you with what you asked, not fix the broken code you posted.
If you run the following in Script Editor:
property theList : {1, 2, 3}
copy (count theList) + 1 to end of theList
log theList
You'll see theList as a property grow by 1 each time you run it, that is until the script is recompiled.
If you need absolute long term storage where nothing will be lost of anything added to theList, then you need to save to and retrieve from a disk file.
Variables in AppleScript don't span outside the duration of execution of the script in which they are defined, as you've quite rightly noticed.
However, a property can, and will continue into subsequent executions of a script with the information left over from the previous execution.
Bear in mind, though, that a property will get reset (restored to its original value) each time the script is re-compiled (which happens whenever you make edits to it, or trigger it to compile manually).
With this in mind, change this line:
set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
to this:
property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
and you'll be good to go.
If you want a more permanent way to ensure you don't accidentally lose the new additions to this property, such as when you need to make any changes to the script in the future that will reset its value, then you'll need to store the information in an external file, which will serve as a sort of "dictionary" of vocabulary terms.
The simplest way to do this is to create a text file and put each item of the list on its own line, like this:
Fabulous
Great
Alright
Excited
...etc.
Save it as something like HappyTerms.txt, somewhere like your Documents folder, then change the variable declaration for TheStringsQ1Happy to this:
set TheStringsQ1Happy to the paragraphs of (read "/Users/%you%/Documents/HappyTerms.txt")
replacing %you% with the name of your Home folder in which your Documents folder lives. In fact, it's a useful idea to put the path to this text file in its own variable definition just beforehand:
set VocabDictionary to "/Users/%you%/Documents/HappyTerms.txt"
set TheStringsQ1Happy to the paragraphs of (read VocabDictionary)
Finally, to make changes to this file and add new terms to it, immediately after this line:
if theResponseNotInVocabulary is "Yes" then set end of TheStringsQ1Happy to theResponse
simply add either these lines:
set AppleScript's text item delimiters to return
write (TheStringsQ1Happy as text) to VocabDictionary starting at 1
OR this line
write "\n" & theResponse to VocabDictionary starting at (get eof VocabDictionary) + 1
The first version overwrites the entire file with all the terms in the new list. The second version simply appends the new addition to the end of the file. You might want to experiment with both and get the file turning out the way you want it to, as you'll sneakily discover that one might give you a stray blank line in the file that results harmlessly in an empty string "" appearing in your list; whilst the other does not; but I'll leave you to figure out if and why this happens, and—should you really want it not to happen—how to prevent it. 🙃 Either way, it shouldn't cause you any problems.
If you have any other queries, post a comment and I'll back to you. If this is helpful, don't forget to +1 my answer, and mark it as "correct" if solves your problem for you.
This works for me using the latest version of Sierra
If this script is saved as an application,and you launch this new app... every time a new Item is added to the list, that new item will remain in the list every time you reopen the application. However if you open the application again in script editor,to edit the code and re-save... You will lose all of the values of the added list items And the whole cycle starts over again with the default list.
property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
set theResponse to (display dialog "How Do You Feel Today?" default answer "" giving up after 25)
if text returned of theResponse is in TheStringsQ1Happy then
display dialog "That's Great!"
else
say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
set theResponseNotInVocabulary to display dialog "Add This Term" & " " & quote & text returned of theResponse & quote & " " & "To My Vocabulary?" buttons {"No", "Yes"} default button 2
if the button returned of the result is "Yes" then
if TheStringsQ1Happy does not contain text returned of theResponse then
set end of TheStringsQ1Happy to text returned of theResponse
end if
return TheStringsQ1Happy
end if
end if

How to change variables in applescript with if command

I have tried this forever, what am I doing wrong?
you get my variable repeatvar by
set repeatvar to the text returned of (display dialog "set repeatvar")
if repeatvar is greater than "1000" then
set repeatvar to "1"
end if
when i enter any number it always just sets that number to 1, regardless of the number. What am I doing wrong?
If you want to perform numeric comparison with strings – which behave different as integers – you have to tell AppleScript explicitly to consider numeric strings
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
considering numeric strings
if repeatvar is greater than "1000" then
set repeatvar to "1"
end if
end considering
The syntax of your first line is not correct. with this syntax, you will never get "text returned" valid value, but only valid "button returned".
to have text returned, you must tell the script that you expect user to input a value. this is done by adding words "default answer"":
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
The second issue with your text is that you want to compare the text entered with the string "1000". Comparing strings and numbers does not always give correct result. For instance strings are using alphabetical order and "9" is greater than "1000", because "9" > "1".
Script bellow will work :
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
try
set myNumber to repeatvar as integer
on error
set myNumber to 0
end try
if myNumber is greater than 1000 then
set repeatvar to "1"
end if

Capture Result of Input - Display Dialog

How to store the result of input in a variable ?
For exemplo:
display dialog "Insert Number" default answer "1" buttons{"Save"} default button 1
-- Result: {button returned:"OK", text returned:"1"}
How to capture the result and declare as the value of a variable?
set number to text returned as string
^ It doesn't work.
Help ? =P
set userResponse to text returned of (display dialog "Insert Number" default answer "1" buttons {"Save"} default button 1)

Getting line by line in Apple Script from Address Books Note Field

I have two lines in my address book's note field
Test 1
Test 2
I would like to get each line as a separate value or get the last line from the notes field.
I tried doing it this way:
tell application "Address Book"
set AppleScript's text item delimiters to "space"
get the note of person in group "Test Group"
end tell
but the result is
{"Test 1
Test 2"}
I'm looking for :
{"Test1","Test2"}
What am I doing incorrect?
There are a few things wrong with your code. First, you never actually ask for the text items of the note :-) You just get the raw string. The second is that set AppleScript's text item delimiters to "space" sets the text item delimiters to the literal string space. Thus, for instance, running
set AppleScript's text item delimiters to "space"
return text items of "THISspaceISspaceAspaceSTRING"
returns
{"THIS", "IS", "A", "STRING"}
Secondly, even if you had " " instead of "space", this would split your string on spaces, and not newlines. For instance, running
set AppleScript's text item delimiters to " "
return text items of "This is a string
which is on two lines."
returns
{"This", "is", "a", "string
which", "is", "on", "two", "lines."}
As you can see, "string\nwhich" is a single list item.
To do what you want, you can just use paragraphs of STRING; for instance, running
return paragraphs of "This is a string
which is on two lines."
returns the desired
{"This is a string", "which is on two lines."}
Now, I'm not entirely clear on exactly what you want to do. If you want to get this for a specific person, you can write
tell application "Address Book"
set n to the note of the first person whose name is "Antal S-Z"
return paragraphs of n
end tell
You have to split it into two statements because, I think, paragraphs of ... is a command, whereas everything on the first line is a property access. (I usually discover these things via trial and error, to be honest.)
If, on the other hand, you want to get this list for every person in a group, it's slightly harder. One big problem is that people without a note get missing value for their note, which isn't a string. If you want to ignore these people, then the following loop will work
tell application "Address Book"
set ns to {}
repeat with p in ¬
(every person in group "Test Group" whose note is not missing value)
set ns to ns & {paragraphs of (note of p as string)}
end repeat
return ns
end tell
The every person ... bit does exactly what it says, getting the relevant people; we then extract their note's paragraphs (after reminding AppleScript that the note of p really is a string). After this, ns will contain something like {{"Test 1", "Test 2"}, {"Test 3", "Test 4"}}.

Resources