0

I've been having some trouble with this. I'm new to Automator and I'm looking to make a Quick Service to: have a script that copies the selected text (on Safari), adds it to the end of the same URL the text is on, and then saves that new link to the clipboard with some additional characters. Example of what I'm looking for:

URL: https://en.wikipedia.org/wiki/Penny_Mordaunt

Selected Text: She used the word

Desired Result in Clipboard: https://en.wikipedia.org/wiki/Penny_Mordaunt#:~:text=She%20used%20the%20word

I dont have too much so far but what I do have is:

tell application "Safari"
    
    set theURL to URL of current tab of window 1
    set theName to theURL & "selected text"
    
end tell

The first line does get the URL of the tab. The part I'm having trouble in is getting the selected text and appending with the correct special characters in the right places.

This would be adding #:~:text= before and %20 before every consective word selected.

Any help would be great!

spooky
  • 1
  • 1
  • Does https://stackoverflow.com/questions/23852182/i-need-to-url-encode-a-string-in-applescript help? Note that a Quick Action workflow will be passed the selected text. – red_menace Jul 29 '22 at 13:13
  • `tell app id "com.apple.safari" to tell the front window to if it exists then tell (make new tab with properties {URL:the current tab's URL & "#:~:text=" & the input}) to set the index to 1`, where `input` is the variable that by default forms part of the `on run` handler in a **Run AppleScript** Automator action, into which this line could be placed should you ultimately wish the URL to be opened in Safari (which is easier than putting it on the clipboard, as there’s no need to percent encode the spaces). – CJK Jul 30 '22 at 02:06

1 Answers1

-1

I ended up getting it! This worked for me:

property theURL : ""

tell application "Safari"
    
    set theURL to URL of current tab of window 1
    set theText to (do JavaScript "getSelection().toString()" in document 1)
    set newURL to theURL & "#:~:text=" & theText
    
end tell
spooky
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '22 at 06:05