Get all text up to first "("

Below is the code I’m working on. I’m stuck with trying to get rid of “(Radio Paradise)”.

--I want this script to get the name of the current track playing on Radio Paradise TNP 2013

tell application "Safari"
	set radioWindow to the name of every window whose name contains "(Radio Paradise)"
end tell
-- example return: {"John Butler Trio - Losing You (Radio Paradise)"}

set trackTitle to all text up to first "("
--so I want trackTitle to return: "John Butler Trio - Losing you" but it won't compile because I can't figure out the syntax to tell it to return only the text before the parenthesis.

return trackTitle

Any help appreciated.
Thanks!
Tia

Hi.

tell application "Safari"
	set radioWindow to the name of first window whose name contains "(Radio Paradise)"
end tell
-- example return: "John Butler Trio - Losing You (Radio Paradise)"

set trackTitle to text 1 thru ((offset of "(" in radioWindow) - 2) of radioWindow

The -2 omits the space before the “(” as well. If you want to keep it, use -1 instead.

Nigel,

Thank you so much for the help! However it kept returning an error until I added “of (radioWindow as string)” instead of just “of radioWindow”.

So for posterity’s sake, here’s the working script:

--This script will get the name of the current track playing on Radio Paradise

tell application "Safari"
	set radioWindow to the name of every window whose name contains "(Radio Paradise)"
end tell

set trackTitle to text 1 thru ((offset of "(" in radioWindow) - 2) of (radioWindow as string)
return trackTitle

Cheers

Parsing text is a job for regular expressions, and the easiest way to use them in Applescript IMO is to install the Satimage.osax.

http://tinyurl.com/dc3soh

It’s quick too.

-ccs


set testList to {"John Butler Trio - Losing You (Radio Paradise)"}
set _len to length of testList
if _len = 1 then
	set _item to testList as text
else
	error "Bail! List length unexpected: [" & _len & "]."
end if
set groupAndTitle to find text "^.+?(?= *\\()" in _item with regexp and string result

--> "John Butler Trio - Losing You"

set {_group, _title} to splittext groupAndTitle using " *- *" with regexp

That’s undoubtedly because you changed ‘first window’ back to ‘every window’ and thus got a list containing the name instead of just the name. You can’t get text 1 thru anything of a list. Surprisingly, ‘offset’ does work in this case, doing an automatic coercion for its bit of the line as long as there’s only one item (text, of course) in the list.

Ahh, I see. I hadn’t noticed that you had changed “every” window to “first” window. I’m still new to applescript, so thank you for taking the time to explain why I kept getting the error. I’ve cobbled together my rudimentary skills in applescript from various online forums and how-tos, so my knowledge is piecemealed together. Here is my current script (which works well after I changed ‘every’ to ‘first’):

--This script gets the name of the current track playing on Radio Paradise, appends it to my document, then copies to clipboard
try
	tell application "Safari"
		
		set radioWindow to the name of first window whose name contains "(Radio Paradise)"
		set omitRadio to text 1 thru ((offset of "(" in radioWindow) - 2) of radioWindow
		set artistName to text 1 thru ((offset of "-" in omitRadio) - 2) of omitRadio
		set trackTitle to text ((offset of "-" in omitRadio) + 2) thru end of text of omitRadio
	end tell
on error
	display dialog "Radio Paradise isn't open."
	error number -128
end try

--opens my Music doc, appends track and artist as new text, saves and closes
set filepath to POSIX path of "Users/Tia/Desktop/Music.rtf"
try
	set command to "open " & quoted form of filepath
	do shell script command
end try

tell application "TextEdit"
	delay 0.5
	set myMusicDoc to front document
	make new paragraph at end of text of myMusicDoc with data (omitRadio as string) & return
	save myMusicDoc
	close myMusicDoc
	set the clipboard to (trackTitle as string) & " " & (artistName as string)
	tell application "TextEdit" to quit
end tell