Sunday, November 29, 2020

#1 2018-02-20 07:23:00 am

geddy76
Member
Registered: 2007-03-08
Posts: 83

short name for choose from list

I feel like I've seen how to do this before, but I'm blanking due to lack of coffee at the moment.
If I have a list of email addresses, can I easily associate those addresses to a "short name" which would be used for "choose from list" portion?

Applescript:


set theUsers to {"John", "Howard", "Han"}
set theAssignees to {"John.Smith@company.com", "Howard.Hughes@company.com", "Han.Solo@star.wars"}


set theAssign to choose from list theUsers

Basically, I want "Josh, Howard and Han" to be the choices in the list. But I want the variable theAssign to be set to the full email address. I know I could do a series of if...else if statements to force it, but thought there might be a more elegant solution using an array of the two lists, or something.

Thanks!

Offline

 

#2 2018-02-20 11:08:24 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11699
Website

Re: short name for choose from list

Hi,

two suggestions:

1) Create the list with an index prefix

Applescript:

{"1 John", "2 Howard", "3 Han"}

then get the first word of the result, coerce it to integer and get the address by that index

2) Use a handler to get the index of a given item in a given list


Applescript:

set theUsers to {"John", "Howard", "Han"}
set theAssignees to {"John.Smith@company.com", "Howard.Hughes@company.com", "Han.Solo@star.wars"}

set theAssign to choose from list theUsers
if theAssign is false then return
set assignIndex to indexOfItem(item 1 of theAssign, theUsers)
set theAddress to item assignIndex of theAssignees

on indexOfItem(theItem, theList)
   repeat with i from 1 to count theList
       if item i of theList is theItem then return i
   end repeat
end indexOfItem


regards

Stefan

Offline

 

#3 2020-11-19 06:41:23 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: short name for choose from list

geddy76 wrote:

I feel like I've seen how to do this before, but I'm blanking due to lack of coffee at the moment.
If I have a list of email addresses, can I easily associate those addresses to a "short name" which would be used for "choose from list" portion?


Here is other (the fastest solution). The OP should add match list, then run this code:

Applescript:


set theUsers to {"John", "Howard", "Han"}
set theAssignees to {"John.Smith@company.com", "Howard.Hughes@company.com", "Han.Solo@star.wars"}
set matchList to {"-1-", "-2-", "-3-"}

try
   set theChoice to item 1 of (choose from list theUsers)
on error
   return
end try


set thePosition to my indexOf(theChoice, theUsers)
set theAssign to (get item thePosition of theAssignees)

on indexOf(theItem, theList)
   set oTIDs to AppleScript's text item delimiters
   set AppleScript's text item delimiters to return
   set theList to return & theList & return
   set AppleScript's text item delimiters to oTIDs
   try
       -1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
   on error
       0
   end try
end indexOf

NOTE: it is faster, because you create matchList once, then choose name multiple times.

