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 theon waitForPageToFinishLoadingInSafari()
handler, and it is shown in the comment-- # NOTE:
within the code below.Example AppleScript code:
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:
Note: In order to use the Quick Action service, each application you trigger it from will need to be added to two locations in:
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.
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.