delete characters in strings where their position variesI am trying to

I am trying to get my video files inline with PLEX conventions, however PLEX never mentioned that I could not have a date in parenthesis in any part of the file. I have many files with (mm-dd-yyyy) in various places in the title. I’ve found several" find and replace" for strings but cannot find a script that will delete a section of text in a string. I know enough about Applescript to know that I know little about text delimiters and string manipulation.
It appears that Stefan’s code could be a basis for simply inserting blanks where ever (mm-dd-yyyy) appears if you could find the “(” as the beginning and the “)” as the end, but since they appear is different positions in different files .I don’t have the scripting ability to do it. Here are two file titles to try:

Bonanza - S08 E09 - Old Charlie (07_25_2012) 808 TVLAND 66.mp4
Gunsmoke - S12 E27 - Ladies From St. Louis (07_31_2012) 6627 WOIODT2 110.mp4

The date appears in a different position in each string so I have to search the string and then delete.

Here is my try using Stefan’s code


set x to "Bonanza - S08 E09 - Old Charlie (07_25_2012) 808 TVLAND 66.mp4"
set {TID, text item delimiters} to {text item delimiters, "("}
set x to text items of x
set text item delimiters to "\\"
set x to "\\" & x as text
set text item delimiters to TID
x 


set y to x
set {TID, text item delimiters} to {text item delimiters, ")"}
set y to text items of y
set text item delimiters to "\\"
set y to "\\" & y as text
set text item delimiters to TID
y 

I don’t know how to capture the beginning and end positions nor how to cut the (mm_dd_yyyy)

Any help would be appreciated

Hi! :slight_smile:



set x to "Bonanza - S08 E09 - Old Charlie (07_25_2012) 808 TVLAND 66.mp4"

set a to offset of "(" in x
set b to offset of ")" in x

set y to text 1 thru (a - 1) of x & text (b + 2) thru -1 of x
”>"Bonanza - S08 E09 - Old Charlie 808 TVLAND 66.mp4"


set x to "Gunsmoke - S12 E27  - Ladies From St. Louis (07_31_2012) 6627 WOIODT2 110.mp4"

set a to offset of "(" in x
set b to offset of ")" in x

set y to text 1 thru (a - 1) of x & text (b + 2) thru -1 of x
”>"Gunsmoke - S12 E27  - Ladies From St. Louis 6627 WOIODT2 110.mp4"

Your movies never start with (date) do they, in that case you must take precautions:


set x to "(07_31_2012) Twin Peaks - S12 E27  - Ladies From St. Louis 6627 WOIODT2 110.mp4"

set a to offset of "(" in x
set b to offset of ")" in x

set y to text (b + 2) thru -1 of x
”>"Twin Peaks - S12 E27  - Ladies From St. Louis 6627 WOIODT2 110.mp4"

Here is the piece you miss:



set a to offset of "(" in x
set b to offset of ")" in x

if a = 1 then
	set y to text (b + 2) thru -1 of x
else
	set y to text 1 thru (a - 1) of x & text (b + 2) thru -1 of x
end if

Since you (wkmanley) are using Snow Leopard, you can use both parentheses as delimiters simultaneously:

on undate(fileName)
	if (fileName contains "(") then
		-- Assumes only one set of parentheses, if any, in the name and that it's complete.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"(", ") "} -- Multiple delimiters require Snow Leopard or later.
		set fileName to text item 1 of fileName & text item -1 of fileName
		set AppleScript's text item delimiters to astid
	end if
	
	return fileName
end undate

undate("Bonanza - S08 E09 - Old Charlie (07_25_2012) 808 TVLAND 66.mp4")

Or there’s the “sed” shell script. It can be arranged for this to edit all the names at once:

set x to "Bonanza - S08 E09 - Old Charlie (07_25_2012) 808 TVLAND 66.mp4"

do shell script ("echo " & quoted form of x & " | sed -E 's/\\([0-9]{2}_[0-9]{2}_[0-9]{4}\\) //'")