Last edited by KniazidisR (2020-11-19 06:56:14 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#4 2020-11-19 09:23:54 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: short name for choose from list

KniazidisR wrote:


Applescript:


on indexOf(theItem, theList)
   set oTIDs to AppleScript's text item delimiters
   set AppleScript's text item delimiters to return
   set theList to return & theList & return
   set AppleScript's text item delimiters to oTIDs
   try
       -1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
   on error
       0
   end try
end indexOf



Here is my version of your handler.

Applescript:

set theUsers to {"John", "Howard", "Han"}
set theAssignees to {"John.Smith@company.com", "Howard.Hughes@company.com", "Han.Solo@star.wars"}

try
   set theChoice to item 1 of (choose from list theUsers)
on error
   return
end try


set thePosition to my indexOf(theChoice, theUsers)
set theAssign to (get item thePosition of theAssignees)

on indexOf(this_item, this_list)
   repeat with i from 1 to the count of this_list
       if item i of this_list is in this_item then return i
   end repeat
   return 0
end indexOf

Last edited by Fredrik71 (2020-11-19 09:28:45 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#5 2020-11-19 04:04:42 pm

robertfern
Member
Registered: 2011-11-29
Posts: 82

Re: short name for choose from list

Here is my version of the handler "indexOf"

Applescript:


on indexOf(theItem, theList)
   local tid, tempList
   set tid to text item delimiters
   set text item delimiters to return
   set tempList to theList as text
   set text item delimiters to tid
   try
       return count paragraphs of text 1 thru (offset of theItem in tempList) of tempList
   on error
       return 0
   end try
end indexOf

Last edited by robertfern (2020-11-19 04:07:27 pm)

Offline

 

#6 2020-11-20 09:32:14 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: short name for choose from list

Fredrik71,

I can certainly see that you are pretty good at programming, maybe better than me, but you don't need to suggest this StefanK's repeat loop approach over and over again. This is what I'm trying to rid the user of.

robertfern,

your handler and mine are twins because they are one and the same.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-11-20 10:54:27 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5312

Re: short name for choose from list

KniazidisR wrote:

… but you don't need to suggest this StefanK's repeat loop approach over and over again. This is what I'm trying to rid the user of.


And you, KniazidisR, don't need to start replying to old threads again. This one dates from February 2018 and the OP received a perfectly good answer then. They're unlikely still to be watching it in case you happen to come up with an answer that others have thought of before.

You may not tell others what they can and cannot post here.


NG

Offline

 

#8 2020-11-20 11:26:30 am

robertfern
Member
Registered: 2011-11-29
Posts: 82

Re: short name for choose from list

KniazidisR

Yours seems to do unnecessary things like manually concatenateing returns at front and rear of list
then subtract 1 from answer (WHY)

Mine also doesn't change the passed list. Since AppleScript passes list by reference, this could be problematic. Mine copies contents of list as text to temp variable

So no, yours and mine are not the same.

Just my opinion

Last edited by robertfern (2020-11-20 11:30:08 am)

Offline

 

#9 2020-11-20 11:37:36 am

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 96

Re: short name for choose from list

You could use a record or dictionary instead of two arrays and repeat loops.

Applescript:


use scripting additions
use framework "Foundation"

set NSDictionary to current application's class "NSDictionary"

set theAssigneesRecord to {John:"John.Smith@company.com", Howard:"Howard.Hughes@company.com", Han:"Han.Solo@star.wars"} as record
set theAssigneesDictionary to NSDictionary's dictionaryWithDictionary:theAssigneesRecord
set theAssigneesDictKeys to theAssigneesDictionary's allKeys() as list

try
   set theChosenAssignee to item 1 of (choose from list theAssigneesDictKeys)
on error
   return
end try

set theAssigneesEmail to (theAssigneesDictionary's valueForKey:theChosenAssignee) as text

Just another option

Regards Mark

Last edited by Mark FX (2020-11-20 02:25:21 pm)

Offline

 

#10 2020-11-20 01:29:55 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: short name for choose from list

Mark FX wrote:

You could use a record or dictionary instead of two arrays.


Thanks for sharing, do you know if that will be faster approach in huge list ??

KniazidisR wrote:

...


Some people know the answer to a question but its always the person who ask or seek the answer who decide if the answer is of any value to them. People who like to judge that also limit the
creative mind to innovate something new or take old ideas to build something better.

I will thank StefanK next time I will talk to him, I use that handler in many Script so useful.


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#11 2020-11-20 02:07:44 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 96

Re: short name for choose from list

Fredrick71 wrote

Thanks for sharing, do you know if that will be faster approach in huge list ??



I'm not sure if it would be significantly faster, as it's still AppleScript at the end of the day.
So the awnser to that would be try testing it.

My guess would be that because it's using the NSDictionary class, it should be faster than AppleScript lists and repeat loops.
But I'm not sure it would be noticeably different.
I changed all of my projects and applets over to Swift applications and utilities some time ago, which are ten times faster, but I wouldn't know if Foundation classes like NSArray or NSDictionary perform much quicker than AppleScript Lists and Records.

Sorry for not helping much with that question.

Sometimes it's not just a performance thing, but a preference thing.
To me the record or dictionary method looks and feels less messy and more concise.
And why use a loop if you don't need to ?

But were all different, and what looks and feels good to me, might not to others.

Mark

Last edited by Mark FX (2020-11-20 02:28:10 pm)

Offline

 

#12 2020-11-20 03:40:37 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5312

Re: short name for choose from list

robertfern wrote:

Yours seems to do unnecessary things like manually concatenateing returns at front and rear of list
then subtract 1 from answer (WHY)


Hi robertfern.

Bracketing both the search string and the return-delimited source string with returns ensures that the delimitation occurs in the right place, not at some other point which happens to have the same characters as the bare search string. The returns at each end of the source string are needed for when the return-bracketed search string occurs at the beginning or the end of it.


NG

Offline

 

#13 2020-11-20 05:29:20 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

Re: short name for choose from list

Mark FX wrote:

Sometimes it's not just a performance thing, but a preference thing.
To me the record or dictionary method looks and feels less messy and more concise.



I like that approach. For me the process of learning AppleScriptObjC also make it easier to learn other language from Apple like Objective-C and Swift. And when we feel more comfortable about
coding we could choose a solution with better performance.

I have heard people who have start coding in AppleScriptObjc and become Objective-C coder.
Maybe that was the same for you when you start to code in Swift.

Last edited by Fredrik71 (2020-11-20 05:31:17 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#14 2020-11-20 08:24:41 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6499

Re: short name for choose from list

Fredrik71 wrote:

do you know if that will be faster approach in huge list ??



For a big list ( a thousand or more) it possibly will be -- but who's going to use choose from list with that many entries?

That said, it can probably be simplified a little:

set theEmail to ((current application's NSDictionary's dictionaryWithObjects:theAssignees forKeys:theUsers)'s objectForKey:theChoice) as text



And if you're putting up a dialog, which halts a script, a one-off hit of a fraction of a second is probably just noise in the overall scheme of things.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#15 2020-11-20 11:19:02 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: short name for choose from list

AsObjC-solution proposed by Mark FX is the only worthy competitor to my plain-AppleScript solution in this topic. Everything else is pouring water from empty to empty and back. I don't mean StefanK. His solution is the first working solution in this thread, although I don't find it the most efficient one. Why I was looking for something else. I didn't want to offend anyone here.

Last edited by KniazidisR (2020-11-20 11:32:10 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#16 2020-11-21 12:02:25 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6499

Re: short name for choose from list

KniazidisR wrote:

I didn't want to offend anyone here.



Then I'd suggest you stop passing judgment on others' posts. Post your own versions by all means, but please don't set yourself up as some kind of modern Solomon.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#17 2020-11-21 12:54:49 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: short name for choose from list

Hi, Shane,

The exact repetition of the solution already proposed above seemed to me an act hostile to me. Or was it really done on purpose? Then I have the right to defend myself.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#18 2020-11-21 03:31:22 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5312

Re: short name for choose from list

KniazidisR wrote:

The exact repetition of the solution already proposed above seemed to me an act hostile to me. Or was it really done on purpose? Then I have the right to defend myself.


You have the right to complain to either Mark or myself if you feel any script posted here is somehow an act hostile to yourself. You have a right to discuss the relative merits of various approaches and to defend your own if someone else criticises it. You don't have a right to set yourself up, as Shane said, "as some kind of modern Solomon".

Other people post their solutions here in good faith in order to contribute to the discussion, not to attack you. It's not for you to judge publicly whether their efforts are worthy competitors to your own.

This topic is now closed.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)