0

I've been writing a workflow for Alfred 2 which takes a selected jpeg on the users desktop - inserts it to google chrome then use cliclick to right click and reverse search the image via google search.

I cant seem to avoid having the user to put the cliclick in there home folder any attempt to reference the file elsewhere seems to return a systems events error in applescript.

Any help would be appreciated! (I'm extremely new to applescript and alfred apologises if this is a very simple problem)

--get location of selected folder
tell application "Finder"
    set sel to the selection as text
    set the clipboard to POSIX path of sel
end tell

activate application "Google Chrome"
--open selected jpeg in google chrome
tell application "System Events" to tell process "Google Chrome"
    delay 1
    keystroke "t" using command down
    keystroke "v" using command down
    key code 36
    delay 1
    -- calculate the top left corner of the image
    set myBounds to bounds of window 1 of application "Google Chrome"
    set x to item 1 of myBounds
    set y to item 2 of myBounds
    --locate cliclick in users home
    set uHome to get path to home folder
    set locHome to POSIX path of uHome
    --run the right click -> search by image mouse event
    do shell script "" & locHome & "/cliclick m:" & x & "," & y & "w:100 kd:ctrl c:+2,+74 ku:ctrl w:1000 c:+66,+90 w:700"
end tell

--close "file://" tab
tell application "Google Chrome"
    set windowList to every tab of every window whose URL starts with "file://"
    repeat with tabList in windowList
        set tabList to tabList as any
        repeat with tabItr in tabList
            set tabItr to tabItr as any
            delete tabItr
        end repeat
    end repeat
end tell

I tried using

set UnixPath to POSIX path of ((path to me as text) & "::" & "cliclick")

in replacement of

set uHome to get path to home folder
set locHome to POSIXpath of uHome

but replacing it into the shell script with

do shell script "" & UnixHome & " m:" & x & "," & y & "w:100 kd:ctrl c:+2,+74 ku:ctrl w:1000 c:+66,+90 w:700"

stops the workflow with the System Event error

The Script as it stands (cl.ly link)

1 Answer 1

0

You could put cliclick in the resource folder of your app and call it like this:

set pathtocc to quoted form of (POSIX path of (path to resource "cliclick"))
do shell script pathtocc & " -h"

Or let curl do the job:

set imagefile to POSIX path of ¬
    (choose file of type "public.image" default location path to desktop)

do shell script "
open -a 'Google Chrome' $(curl -F 'encoded_image=@" & imagefile & ¬
    "' https://www.google.com/searchbyimage/upload | grep 'tbs=sbi' | sed 's/^.*http/http/;s/\".*$//')"

curl -F uploads the file to reverse search. The output is a redirect which includes the URL to the google result page.

3
  • Still getting the error with trying to call cliclick in the resource folder using your code also but the curl script works 1000 times better and has totally blown my mind xD Thank you!
    – Jeremy
    Apr 2, 2015 at 22:36
  • Not knowing much about curl - is there a particular reason for the lag after sending the script? it's averaging at about 13second which feels rather weird :3 Is it due to me calling curl through applescript?
    – Jeremy
    Apr 2, 2015 at 23:02
  • It needs time to upload the image.
    – user309603
    Apr 3, 2015 at 0:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.