简体   繁体   中英

Applescript alter text on clipboard

I have written an AppleScript to automate my work setup and just need the final touch. I copy something to clipboard in my script; I need to edit the text I copied to leave only the last line (preferably without opening an actual text editor application, as this would clutter my workspace) and just paste that line in the Google Chrome browser.


In the last step of my script so far I do something causing message text to be output to Terminal. Then I get the text visible in Terminal to be copied to the clipboard as follows:

tell application "Terminal"
    tell front window
        set the clipboard to contents of selected tab as text
    end tell
end tell

Now I can for example paste it, and it's something like

...
[I 15:03:31.259 LabApp] Serving notebooks from local directory: /workspace
[I 15:03:31.259 LabApp] The Jupyter Notebook is running at:
[I 15:03:31.259 LabApp] http://1fw5c518af9:1932/?token=5e6d97b348fsy734gd
[I 15:03:31.259 LabApp]   or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd
[I 23:56:47.798 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 23:56:47.803 LabApp] 
    
    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime-open.html
    Or copy and paste one of these URLs:
        http://1fw5c518af9:1932/?token=5e6d97b348fsy734gd
     or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd

This always ends with text of the format or *URL* , eg in the above example it would be or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd . This is separated from the line above by a line delimiter plus some trailing white-space before "or".

What I need to do is grab that URL, start a Google Chrome browser and paste it in there.

For opening Google Chrome, it should be simple enough, the following code (from this tut ) takes you to Instagram:

tell application "Google Chrome" to activate

tell application "System Events"
   key code 37 using command down
   delay 0.5
   keystroke "https://www.instagram.com/instagram"
   delay 1
   key code 36
   delay 1

end tell

and literally all I need is something which edits what's on the clipboard to just the *URL* (in the case of the above example, just http://88.0.0.1:1932/?token=5e6d97b348fsy734gd ). After that I can paste it from the clipboard (where Instagram is typed in, in the tutorial).


After trying the lovely answer by @user3439894 below I found the situation is a bit more complicated:

  1. there are an unpredictable number of blank lines "" after the final link.
  2. I'm also wondering whether there may be white-space at the end as well

I already know the form of http://88.0.0.1 (it's always consistently this) so is there a way of maybe searching for that and then just grabbing it with the rest of the link that follows? (it would be great to be able to delimit the end by either " " or new-line)

Edit 2: In case http://88.0.0.1... occurs multiple times, we would like to select just one of these URLs - they are probably generally the same, but selecting the last would be safest.

What I need to do is grab that URL, start a Google Chrome browser and paste it in there.

&

This always ends with text of the format or *URL* , eg in the above example it would be or http://88.0.0.1:1932/?token=5e6d97b348fsy734gd . This is separated from the line above by a line delimiter plus some trailing white-space before "or".

&

In case http://88.0.0.1... occurs multiple times, we would like to select just one of these URLs - they are probably generally the same, but selecting the last would be safest.

If what you are trying to do is get the last URL that starts with http://88 from the contents of the selected tab of the front window in Terminal , then here is a way to do it without using the clipboard, and open it in Google Chrome , and also do it without having to use UI Scripting .

In other words, no need to alter the text on the clipboard or keystroke the URL as you can just tell Google Chrome what to use for the URL .

Example AppleScript code :

tell application "Terminal" to ¬
    set theTextFromTerminal to ¬
        the contents of ¬
            the selected tab of ¬
                the front window ¬
                    as text

set theURL to ¬
    the last paragraph of ¬
        (do shell script ¬
            "grep -o 'http://88.*$' <<< " & ¬
            theTextFromTerminal's quoted form & ¬
            "; exit 0")

if theURL is "" then return
if theURL does not start with ¬
    "http://88." then return

tell application "Google Chrome"
    activate
    delay 1
    if exists front window then
        make new tab at ¬
            end of front window ¬
            with properties {URL:theURL}
    else
        set URL of ¬
            the active tab of ¬
                (make new window) to theURL
    end if
end tell

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide . See also, Working with Errors . Additionally, the use of the delay command may be necessary between events where appropriate, eg delay 0.5 , with the value of the delay set appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM