Applescript (list choices) - list

I need som help with a small Applescript.
When i make my choice in the list. Safari always open test3. Maybe someone can help me?
The code
set choice to choose from list {"1", "2", "3"}
if choice is "1" then
tell application "Safari" to open location "http://test1/"
else if choice is "2" then
tell application "Safari" to open location "http://test2/"
else
tell application "Safari" to open location "http://test3/"
end if

The result from a "choose from list" dialog is a list. Even if it only has 1 item in the list it's still a list. So you really want "item 1" from the list to get the actual result.
To fix your code change your first line to this...
set choice to item 1 of (choose from list {"1", "2", "3"})

When you run your code within the Applescript Editor and you look at the Events/Replies area,
you will see that your selection is returned in braces - {"1"}. They indicate a list was returned back to you. This is especially useful if you wanted to choose multiple items from the list using the with multiple selections allowed statement.
So, your if then statements failed because you were comparing a list {"1"} with a string "1". Since all else failed, test3 was the only option.
To fix it, you can pick an item from the list returned, like this
set choice to item 1 of (choose from list {"1", "2", "3"})
or convert the selection to text so you are comparing apples with apples :)
set choice to (choose from list {"1", "2", "3"}) as text
Hope this helped.

Related

MIT AppInventor list Error : Attempt to get item number 0, of the list [5]

" Select list item: Attempt to get item number 0, of the list [5]. The minimum valid item number is 1.
Note: You will not see another error reported for 5 seconds. "
Hey Guys,
The message on top appear when the app open the app not crashing but I need to click somewhere on screen few times till it disappear..
The idea is to search a value of var 'UID' in firebase.database and when it found it I need to get the value of is brother key..
I want you please to help me to clear this message I tried a few ways to get the result but nothing yet..
Thanks a lot for helpers :)
When Screen Open.
This is how I tried to get the uid && employeeKey.
for your help me I set var 'UID' = to uid value of employee number 1
'UID' get the value from firebase.auth form on screen 1..
The index in list block returns 0, if the "thing" has not been found in the list and in this case you are trying to get the value from the 0th item from the empKey list, which does not make sense.
Therefore you will have to add an if statement... if the returned index is 0, then do not search for the empKey.
Btw. the better place to ask questions like this would be the MIT App Inventor community...

How to extract a column based on it's content in PowerBI

I have a column in my table which looks like below.
ResourceIdentifier
------------------
arn:aws:ec2:us-east-1:7XXXXXX1:instance/i-09TYTYTY79716
arn:aws:glue:us-east-1:5XXXXXX85:devEndpoint/etl-endpoint
i-075656565f7fea3
i-02c3434343f22
qa-271111145-us-east-1-raw
prod-95756565631-us-east-1-raw
prod-957454551631-us-east-1-isin-repository
i-02XXXXXXf0
I want a new column called 'Trimmed Resource Identifier' which looks at ResourceIdentifier and if the value starts with "arn", then returns value after last "/", else returns the whole string.
For eg.
arn:aws:ec2:us-east-1:7XXXXXX1:instance/i-09TYTYTY79716  ---> i-09TYTYTY797168
i-02XXXXXXf0 --> i-02XXXXXXf0
How do I do this ? I tried creating a new column called "first 3 letters" by extracting first 3 letters of the ResourceIdentifier column but I am getting stuck at the step of adding conditional column. Please see the image below.
Is there a way I can do all of this in one step using DAX instead of creating a new intermediate column ?
Many Thanks
The GUI is too simple to do exactly what you want but go ahead and use it to create the next step, which we can then modify to work properly.
Filling out the GUI like this
will produce a line of code that looks like this (turn on the Formula Bar under the View tab in the query editor if you don't see this formula).
= Table.AddColumn(#"Name of Previous Step Here", "Custom",
each if Text.StartsWith([ResourceIdentifier], "arn") then "output" else [ResourceIdentifier])
The first three letters bit is already handled with the operator I chose, so all that remains is to change the "output" placeholder to what we actually want. There's a handy Text.AfterDelimiter function we can use for this.
Text.AfterDelimiter([ResourceIdentifier], "/", {0, RelativePosition.FromEnd})
This tells it to take the text after the first / (starting from the end). Replace "output" with this expression and you should be good to go.

Tkinter: Adding a entry at index 3 without there being one at index 1 or 2

The problem im getting is if type something in my entry box it first fills index 1 and 2 of the listbox before finally typing into the 3rd index.
def country_get(event):
listbox.delete(3)
listbox.insert(3, country_label.cget('text') + event.widget.get() + '\n')
title_text=StringVar()
entry_country=Entry(master, bg="wheat3", fg="dark slate gray", textvariable=title_text)
entry_country.bind('<KeyRelease>', country_get)
entry_country.grid(row=4, column=1)
I want to be able to type at any index of the listbox whether it be the 3rd or 5th, without having anything at the previous index's.
You can't do what you want. The listbox isn't designed to have empty rows. If you want empty rows, you will need to insert empty strings.
Depending on how many entries I'm using or need, i just insert empty strings like so:
listbox.insert(0, "")
listbox.insert(1, "")
listbox.insert(2, "")
listbox.insert(3, "")
listbox.insert(4, "")
It appears that the listbox is empty when it fact it is just filled with empty strings. There are most likely other ways to get around this, but for what I need my program to do, this is what worked for me.

How to manage looping on this list on Applscript?

The list is in the form of:-
0:
https://url
1:
https://url
..... And so on.
How could I loop on this list. So I could fetch the number first without ":" and type it somewhere then fetch the url that comes after that number and type it elsewhere. Then end repeat if the list is over.
Or should I use records instead?
I am still a beginner using AppleScript. I tried many commands I mixed up but the computer keeps running the script nonestop and the activity monitor shows the applescript using 100% of the processor and huge amount of ram.
Appreciate any help.
Thank you
You didn't define what your list really looks like very well so I made an assumption on my answer below. If I was wrong, hopefully my answer will at least point you in the right direction. (or if I've gotten it wrong, but you can choose to reformat it to the way I suggested, that could still help)
on run
set theList to {"0:http://apple.com", "1:http://google.com"} -- my guess at what your list looks like.
repeat with anItem in theList
set anItem to anItem as string
set itemParts to myParseItem(anItem)
set tID to the_integer of itemParts as integer
set tURL to the_url of itemParts as string
end repeat
end run
on myParseItem(theItem)
set AppleScript's text item delimiters to ":"
set delimitedList to every text item of theItem
set newString to (items 2 thru -1 of delimitedList as string)
set AppleScript's text item delimiters to {""}
set theInt to item 1 of delimitedList
set theURL to newString as string
return {the_integer:theInt, the_url:theURL}
end myParseItem

Print key of list w/o knowing its position

I have a list that contains many keys:
mylist = {"a", "b", "c", "1", "2", "3", ...}
and I want to print the key for example that has value "x", without knowing it's exact position in the list. That mean I have to run the whole list and till "x" is found and print it. How could I do this? Seems easy question but it confuses me a bit... Thanks a lot
for key, value in pairs(mylist) do
if value == "x" then print(key) end
You can also create another mapping, eg.
mapping_list = {}
for key, value im pairs(mylist) do
mapping_list[value] = key
(assuming that list elements are unique) then, you'd be able to
print(mapping_list["x"])