Lookup / Search within macros (telephone directory) / internal "database" in KM and how to do it elegantly?

Phone-Directory Macros.kmmacros (9.2 KB)

I have enclosed a couple of macros in a macrogroup (named "Phone-Directory"), which is identical to what I use (the macros), execpt for the content in these being fake names and numbers.

The phone number would be in the clipboard or as a variable "target" (we can start from there)....

GOAL:

Perhaps 3 variables named "target" (phone number to look up and "status" (if true or false) and "result" (what to output on the screen), may be used for these tasks?

  • Search to check if the phone number is already within the contents of the macro list (please check the file above with the macros), and in the variable "status" set that variable "true" if the number is found and also return the corresponding name in the variable "result".

So searching for the number: 32274410, should set "status" to "true" (since it exsists) and the output "result" should be:

"Bama V/ InTunn Bundteigen"

And searching for the number: 92435623 should set "status" to "true" (since it exsists) and the output "result" should be:

"Anne Landet"

And searching for "12348892", should yield as "false" as this number does not exsist. The output on the screen should be:

"12348892, does not exist in your phone directory!"

OTHER:

Please disregard the info with a red cross over, the important info in the macro is in the macro name.... (I do not think that other info in the macro as important for this task):
macro_phone_dir

Is it possible to search within the whole file with all the macros in it? Is all the macros stored within one file?

Sorry, but I have to say that this is a very poor design for a telephone directory.
Putting each contact in KM as a macro has lots of problems.

I'd suggest using a TAB-delimited text file, which you can easily create/maintain using Excel (always saving as text). You can then easily read the Phone Directory.txt file using KM, and search and parse it as needed.

I'll stop here for now, until you provide some feedback on my comment/suggestion.

1 Like

Thanks! I prefer to leave it like it is. I really appreciate how it works for now. The only thing I do not understand, is how to search for the numbers as written above (and having the lookup return values / name).

Anyways. I do alreadly have the phone directory alreade as a text file (for backup etcetera).

How to search within that text file? Can it be a word doc and the info inside a table there?

Possible to search that file quickly withouth opening Word at all from KM?

See

No. I have done this many times. The best approach for small datasets (< 1,000 records) is a plain text file.

Could this be a TextEdit file as a "plain text" format document (Unicode utf-8)? Would that be the best? No Table or anything? Should I seperate the phone numbers and names by ";" or any other seperator character?

I would like to be able to sort that document later so the phone numbers and names comes into order. So probably a good idea to plan that now.

This is how that doc. is looking now:
phonedirtest

Here is that file: Phonedirectory_test.zip (889 Bytes)

I have to agree with @JMichaelTX and reiterate that the current implementation of this telephone directory is not at all one that you should persist with. Given that the title of this post does ask how to do it elegantly, you should consider re-evaluating.

As an alternative to a text file, which is a fine idea suggested by @JMichaelTX, I might suggest storing the contacts in this directory in a KM dictionary. A single dictionary (called, say, "phonebook") can contain innumerable entries stored as key-value pairs, where:

{value} = %Dictionary[phonebook,{key}]%

represents one person in the phonebook. Conventionally, one would set the {key} to be the person's name, and the {value} to be the person's number, which you can very easily perform a search using a For Each action that operates over dictionary keys, comparing the number (value) of each person until it finds the name (key) that is associated with it.

However, as is sounds like you'll predominantly be searching by number, there's absolutely no reason you couldn't switch this around and have the {key} be set to a person's number, and the {value} to the person's name. The advantage of this is that you then don't need to do any searching or looping: you can directly access the dictionary item by the phone number, and have it return the name of the person instantly.

2 Likes

Thanks I appreciate your input on this! For now I will go for the advice of @JMichaelTX and explore the possiblities using a text file for this purpose.

That’s good. I’m just glad you’re moving away from your original design :+1:t3:

You can use any text editor that supports plain text, but I would use TAB as the delimiter.
My preference is to use Excel, and SaveAs text file.
Excel allows me to view the data in columns, and to sort, copy, etc.
You can save as a ".txt" file, and also right-click on the text file and open with Excel.

Having said all that, I wonder if creating your own system for a phone directory is wise. Is there some reason you don't want to use the Contacts.app or Outlook.app, or other contacts app of your choice? Both of those apps provide a lot more data stored with each contact, and are scriptable.

1 Like

So then you can use the table function in Excel? I am not used to that program, not too fond of the company behind it. Numbers the program on the Mac does not work in the same manner then?

You don't need the "table" function.
Don't let the brand name keep you from using a superior product.
If you want to use Apple Numbers, you will have to determine if it works in the same way. I dislike and don't use Apple Numbers.