Pass parameters from apple script to python to perform click operation

Hi,

I want to pass parameters to python script from apple script that i took using UIElements inspector to perform clicking on specific position (x,y) where i found my app's notification title.I could list titles and verify my own apps's then save its x,y position in a variable.Now i want to pass these variables in python script to click.I found this script to perform click at position:(1136,120).

(Used python script to click as unable to click using click command in apple script)

I want x and y parameters to set here coming from my apple script.

set x to 1136 as integer

set y to 120 as integer


how can i pass it here in python script??


-------------------------------------------------------------------------------- --------------------------

tell application "System Events"


tell process "Notification Center"


do shell script "

python <<-END

import sys

import time

from Quartz.CoreGraphics import *


def mouseEvent(type, posx, posy):

theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)

CGEventPost(kCGHIDEventTap, theEvent)

def mousemove(posx,posy):

mouseEvent(kCGEventMouseMoved, posx,posy);

def mouseclick(posx,posy):

mouseEvent(kCGEventLeftMouseDown, posx,posy);

mouseEvent(kCGEventLeftMouseUp, posx,posy);

time.sleep(2);

ourEvent = CGEventCreate(None);

currentpos=CGEventGetLocation(ourEvent); # Save current mouse position

mouseclick(1136, 120); --want to replace with mouseclick(x,y);

time.sleep(2);

mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position

END

"


end tell


end tell


-------------------------------------------------------------------------------- --------------------------


if i am using this one:

do shell script "

python <<-END" & x & y &"

import......

then it gives error.

plz help me.

MacBook Pro, macOS High Sierra (10.13.6)

Posted on Nov 3, 2018 11:36 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 4, 2018 10:00 AM

You need an AppleScript handler that incorporates a Python HERE script. You pass arguments to that HERE script's command-line. Python is indent sensitive, and its statements use no termination character (e.g. ';'). So, it would be wise to edit/debug your Python code in an external programmer's editor that understands Python indent, and then paste back into the AppleScript handler. AppleScript is clueless about Python, and if you attempt to edit your Python code in the handler, you will get an explosion when you run it.


Here is the revised AppleScript with its handler incorporating the Python HERE script. Any double-quotes, or \ character in Python must be escaped as \" and \\. I used a scrollable content window to preserve the Python HERE script indent from the hosting software.

set x to 1136 as integer set y to 120 as integer # pass to Python as quoted text items that get converted to integer set argvec to (x as text)'s quoted form & space & (y as text)'s quoted form my mouse_click(argvec) return on mouse_click(args) return do shell script "python <<EOF - " & args & " #!/usr/bin/python # coding: utf-8 import sys import time from Quartz.CoreGraphics import * # https://developer.apple.com/documentation/coregraphics/1454356-cgeventcreatemouseevent?language=objc def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx, posy), kCGMouseButtonLeft) CGEventPost(kCGHIDEventTap, theEvent) def mousemove(posx, posy): mouseEvent(kCGEventMouseMoved, posx, posy) def mouseclick(posx, posy): mouseEvent(kCGEventLeftMouseDown, posx, posy) mouseEvent(kCGEventLeftMouseUp, posx, posy) # pluck x and y from command-line as integers x, y = [int(z) for z in sys.argv[1:]] time.sleep(2) ourEvent = CGEventCreate(None) currentpos = CGEventGetLocation(ourEvent) # Save current mouse position mouseclick(x, y) # want to replace with mouseclick(x,y) time.sleep(2) mousemove(int(currentpos.x), int(currentpos.y)) # Restore mouse position ort time from Quartz.CoreGraphics import * def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx, posy), kCGMouseButtonLeft) CGEventPost(kCGHIDEventTap, theEvent) def mousemove(posx, posy): mouseEvent(kCGEventMouseMoved, posx, posy) def mouseclick(posx, posy): mouseEvent(kCGEventLeftMouseDown, posx, posy) mouseEvent(kCGEventLeftMouseUp, posx, posy) # pluck x and y from command-line as integers x, y = [int(z) for z in sys.argv[1:]] time.sleep(2) ourEvent = CGEventCreate(None) currentpos = CGEventGetLocation(ourEvent) # Save current mouse position mouseclick(x, y) # want to replace with mouseclick(x,y) time.sleep(2) mousemove(int(currentpos.x), int(currentpos.y)) # Restore mouse position EOF" end mouse_click

1 reply
Question marked as Top-ranking reply

Nov 4, 2018 10:00 AM in response to manisaga

You need an AppleScript handler that incorporates a Python HERE script. You pass arguments to that HERE script's command-line. Python is indent sensitive, and its statements use no termination character (e.g. ';'). So, it would be wise to edit/debug your Python code in an external programmer's editor that understands Python indent, and then paste back into the AppleScript handler. AppleScript is clueless about Python, and if you attempt to edit your Python code in the handler, you will get an explosion when you run it.


Here is the revised AppleScript with its handler incorporating the Python HERE script. Any double-quotes, or \ character in Python must be escaped as \" and \\. I used a scrollable content window to preserve the Python HERE script indent from the hosting software.

set x to 1136 as integer set y to 120 as integer # pass to Python as quoted text items that get converted to integer set argvec to (x as text)'s quoted form & space & (y as text)'s quoted form my mouse_click(argvec) return on mouse_click(args) return do shell script "python <<EOF - " & args & " #!/usr/bin/python # coding: utf-8 import sys import time from Quartz.CoreGraphics import * # https://developer.apple.com/documentation/coregraphics/1454356-cgeventcreatemouseevent?language=objc def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx, posy), kCGMouseButtonLeft) CGEventPost(kCGHIDEventTap, theEvent) def mousemove(posx, posy): mouseEvent(kCGEventMouseMoved, posx, posy) def mouseclick(posx, posy): mouseEvent(kCGEventLeftMouseDown, posx, posy) mouseEvent(kCGEventLeftMouseUp, posx, posy) # pluck x and y from command-line as integers x, y = [int(z) for z in sys.argv[1:]] time.sleep(2) ourEvent = CGEventCreate(None) currentpos = CGEventGetLocation(ourEvent) # Save current mouse position mouseclick(x, y) # want to replace with mouseclick(x,y) time.sleep(2) mousemove(int(currentpos.x), int(currentpos.y)) # Restore mouse position ort time from Quartz.CoreGraphics import * def mouseEvent(type, posx, posy): theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx, posy), kCGMouseButtonLeft) CGEventPost(kCGHIDEventTap, theEvent) def mousemove(posx, posy): mouseEvent(kCGEventMouseMoved, posx, posy) def mouseclick(posx, posy): mouseEvent(kCGEventLeftMouseDown, posx, posy) mouseEvent(kCGEventLeftMouseUp, posx, posy) # pluck x and y from command-line as integers x, y = [int(z) for z in sys.argv[1:]] time.sleep(2) ourEvent = CGEventCreate(None) currentpos = CGEventGetLocation(ourEvent) # Save current mouse position mouseclick(x, y) # want to replace with mouseclick(x,y) time.sleep(2) mousemove(int(currentpos.x), int(currentpos.y)) # Restore mouse position EOF" end mouse_click

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Pass parameters from apple script to python to perform click operation

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.