delimiter usage with variable text

hey everyone… new to applescript but I’ve done some coding in C before. I’m trying to make a script that will take what’s on the clipboard and remove “1998 Digital Remaster” from it. Or, any other year. here’s what I’ve tried:

set this_string to the clipboard
set OldDelim to AppleScript’s text item delimiters
set counter to 1970
repeat until counter = 2007
set AppleScript’s text item delimiters to " ( " & counter & " Digital Remaster)"
set AppleScript’s text item delimiters to OldDelim
set y to this_string
set the clipboard to y
if y contains “Digital Remaster” then
set counter to counter + 1
display dialog "counter = " & counter & " & y = " & y & " & item delimiters = " & OldDelim & “”
else
set counter to 2007
end if
end repeat
return y

My thoughts are that it would go through all the years, and when it finds one that matches the text, say “1998 Digital Remaster” it would get rid of that, end the loop and return the original clipboard string minus the “1998 Digital Remaster.” You can see I have a bit of debugging code in there to see what values I’m getting for my variable “OldDelim” but it’s always blank, even when the counter is the same as the year in the text. If anyone could help me out that would be great!

-Weston

Somethins like this might be able to work for you without having to do a repeat loop.

set this_string to the clipboard
if this_string contains "Digital Remaster" then
	return do shell script "echo " & quoted form of this_string & " | sed 's/[0-9]\\{4\\} Digital Remaster//'"
end if

Thanks for the quick reply, would you mind explaining a bit of what exactly this line means?

return do shell script “echo " & quoted form of this_string & " | sed ‘s/[0-9]\{4\} Digital Remaster//’”

Like I said I’m new to this and that whole line looks pretty foreign to me…

-W

Hey W, here is what’s happpening.

do shell script "echo " & quoted form of this_string

We are outputting to stdout the contents of this_string (which we got from the clipboard) in quoted form so we don’t break any paths if they contain spaces.

 | sed 

The “|” redirects the stdout from echo into our next statement sed. Sed is a stream editor that is very powerful. The format for sed, when fed data from a pipe, is

sed command

In this case we are using the substitue command. The syntax for substitue is:

‘s/find text/replacement text/’

So looking at our code we have the following,

Find Text: [0-9]\{4\} Digital Remaster
Replacement Text: (nothing)

The tricky part here is deciphering the find text. Sed is powerful because, among other reasons, it allows the use of regular expressions. I would love to explain them in detail, but there are entire books written on the subject. The key point is this though

[0-9]{4} Digital Remaster

means to find a pattern of digits 0-9 that exist in a group 4 and then is immdiately followed by " Digital Remaster". If that pattern is found it is then replaced by our replacement text, which being nothing, essentially cuts our found text from the string.

The last important bit here is that because we are running this from a applescript we need to escape the \ character with the \ character which is why

[0-9]{4} becomes [0-9]\{4\}

Hope that helps!

Thanks for the explanation, that helps very much… way more concise than the loop I was thinking of using!