Open url in new tab and paste the copied text

applescriptsafari

I'm new to AppleScript. I want to use AppleScript with Sci-hub page. In my scenario, I highlight DOI address -> AppleScript will open "Sci-hub.tw" page in new tab on Safari -> paste DOI address to text box -> press Enter / Return.
I found in many sites and tried to code. It here but my code just open url in new tab, not paste the highligh address.

Please, help me!

Thanks and best regards!

on run {input, parameter}
   tell application "Safari"
        activate
        try
        tell window 1 to set current tab to make new tab with properties {URL:"http://www.sci-hub.tw"}
             on error
                open location theURL
        end try
        tell application "System Events"
            tell process "Safari"
                 activate
                 keystroke "v" using command down
                 delay 0.3
                 key code 36 #return
            end tell
        end tell
    end tell
end run

Best Answer

The example AppleScript code, shown below, may be helpful to you.

As coded, it does work from Script Editor in macOS High Sierra; however, for macOS Mojave, one minor edit it required to the tell application "System Events" command in the on waitForPageToFinishLoadingInSafari() handler, and it is shown in the comment -- # NOTE: within the code below.

Example AppleScript code:

set theURL to "http://www.sci-hub.tw"

tell application "Safari"
    activate
    try
        tell window 1 to set current tab to make new tab with properties {URL:theURL}
    on error
        open location theURL
    end try
end tell

my waitForPageToFinishLoadingInSafari()

tell application "System Events"
    keystroke "v" using command down
    delay 0.3
    key code 36 #return     
end tell

--  # Handlers:

on waitForPageToFinishLoadingInSafari()
    
    --  # NOTE: For macOS Mojave, change 'UI element 1' to 'UI element 2` in the code below.
    
    tell application "System Events"
        repeat until (accessibility description of ¬
            button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page") contains "Reload this page"
            delay 0.5
        end repeat
    end tell
    
end waitForPageToFinishLoadingInSafari
  • Note: As coded, it assumes you have the desired search string already on the Clipboard so when System Events does ⌘V, it is pasted into the text box on the page.

Note: The example AppleScript code is just that and sans error handling from your original code it 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.


Update:

This update is to address explicitly how the OP is using his Quick Action service in Automator as per his comment to this answer.

The example AppleScript code, shown below, has been tested in macOS Mojave and works on my system as is.

Replace the default code of the Run AppleScript action with the following example AppleScript code:

on run {input, parameters}
    
    set theURL to "http://www.sci-hub.tw"
    set theSearchString to (item 1 of input as text)
    
    tell application "Safari"
        activate
        try
            tell window 1 to set current tab to make new tab with properties {URL:theURL}
        on error
            open location theURL
        end try
    end tell
    
    my waitForPageToFinishLoadingInSafari()
    
    tell application "System Events"
        keystroke theSearchString
        delay 0.2
        key code 36 -- # Enter Key     
    end tell
    
end run


--  # Other Handlers:

on waitForPageToFinishLoadingInSafari()
    
    -- # NOTE: For macOS High Sierra and earlier , change 'UI element 2' to 'UI element 1' in the code below.
    
    tell application "System Events"
        repeat until (accessibility description of ¬
            button 1 of UI element 2 of every group of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page") contains "Reload this page"
            delay 0.5
        end repeat
    end tell
    
end waitForPageToFinishLoadingInSafari

Note: In order to use the Quick Action service, each application you trigger it from will need to be added to two locations in:

  • System Preferences > Security & Privacy > Privacy
    • Accessibility
    • Automation

You should be prompted as necessary and you'll need to unlock the System Preferences > Security & Privacy > Privacy tab as necessary and check the application as necessary. See the images below.


Automator Quick Action

In the following two images, you'll see that Safari and TextEdit have been added and this is a direct result of using the Quick Action service from both of these applications. If I were to select something to be searched with Search With SCI-HUB from the Services menu in, e.g., Mail, then it too will need to be added, and again you should be prompted.

Security & Privacy - Accessibility Security & Privacy - Automation