Running the following AppleScript code in AppleScript Editor:
set theText to "I ate an apple at 11:54 pm without the skin."
set theTime to do shell script "awk -F ' at | am | pm ' '{print $2}'<<<" & quoted form of theText
log "The time was: " & theTime
set theText to "I ate two navel oranges at 6:30 am with a glass of water."
set theTime to do shell script "awk -F ' at | am | pm ' '{print $2}'<<<" & quoted form of theText
log "The time was: " & theTime
Produces the following output in the AppleScript Editor's Event Log:
tell current application
do shell script "awk -F ' at | am | pm ' '{print $2}'<<<'I ate an apple at 11:54 pm without the skin.'"
--> "11:54"
(*The time was: 11:54*)
do shell script "awk -F ' at | am | pm ' '{print $2}'<<<'I ate two navel oranges at 6:30 am with a glass of water.'"
--> "6:30"
(*The time was: 6:30*)
end tell
In the above examples I've defined the field separators (delimiters) in awk
using the -F
option as ' at | am | pm '
which equates to " at ", " am " and " pm " and it prints '{print $2}'
what's between the field separators.
Note: The use of the log
command is not necessary to the coding for the answer and is being used just to show what the value of theTime
contains for the Event Log output aside from what's shown after -->
, which is the result as normally shown in the Event Log.
Update: I wrote my original answer based on a literal interpretation in that when said, "So, I would like the opening text item delimiter to be at_
and the closing text item delimiter to be either _pm
or _am
", what was wanted was to literally use those as the delimiters. However, since a different solution using pure AppleScript code, in a separate answer, was presented, let me present a one-line AppleScript code solution that does the same thing the 8 lines of pure AppleScript code does and by focusing on the colon, but as as part of a RegEx representation of the time in hours and minutes.
set theTime to do shell script "awk 'match($0,/[0-9]{1,2}:[0-5][0-9]/) {print substr($0,RSTART,RLENGTH)}'<<<" & quoted form of theText
Running the following AppleScript code in AppleScript Editor:
set theText to "I ate an apple at 11:54 pm without the skin."
set theTime to do shell script "awk 'match($0,/[0-9]{1,2}:[0-5][0-9]/) {print substr($0,RSTART,RLENGTH)}'<<<" & quoted form of theText
set theText to "I ate two navel oranges at 6:30 am with a glass of water."
set theTime to do shell script "awk 'match($0,/[0-9]{1,2}:[0-5][0-9]/) {print substr($0,RSTART,RLENGTH)}'<<<" & quoted form of theText
Produces the following output in the AppleScript Editor's Event Log:
tell current application
do shell script "awk 'match($0,/[0-9]{1,2}:[0-5][0-9]/) {print substr($0,RSTART,RLENGTH)}'<<<'I ate an apple at 11:54 pm without the skin.'"
--> "11:54"
do shell script "awk 'match($0,/[0-9]{1,2}:[0-5][0-9]/) {print substr($0,RSTART,RLENGTH)}'<<<'I ate two navel oranges at 6:30 am with a glass of water.'"
--> "6:30"
end tell
As you can see, whether or not there are one to two numbers preceding the colon the RegEx matches and the awk
program returns the desired match, of which being the time.
Personally, I'd choose to use this particular method over my original answer, as it's a better method under the circumstances, and or over the pure AppleScript code as I can't justify writing 8 lines of pure AppleScript code when a single line of regular AppleScript code produces the same results as the 8 lines do!
Best Answer
If I were you, i will search for online superscript converter to convert that word 1 to ¹ such as https://lingojam.com/SuperscriptGenerator
Then i will copy converted text
> pages
> find "[1]"
> change "Find" to "Find & replace"
> paste the copied text in the replace field "[¹]"
> click on "Replace all"