How To Remove Only The Characters Inside Brackets [ ] From A String So I have been searching for hours on how to do this via Applescript and I see no post about it.

Basically, I have the following string:


  @junior_cat23, [ID]-[E9B-Z8X-H1V-722]


I want the script to remove only this part:


  , [ID]-[E9B-Z8X-H1V-722]


So I am left only with:


  @junior_cat23


I am using "AppleScript's text item delimiters" but the problem is that it will also remove the characters from the text I want to keep. 

This is the script I am using:

set theName to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"

tell application "Finder"

    -- Remove Characters

    set oldDelims to AppleScript's text item delimiters
    set the_strings_to_strip to {"[", "]", "-", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

    set the_files to theName
    repeat with a_file in the_files
        set the_name to theName
        repeat with I from 1 to count of the_strings_to_strip
            set AppleScript's text item delimiters to item I of the_strings_to_strip
            set the_text_items to text items of the_name
            set AppleScript's text item delimiters to ""
            set the_name to the_text_items as string
        end repeat
    end repeat

    -- Remove Preceding and Trailing Spaces in String

    set trimName to "echo \"" &amp; the_text_items &amp; "\" | xargs"
    set nameTrimmed to (do shell script trimName)
end tell


I get the following result:


  "@_,"


I just want to remove the characters in side of the brackets and keep the others. I will appreciate a solution that does not require a handler since I will be using the script with an application that does not accept handlers.
One of the reasons you were not getting returned what you wanted is in some cases AppleScript is case insensitive and wrapping the -- Remove Characters code in a considering case block handles some of the issues, but not all. Additionally, since you have numeric characters in both what you want retained and not, then as currently coded it removes all numeric characters.

While your current code could be reworked, I'm not inclined to do that as from both deleted comments and still existing comments you've left out vital information in the OP. That said, I'll assume for the moment that all target strings will have either , [ ... ] or [ ... ] that needs to be removed and the following sed command handles the example value of theName variable in your OP and in the comments.

So whether you have:

set theName to "@junior_cat23, [ID]-[E9B-Z8X-H1V-722]"


Or:

set theName to "@junior_cat23 @james_cat24 [ID]-[E9B-Z8X-H1V-722]"


Then:

do shell script "sed -E -e 's|\\[.*||' -e 's|,||g' -e 's|^[ ]+||' -e 's|[ ]+$||'&lt;&lt;&lt;" &amp; theName's quoted form


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;• Hint: Mouse over and horizontal scroll to see full code.

Returns:

@junior_cat23


Or:

@junior_cat23 @james_cat24


Without any leading/trailing spaces.



Understanding the do shell script and sed commands:


do shell script -- Executes a shell script using the sh shell.
sed -- Stream Editor.


-E -- Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE’s). The re_format(7) manual page fully describes both formats.
−e command -- Append the editing commands specified by the command argument to the list of commands.
's|\\[.*||' -- Removes everything from the first [ character to the end of the line.
's|,||g' -- Removes all commas, if they exist.
's|^[ ]+||' -- Removes all leading spaces, if they exist.
's|[ ]+$||' -- Removes all trailing spaces, if they exist.
&lt;&lt;&lt; -- Here Strings - A variant of here documents, the format is: [n]&lt;&lt;&lt; word - The word undergoes brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion and word splitting are not performed. The result is supplied as a single string, with a newline appended, to the command on its standard input (or file descriptor n if n is specified).

&amp; theName's quoted form -- Appends the single quoted value of the variable theName to the end of the command line of the sed command in the do shell script command so as not to expand any shell special characters in the value of the variable theName, if any exists.