0

Can I create a list from the clipboard?

Let's say my clipboard is `name email phone and any random number of text

Can the words be separated and added in a list?

Also the item can be either text and numbers, but I guess the best way would be to do a text delimiter by space?

  • 1
    For a solution, just do set myList to the words of (the clipboard) which will yield {"name", "email", "phone"} for your supplied example data. – CJK Dec 7 '18 at 15:30
  • @CJK, you should make an answer out of your comment so it can get upvoted. – user3439894 Dec 7 '18 at 22:50
  • I wasn't sure if that was against etiquette, but ok then 🙂 – CJK Dec 8 '18 at 0:01
  • @CJK, It' not, and technically answers are supposed to be posted as answer, not comments. I'll post an answer as a comment if I don't have time to write a good answer that's more then a one line comment type answer or if I intend to write an actual answer a bit later etc. Anyway, nice expansion on your comment answer as an answer +1. – user3439894 Dec 8 '18 at 2:09
2

Let's say my clipboard is "name email phone" and any random text and numbers.
Can the words be separated and added in a list?

You don't realise it, but you've answered your own question.

TL;DR

Say your clipboard contains the string "Kevin stackexchange.com 124098". Separate this into a list of words like so:

words of (the clipboard) --> {"Kevin", "stackexchange.com", "124098"}

You can read more about this builtin AppleScript feature below, where I've given a detailed explanation, including why, sadly, this won't be quite as effective if an email address is involved:

words of "Kevin@example.com" --> {"Kevin", "example.com"}

So you'll either need to remember to restore the obliterated "@" symbol later; or, as you suggested, use text item delimiters set to a space character.

Detailed Information: text item delimiters vs words

AppleScript text objects are comprised of word and character elements that subdivide one another into the component building blocks of any string: text subdivides into words and/or characters; a word subdivides into characters.

Moreover, the words subdivide text at intervals marked by whitespace of any length, which is much more convenient than text item delimiters, which will split a string at each individual occurrence of a specified delimiter.

For example, setting text item delimiters to the space character, and applying it to a two-word phrase with three spaces between them, you'll end up with this:

text items of "foo   bar" --> {"foo", "", "", "bar"}

However, obtaining instead the words that make up the string avoids having to deal with empty-string list items:

words of "foo   bar" --> {"foo", "bar"}

Moreover, like text items, words of a text object will subdivide the string naturally into a list of individual items.

For completeness, I'll quickly demonstrate how characters will decompose a string:

characters of "foo"       --> {"f", "o", "o"}
characters of "foo   bar" --> {"f", "o", "o", " ", " ", " ", "b", "a", "r"}

A bit of caution is warranted when decomposing a string into words, believing it operates identically (or near enough) to a space delimiter. It doesn't. There are a defined set of characters that are considered "word characters", and a set of characters that are not. "Word characters" are any character that, when grouped together, are considered word-forming. If any non-word character appears, the word is considered split into two distinct words at that character.

Word characters basically include the letters A-Z and a-z (and any accented variants), numbers 0-9, the period (.) and the underscore (_). So "tH1s" is a word, and "this_is_a_single_word.123". However, "this#is%four words", and, counterintuitively to me, "over-achieving" is considered two words (the hyphen is a non-word character).

Other non-word characters obviously include the space; or any whitespace for that matter. Plus any symbol you can type with your keyboard that wasn't included in the list of word characters, except the dollar sign ($), which I annoyingly left to now to mention just to reflect how annoying it is that it's randomly considered a word character:

words of "hello#$&world" --> {"hello", "$", "world"}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .