1

I have a piece of applescript (added underneath) that basically does two things

  1. grab the selected text and takes that to url request (e.g. urban dictionary url) in order to have a safari window popup with the result (translation or definition)
  2. push some usage statistics to google analytics using curl.

Adding the google analytics curl push part ('nr 2') makes the first part (as experienced by end-user) significantly slower. My assumption here is that somehow this google analytics part (curl push) is done prior to the url request (instead of after or simultaneously). How can I can speed up the visible part (i.e. the url request), like by making this simultaneous with the url push making sure the url request comes first?

on run {input, parameters}
    set output to "https://www.urbandictionary.com/define.php?term=" & urldecode(input as string)
    set Cntr_post to "https://www.google-analytics.com/collect?v=" & 1 & "&t=" & event & "&tid=" & "UA-99571xxx-1" & "&cid=" & 12345678 & "&ec=" & "EasyLookUp" & "&ea=" & "UrbanDictionary" & "&el=" & "xxx_test_cp" & "&aip=" & 1
    do shell script "curl " & quoted form of Cntr_post
    return output
enter code here
end run
on urldecode(x)
    set cmd to "'require \"cgi\"; puts CGI.escape(STDIN.read.chomp)'"
    do shell script "echo " & quoted form of x & " | ruby -e " & cmd
end urldecode

It works as coded above. But commenting out the google analytics part makes just the first action (the url request with the safari popup) significantly faster (avg of 3.5 seconds instead instead of 5.5sec).
Can I change this code in such a way that the analytics part (curl push) does not delay the first part?

Update 1: here is all an image where it is all in one canvas enter image description here

Update 2: if I split the code up, to make the analytics part run afterwards this works, but it does not run 'in the background' it waits until the first is finished (this means it waits until the safari popup is closed).. But for now this seems the best option.

Thank you user3439894 and Ted Wrigly for your help!
@Ted Wrigly: I will try your answer next, thank you!

3
  • There is nothing in the AppleScript code you've shown that has anything to do with Safari, unless it's in the enter code here part, or there is another action after the Run AppleScript action. It would probably be more helpful if you showed a screenshot of the Automator Service/Quick Action or at least provide the missing details. That said, is the some reason why you can not separate the tasks so that everything necessary for the User happens first, then in the background the Google Analytics part runs? Commented Sep 10, 2020 at 14:31
  • 1
    Just to be clear, you don't need or use any response from the 'google analytics' curl command, right? The slowdown you're seeing is probably because the do shell script command is pausing the script while it waits for the command to signal that it has completed. if you detach that unix command as a separate process, the script will continue immediately. Commented Sep 10, 2020 at 16:40
  • 1
    Steven B. Peutz, After seeing your updated question, I'd definitely go with Ted Wrigley's answer! Commented Sep 10, 2020 at 19:48

1 Answer 1

1

Most likely your script is waiting for the google analytics curl command in your do shell script to complete before it moves on. If you don't care about the output of that command, and are content to have it run in parallel in the background, change your fourth line to the following:

do shell script "curl " & quoted form of Cntr_post & " &> /dev/null &"

The added code directs any output from the curl command to /dev/null (i.e., consigns it to oblivion), releases the curl command to the background, and immediately returns to process the remainder of the AppleScript. See the relevant section of Technical Note TN2065 for further details.

Sign up to request clarification or add additional context in comments.

1 Comment

That is gold, exactly what I was hoping for! Thank you Ted Wrigley!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.