List values in AppleScript? - macos

I have the following AppleScript so far:
# List of possible options to control the development environment.
set WhatDoUWantToDoList to {"1", "2", "3", "4"}
set MySites to {"test1", "test2"}
# task selected
set selectedTask to {choose from list WhatDoUWantToDoList with prompt "Pick your task now!!!" without multiple selections allowed}
if selectedTask is equal to {"1"} then
display dialog selectedTask
else
# site selected
set selectedSite to {choose from list MySites with prompt "Pick the site your working on!!!"}
if (selectedTask is not equal to false and selectedSite is not equal to false) then
display dialog selectedTask
display dialog selectedSite
else
display dialog "you messed up!"
end if
end if
I am trying to say if option 1 is selected in list 1 display only the selected task, however, if any other option is selected in list 1 you have to move to the new code block, and must select an option in list 2, if you cancel on list 1 and list 2 you screwed up.
Any ideas on what I am missing here?

{ } in AppleScript creates a list, so when you set selectedTask, you're putting the results from choose from list into another list. When you try to compare the result to {"1"}, it's actually {{"1"}}, so it isn't equal.
Use parentheses ( ) for grouping instead.

using this code worked: if selectedTask contains "1" then

Choose from list will always return an array because multiple selection is possible. The basic idea is:
set selectedValues to (choose from list {"Value 1", "Value 2"} with prompt "Choose")
if (selectedValues is not false) then
set selectedValue to item 1 of selectedValues
display dialog ("You chose " & selectedValue as text)
else
display dialog ("You did not chose!")
end if

Related

Bot Framework Rendering Choices Changes Format

I've just tested a dialog with a list of choices that seem to render differently and I can't find any information on why this would be happening.
Given this list of choices:
Choices = ChoiceFactory.ToChoices(new List<string>
{
"No",
"Yes - xxxxxxxxxxxxxxxxx",
"Yes - xxxxxxxxxxxxxxxxx",
"Yes - xxxxxxxxxxxxxxxxx",
"Yes - xxxxxxxxxxxxxxxxx"
})
It renders like this:
Whereas this list:
Choices = ChoiceFactory.ToChoices(new List<string>
{
"No",
"Yes 2",
"Yes - 3",
"Yes - 4",
"Yes - 5"
})
It renders how I want it too:
I've got other instances where I have a long list of buttons that scroll so I'm very confused why the first list above rendered like a list.
How can I force it to render like the second example?
This behavior has to do with the size of each choice and the amount of choices. When the choices themselves are small (ex: "Yes 2"), then they are able to show as the button type (how you want it to look). When they are displayed in this fashion, and there are many choices; then it will scroll off the screen (as you have seen).
When the choices (any one choice) becomes longer (ex: "Yes - xxxxxxxxxxxxxxxxx") then they get put into the list format. I don't believe there is a way to override this, but I will take a look. If not; the only option is to try and keep your choices small in size.
Additionally; each channel handles rendering/displaying in it's own fashion. For example; if i create many (~20) choices of the small variety; then they will show as the scroll-able "buttons" in web chat, where in Skype they will show as a list.
Because of the strange relationship between Choice and his CardAction, You can shorten the value, but leave the title long, so you'll see cards with long text.
var values = new Dictionary<string, string>
{
{"1", "No"},
{"2", "Yes - xxxxxxxxxxxxxxxxx"},
{"3", "Yes - xxxxxxxxxxxxxxxxx"},
{"4", "Yes - xxxxxxxxxxxxxxxxx"},
{"5", "Yes - xxxxxxxxxxxxxxxxx"}
};
Choices = values.Select(v =>
new Choice(v.Key)
{
Action = new CardAction(title: v.Value, value: v.Key)
})
.ToList()
The key point is to put a short value in Value and a desired value in Title.
The rest of the CardAction settings can be found in this answer.

Logic not working with List items in Applescript

This is a very strange problem I could not understand it, the code is very clear as you can see, I don't know if I am tired or could not see something... please tell me why I am getting False as a result, while it should be True, I have a list with one item and it is the exact one in the variable
thanks
property forbidenFolders : {"/Volumes/USERS/"}
set ff to "/Volumes/USERS/" as text
my isForbidenFolder(ff)
on isForbidenFolder(SelectedFolder)
repeat with i in forbidenFolders
log "forbiden folders: " & i
log "actual folder : " & SelectedFolder
if i = SelectedFolder then
log "this folder is forbiden"
return true
end if
end repeat
log "NOT forbiden"
return false
end isForbidenFolder
result here
That's the reference trap.
The syntax repeat with item in list iterates thru the list with references e.g. a reference to item 1 of list, a reference to item 2 of list etc. rather than the item itself.
To be able to check for equality you have to dereference the item using contents of
if contents of i = SelectedFolder then
When i is set to the item of a list in a repeat loop, you are getting a reference to the item. You need to coerce it into a string for your comparison.
if (i as string) = SelectedFolder

Applescript and empty mail signature / input in value list

