How can I strip a question mark and all the following characters from a character string?
How can I create an automator action (to save as a system service) to strip a question mark and all the following characters from a character string?
For example, change this…
https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html?action=click&algo=identity&block=editors_picks_recirc&fellback=false&imp_id=857225215&impression_id=9ec8fd81-a1ef-12eb-a9ba-2593f4310346&index=1&pgtype=Article&pool=editors-picks-ls®ion=ccolumn&req_id=877591687&surface=home-featured&variant=0_identity&action=click&module=editorContent&pgtype=Article®ion=CompanionColumn&contentCollection=Trending
to this…
https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html
Solution 1:
If the selected text (URL in this example) is in an editable field, .i.e. a text box or editable document, then you can create an Automator Service/Quick Action setting Workflow receives current [text] in [any application] with [√] Output replaces selected text, adding a Run AppleScript action using either example AppleScript code below:
Here is one method you can use:
set theString to "https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html?action=click&algo=identity&block=editors_picks_recirc&fellback=false&imp_id=857225215&impression_id=9ec8fd81-a1ef-12eb-a9ba-2593f4310346&index=1&pgtype=Article&pool=editors-picks-ls®ion=ccolumn&req_id=877591687&surface=home-featured&variant=0_identity&action=click&module=editorContent&pgtype=Article®ion=CompanionColumn&contentCollection=Trending"
set myString to ¬
text items 1 thru ¬
((offset of "?" in theString) - 1) ¬
of theString as text
Returns:
https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html
Another method:
set theString to "https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html?action=click&algo=identity&block=editors_picks_recirc&fellback=false&imp_id=857225215&impression_id=9ec8fd81-a1ef-12eb-a9ba-2593f4310346&index=1&pgtype=Article&pool=editors-picks-ls®ion=ccolumn&req_id=877591687&surface=home-featured&variant=0_identity&action=click&module=editorContent&pgtype=Article®ion=CompanionColumn&contentCollection=Trending"
set {TID, AppleScript's text item delimiters} to ¬
{AppleScript's text item delimiters, "?"}
set myString to first text item of theString
set AppleScript's text item delimiters to TID
Returns:
https://www.nytimes.com/2021/04/13/books/review/somebooktitle.html
To use the example AppleScript code, shown above, in the Run AppleScript action, replace the default code with:
on run {input, parameters}
set theString to input as text
set myString to ¬
text items 1 thru ¬
((offset of "?" in theString) - 1) ¬
of theString as text
return myString
end run
Or:
on run {input, parameters}
set theString to input as text
set {TID, AppleScript's text item delimiters} to ¬
{AppleScript's text item delimiters, "?"}
set myString to first text item of theString
set AppleScript's text item delimiters to TID
return myString
end run
Notes:
Either example of the AppleScript code will only return the text up to, but not including the question mark in any selected string of text that contains a question mark.
If the selected string of text does not contain a question mark, the selected string of text is replaced with itself.
The Automator Service/Quick Action as currently configured above does not avail itself to selected text that is not in an editable text field.
If you want to be able to select text that is not in an editable text field, then you can leave [] Output replaces selected text unchecked and replace return myString
with set the clipboard to myString
, which places the modified string on the clipboard, and from there you can paste it where you want.