How to remove only the characters inside brackets [ ] from a string

applescript

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 \"" & the_text_items & "\" | 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.

Best Answer

Updated Answer

This is the most straight forward method to accomplish the goal:

Example AppleScript code:

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

set theName to ¬
    characters 1 thru ¬
        ((offset of "," in theName) - 1) ¬
            of theName as string

Result:

@junior_cat23

To address the comment:

What if I have multiple names, for example "@junior_cat23 @james_cat24 [ID]-[E9B-Z8X-H1V-722]" and I want to keep both of them while the string does not have commas?

Example AppleScript code:

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

set theName to ¬
    characters 1 thru ¬
        ((offset of "[" in theName) - 2) ¬
            of theName as string

Result:

@junior_cat23 @james_cat24

Original Answer

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|[ ]+$||'<<<" & theName's quoted form

      • 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.
  • <<< -- Here Strings - A variant of here documents, the format is: [n]<<< 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).
  • & 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.