changing spaces to hyphens

ok, so just for a little tester, i am trying to to get spaces to display as hyphens instead. So say I do something like this

set the_artist to "children of bodom"
display dialog the_artist

but i want all the spaces to be hyphens in the dialog.

I haven’t tested this to be sure, but I think this might work, though I don’t know if it’s what you’re looking for…

set the_artist to "children" & "-" & "of" & "-" & "bodom"
display dialog the_artist

That (should) display a dialog “Children-Of-Bodom”

But why, may I ask, do you need the spaces to be hyphens? (Just curious)

Try something like this:

set the_artist to "children of bodom"

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set the_artist to text items of the_artist

set AppleScript's text item delimiters to {"-"}
set the_artist to the_artist as Unicode text
set AppleScript's text item delimiters to ASTID

display dialog the_artist

well i have an iTunes controller/quick song player and i would like to add a feature that displays the lyrics of the current song in a scroll view. So the problem with your code is the number of hyphens depend on the number of spaces in the song playing, so the code is going to be more .advanced

thanks Bruce that was what I was looking for:D

Bruce, just wondering if there was a reason you went “as Unicode text” versus “string” or even just “text”?

Oh boy. I’d dig up a good explanation, but I’m a bit short on time today. Anyone else want to help me out on that? Bonus point if you also explain why you might use something like this instead (assuming I’m remembering this line correctly):

set the_artist to "" & the_artist

Well I understand the difference between Unicode text and 8-bit (MacRoman) I’m just wondering if you had a specific reason in mind to return Unicode as opposed to the others.

As for the bonus point ya got me. I was guessing that it was because it would keep it’s encoding as defined in the intal set, but that does not appear to be the case.

ok just one more thing, is there a script that say you have a dialog that asks for an artist name and they capitilize the the first letter (or if they are dumb) all the letters, well…w/e…to be plain and simple, is there a script that converts a string to all lowercase letters?

set s1 to "UPPER"
set s2 to lowercase s1

well, that doesnt work one bit but yeah…ok, i have another question, i have a script to grab certain text from the source code of the web page and return it, but there is a problem…but first the code:

set the_URL to "http://www.lyricsdir.com/" & the_artist & "-" & the_song & "-lyrics.html"
set lyricHTMLSource to (do shell script "curl " & the_URL)
(* Chop off all of the text before the lyricsr *)
set AppleScript's text item delimiters to {"<div id="lyrics">"}
set lyricHTMLSource2 to text item 2 of lyricHTMLSource
(* Chop off all of the text after the lyrics *)
set AppleScript's text item delimiters to {"<"}
set the_lyrics to text item 1 of lyricHTMLSource2
(* Set out delimiters back to normal *)
set AppleScript's text item delimiters to {""}
return the_lyrics

The probelm is here:

set AppleScript's text item delimiters to {"<div id="lyrics">"}

because the div id is in quotes so applescript thinks the property ends at the first quote before lyrics…anyway to fix this?

What doesn’t work? The lowercase thing I posted?

As for the div tag escape the " marks, give this a try

set AppleScript's text item delimiters to {"<div id=\"lyrics\">"}

yes the lowercase thing did not work.

Ok, more time than expected.

As of AppleScript 1.9.2 (OS X v10.3) (See the release notes):

Unicode is going to be the standard; Plain text might choke on international characters.

Actually, that should be the case. :slight_smile: There maybe cases in which a string is still desired. Not enough time to expand on all that though, sorry.

I believe that uses TextCommands.

ok this is my get lyrics script…it is not working…which sucks

set song_search to display dialog "What is the song called?" default answer ""
set the_song to the text returned of song_search as string
set artist_search to display dialog "Who is the artist?" default answer ""
set the_artist to the text returned of artist_search as string


set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set the_song to text items of the_song
set the_artist to text items of the_artist

set AppleScript's text item delimiters to {"-"}
set the_song to the_song as Unicode text
set the_artist to the_artist as Unicode text
set AppleScript's text item delimiters to ASTID
set the_URL to "http://www.lyricsdir.com/" & the_artist & "-" & the_song & "-lyrics.html"
set lyricHTMLSource to (do shell script "curl " & the_URL)
(* Chop off all of the text before the number *)
set AppleScript's text item delimiters to {"<div id=\"lyrics\">"}
set lyricHTMLSource2 to text item 2 of lyricHTMLSource
(* Chop off all of the text after the number *)
set AppleScript's text item delimiters to {"<"}
set the_lyrics to text item 1 of lyricHTMLSource2
(* Set out delimiters back to normal *)
set AppleScript's text item delimiters to {""}
return the_lyrics

Why won’t it work?

and i have textcommands installed but it still won’t work…

For the leading blank quote issue - I can’t see that it matters. AppleScript normally coerces the following items in a concatination to the type of the first item. Seems inconsistent in this example.


-- using Script Debugger 4 which tell you the type:
set b to "some text" as Unicode text -- unicode
set c to "some text" -- macRoman
set d to "" & b -- unicode
set e to "" -- macRoman
set j to "" as Unicode text
set f to e & b -- unicode
set g to e & c -- macRoman
set h to j & c -- unicode

Thanks Bruce.

So do I get half a point then? :smiley:

And your right, I wasn’t paying attention to the dictionary from which that command came… sorry bout that hendo.

Side note: At some point, display dialog started returning Unicode text:

display dialog "What class is this stuff?" default answer "Test"
class of (text returned of result)
--> Unicode text, for me

Again, I can’t find a reference, but at some point it was changed to coerce to Unicode if any Unicode was found.

More examples (see above side note):

display dialog "What class is this stuff?" default answer "Test"
set the_artist to (text returned of result)
set the_artist to "" & the_artist

class of the_artist
--> Unicode text

set the_artist to "Test"
set the_artist to "" & the_artist

class of the_artist
--> string

P.S. Change Text Case

Aside from the lowercase, what is not working ?