First newbie question. Obtain all the single letters in a word/phrase

Hello everyone,
I’m absolutely new to AppleScript but I’m trying to understand if AS can help me to develop a small and not-so-useful idea I have. Now with Automator available…maybe I’ll be able to create it :slight_smile:

The first thing to understand is…

…is there a way to enter a word or small phrase in a dialog and let AS get all the single letters in it and “pass” them in some way as variables.

In example…the dialog appears, I write the world “hello”, AS extracts the letters “h”, “e”, “l”, “l” and “o”…and then with Automator (because I can’t handle AS too much) I’d tell what to do with those letters.

Hope not too cryptic :slight_smile:

Paolo

Thats very easy to do:

set thecharacterslist to get characters of text returned of (display dialog "gimme the phrase:" default answer "")

This will return a list of the characters

E.g.:
typing “hello” will return: {“h”, “e”, “l”, “l”, “o”}

Good speed!
Vince

Hi tampano,

There are several ways to do this. Here’s one way:

display dialog “Enter a word or phrase:” default answer “hello”
set user_text to text returned of result
set char_list to characters of user_text

gl,

@ kel:
:P;)

@ tampano:
kel’s and my script do almost exactly the same!
The only difference is that my script creates the variable directly without a temporary one
But the result is the same!

Ok, you both are simply great! :slight_smile:

Now…the second phase is: how to “isolate” the results? Or better. How can I recall each of those letters as a single value?

In example: the result of the starting script is a list formatted this way (i can see in the result window on ScriptEditor:
{“h”, “e”, “l”, “l”, “o”}

Ok?

I’d like to have each single value, identifyable as each one.

If I want to tell Automator (always because now I can’t do it in AS :slight_smile: ) “create (or choose) a document which name starts with the first value of the result”…how to do that? Now I have those values all in a a row, right?

The final result would be something like “choose a file which name starts with “h”, then another starting with “e””…etc

Is it possible?

Thanks for your kindness

Paolo

You mean something like this?

set thecharacterslist to get characters of text returned of (display dialog "gimme the phrase:" default answer "")

repeat with thechar in thecharacterslist
	set thefile to choose file with prompt ("Choose a file which begins with a '" & thechar & "'")
end repeat

If you could tell us a bit more precise about what you want to do exactly - we could help you with better code (as we then know what it is for - and how to write it in the best way)!?

Example:
Do you want to display a dialog that tells the user to choose a file beginning with the desired character?
Or do you want Applescript (or Automator - how you call it) to find a file that begins with the desired character itself?

Well…not exactly.

The idea is, from a given source folder or drive, the program would choose automatically the exact sequence of documents which names are starting with each letter of the string.

So:
a) the first phase of the script gets the string and identifies all the letters
b) at some point of the execution (even before “a)”, no matter) the user has to determine from where to get the documents
b1) (maybe filter only given kind of documents)
c) the second phase of the script choose the documents and organize them in some way (group in another folder, copy the documents elsewhere…something like that)
d)…not thought abot “d” :slight_smile:

Being not a AS expert (neither a simple user) I think all the things could work with a combination of AS and Automator…maybe.

Paolo

I haven’t embedded a kind filter but I think this is the raw structure:

set thecharacterslist to get characters of text returned of (display dialog "gimme the phrase:" default answer "")
set thefolder to choose folder with prompt "Choose folder to take the files from:"
set thefiles to list folder thefolder
repeat with thechar in thecharacterslist
	set thefiltered to {}
	repeat with i in thefiles
		if i starts with thechar then set end of thefiltered to i
	end repeat
	--thefiltered is the list of file in "thefolder" (only files in first folder level!) that beginn with character "thechar"
	--action would go here:
end repeat

Have fun!

Vince

Ps: Why do that with an Automator action?
Automator actions are nice if you want many actions that can follow in custom order.
But if you only want this to be done . why not just make an Applescript Application?
I don’t get the point why to make an action - sorry!

Thanks for the example.

The reason is I’m completely blind about AS :slight_smile: Simple. And I was looking for a way to use Automator without knowing too much about AS language of programming.

Maybe it’s better to explain my small idea.

I’d like a piece of software who asks me a small phrase and create an iTunes compilation of songs which name starts with every single letter of the given phrase.

So…I write “I love you” and the script/program/workflow/whateverelse scans my iTunes music library and creates a new playlist with, in this case, eight songs like:

  1. In the summertime
  2. Let it be
  3. One
  4. Vanity fair
  5. Every breath you take
  6. Yer blues
  7. On my own
  8. Under pressure

A simple and cool way to creat an “acronym” playlist to make cool gifts to friends. That’s all :slight_smile:

Dou you think is there a way to do it?

Paolo

set str to "I love you"
set playlistName to "new list"

tell application "iTunes"
	set thePlaylist to make new playlist with properties {name:playlistName}
	repeat with charRef in str
		if charRef is in "abcdefghijklmnopqrstuvwxyz" then
			duplicate (some track of playlist "library" whose name begins with charRef) to thePlaylist -- note: will raise error if no suitable track found
		end if
	end repeat
end tell

Simply great!!! Thanks.

It will be nice to start learning a little more starting from here.

Thanks again

Paolo