Search code examples
macoscurlgoogle-analyticsapplescriptautomator

In applescript (using MacOS automator) can I manipulate the order for two actions within one piece of code? (to benefit speed)


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!


Solution

  • 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.