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:
Result:
To address the comment:
Example AppleScript code:
Result:
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 aconsidering 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 followingsed
command handles the example value oftheName
variable in your OP and in the comments.So whether you have:
Or:
Then:
• Hint: Mouse over and horizontal scroll to see full code.
Returns:
Or:
Without any leading/trailing spaces.
Understanding the
do shell script
andsed
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 variabletheName
to the end of the command line of thesed
command in thedo shell script
command so as not to expand any shell special characters in the value of the variabletheName
, if any exists.