I have two questions with my applescript. The script is supposed to send a dropped file as attachment by email and asking the object of the mail from a list.
The content of the message MUST be empty.
1) How to set an "empty" email signature because the content of my mail should be empty. I receive an error code "error in mail impossible to solve signature..."
2) I wish that the user can modify the value list {"00111111111111-number1, "0011111111111-number2"...} and add more numbers. What is the best approach to do this ?
Thanks very much in advance for your suggestions.
property theSubject : "subject"
property theNumber : ""
property theContent : ""
property theSignature : "none"
property onRun : ""
on run
tell application "Finder"
set sel to (get selection)
end tell
set onRun to 1
new_mail(sel)
end run
on open droppedFiles
new_mail(droppedFiles)
end open
on new_mail(theFiles)
set chosen to choose from list {"0011111111111-number1", "0011111111111-number2"} with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
set theNumber to text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
if onRun < 1 then
make new attachment with properties {file name:theFiles as alias} at after last paragraph
end if
set the content to theContent
set message signature of newMessage to signature theSignature
end tell
activate
if onRun < 1 then
send
end if
end tell
end new_mail
to add new item in the list, may be this could help you ?
set BaseList to {"0011111111111-number1", "0011111111111-number2"}
set CList to BaseList & {"Add new item"}
set chosen to choose from list CList with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
if "Add new item" is in chosen then
set OKNew to false
repeat until OKNew
set NewItem to display dialog "Enter new value :" default answer ""
set OKNew to (button returned of NewItem is "OK") and (text returned of NewItem is not "")
set theNumber to text returned of NewItem
set BaseList to baseList & {theNumber} -- to add the new item in the BaseList
end repeat
else
set theNumber to text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened
end if
I also experienced some issues with the signature and the attachement together. if you remove the line "make new attachment..." the signature line is working. Also if you move the signature line before the attachement line, and make a break before the attachement, the signature will be OK. bug ?
Then, based on my tests, if you do not want signature at all, just delete the line "set message signature..." by default, no signature will be set.
My last comment is to reduce your script by adding the content directly in property list of the "make new outgoing message..."
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:TheNumber, content:TheContent}
tell newMessage
make new to recipient with properties {address:faxboxmail}
if onRun > 1 then
make new attachment with properties {file name:theFile as alias} at after last paragraph
end if
end tell
activate
end tell
I tried it, and it is creates mail with no content and no signature as expected (Mail 8.2)

Can't make item 1 of <<script>> into type string in Applescript

I'm getting this error after exporting my script as an application. When running the script in script editor it works fine. But when running the application I get his error
Can't make item 1 of <> into type string
here is the code I have for it.
set deptList to {"basketball", "football", "golf", "compliance"}
(choose from list deptList)
if result is not false then
set dept_name to item 1 as string
else
display dialog "Operation Cancelled"
error number -128
Try using:
set dept_name to text of result
Applescript knows it's a string (or text) already, so we just need to tell it to set the variable dept_name to the text of the result.
tell application "Finder"
set deptList to {"basketball", "football", "golf", "compliance"}
(choose from list deptList)
if result is not false then
set dept_name to text of result
else
display dialog "Operation Cancelled"
error number -128
end if
display dialog dept_name
end tell
Choose from list has nothing to do with the Finder. Also, there is no text of result. There is a text returned of result but that is "present only if ‘default answer’ was supplied" with a dialog. Lastly, you should avoid using "result" altogether and assign the response to a variable.
set deptList to {"basketball", "football", "golf", "compliance"}
set dept_name to choose from list deptList with title "Insert your Title" with prompt "Insert your prompt" without multiple selections allowed
if dept_name is false then
error number -128
else
set dept_name to first item of dept_name
end if
Here is an example where using text returned of result would be applicable:
display dialog "Enter text" default answer "My text" buttons {"Cancel", "OK"} default button 2
return the result

Check value in dropdown

I want to check the value in a dropdown list. The list is preconfigured to hold a yes or a no.
At the moment, I am using a check box, that looks like this:
If chkboxOne.Value = vbChecked And (LenB(txtDetailsRefNo.Text) = 0) Then
If vblnShowErrors Then Err.Raise 10000, VALIDATION, "A Default Reference Number must be entered."
blnDataFail = True
End If
Can I simply change chkboxOne to "cboboxOne" by swapping the checkbox for a combobox on the form, and replacing "vbChecked" with True? I'm not sure how similar their functionality is syntax wise.
Thanks
To get the item in a combobox you can examine the listindex to see whats selected (there is no value property)
cboboxOne.AddItem "yes" '//listindex is 0
cboboxOne.AddItem "no" '//listindex is 1
cboboxOne.AddItem "maybe"  '//listindex is 2
...
if (cboboxOne.ListIndex = 0) Then '// yes selected
You can also examine the selected text:
if (cboboxOne.List(cboboxOne.ListIndex) = "yes") Then '// yes selected
You can also test against custom integers using ItemData
cboboxOne.AddItem "yes"
cboboxOne.ItemData(cboboxOne.NewIndex) = 42
cboboxOne.AddItem "no"
cboboxOne.ItemData(cboboxOne.NewIndex) = &HBEEF
...
if (cboboxOne.ItemData(cboboxOne.ListIndex) = 42) Then '// yes selected

Resources