click in a script with 10.4

Hello

I am asked to help somebody which is running AppleWorks 6 in 10.4.11.
The problem is to drive the Print Dialog which in this system embed several objects of class AXUnknown.
More, this dialog doesn’t respond to click at {x,y} issued thru System Events.

Searching in the Internet I found several commands but they require at least 10.5.

I found this python script :

## The following is from TonyT ## http://hints.macworld.com/article.php?story=2008051406323031

import sys
import time
from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module

class AppleMouseEvents():
“”"
with thanks to:
TonyT http://hints.macworld.com/article.php?story=2008051406323031

 example:
m = AppleMouseEvents()
pos = m.currentPos()
m.mousedrag(pos.x,pos.y+float('30'))

"""
def __init__(self):
    self.relative = True
    
def mouseEvent(self,type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(self,posx,posy):
    self.mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(self,posx,posy):
    self.mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(self,posx,posy):
    self.mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(self,posx,posy):
    self.mouseEvent(kCGEventLeftMouseDragged, posx,posy);

def mouserclick(self,posx,posy):
    self.mouseEvent(kCGEventRightMouseDown, posx,posy);
    self.mouseEvent(kCGEventRightMouseUp, posx,posy);

def mousesingleclick(self,posx,posy):
    self.mouseclickdn(posx,posy)
    self.mouseclickup(posx,posy)

def mousedblclick(self,posx,posy):
    self.mousesingleclick(posx,posy)
    self.mousesingleclick(posx,posy)

def mousetrplclick(self,posx,posy):
    self.mousesingleclick(posx,posy)
    self.mousesingleclick(posx,posy)
    self.mousesingleclick(posx,posy)

def currentPos(self):
    ourEvent = CGEventCreate(None);	
    return CGEventGetLocation(ourEvent);	# Save current mouse position    

class AppleKeyboardEvents():
def init(self):
self.relative = True

class AppleWindowEvents():
def init(self):
self.relative = True

but
(1) I don’t know if Python is available in 10.4
(2) if my memory is right a specific task is required to bless such script but I forgot what it is.

Maybe it’s :
[i]
The first is done by executing “chmod +x scriptfile” or perhaps "chmod 755 scriptfile.

$ chmod +x myscript.py[/i]

May you help me ?

KOENIG Yvan (VALLAURIS, France) dimanche 25 août 2013 17:22:52

Hi Yvan.

I’m afraid I can’t help with much of your query, but I can confirm that Python is available in 10.4.11. Its “man” page is dated 2003/05/26.

Thanks Nigel.

I stored the python code on the Desktop as : “AppleUIEvents.py”

and apply the incantation given above.

I tried to use it under 10.8.4 to check the syntax with this script :


activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
	keystroke "p" using {command down}
	(*
Wait for the availability of the sheet *)
	repeat
		if exists sheet 1 of window 1 then exit repeat
	end repeat
	--name of menu buttons of sheet 1 of window 1
	--> {"PDF"} but I don't know if it's spelled this way worldwide
	set {{xMenuButton, yMenuButton}, {wMenuButton, hMenuButton}} to {position, size} of first menu button of sheet 1 of window 1
	set {pos_x, pos_y} to {xMenuButton - 2 + wMenuButton, yMenuButton + (hMenuButton div 2)}
end tell

set python_cmd to (path to desktop as text) & "AppleUIEvents.py"
set python_script to "m = AppleMouseEvents()
m.mousesingleclick(pos_x,pos_y)"
do shell script quoted form of POSIX path of python_cmd & " " & quoted form of python_script

Alas I got this infamous behavior :


	do shell script "'/Users/yvankoenig/Desktop/AppleUIEvents.py' 'm = AppleMouseEvents()
m.mousesingleclick(pos_x,pos_y)'"
		--> error "/Users/yvankoenig/Desktop/AppleUIEvents.py: line 3:  : command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 4: import: command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 5: import: command not found
from: can't read /var/mail/Quartz.CoreGraphics
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 7:  : command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 8: syntax error near unexpected token `('
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 8: `class AppleMouseEvents():'" number 2

And for sure, I understand nothing to it.

The more puzzling for me is :

can’t read /var/mail/Quartz.CoreGraphics

What introduced mail in this pathname in a script which doesn’t speak to this application ?

KOENIG Yvan (VALLAURIS, France) dimanche 25 août 2013 22:51:16

Hi Yvan,

I couldn’t find anything in the Python modules help section in the CoreGraphics module for mouse events. Anyway, this line:

set python_script to “m = AppleMouseEvents()
m.mousesingleclick(pos_x,pos_y)”

Aren’t you supposed to concatenate the variables’ values (pos_x, pos_y) to the string? And the same in the second script.

About the downloaded module, if you save AppleUIEvents.py into your home folder, then you can import it with:

import AppleUIEvents

Home folder is the default current directory. Also, you don’t need to make the module executable. Only if you’re running script statements at the root level. I’m still reviewing and learning more about this Python stuff.

gl,
kel

Thanks kel.

I will coerce the two values to strings but, as shown above, the script fails before using them.
Here is the edited instruction :

set {pos_x, pos_y} to {(xMenuButton - 2 + wMenuButton) as text, (yMenuButton + (hMenuButton div 2)) as text}

Alas, the result is always :

	do shell script "'/Users/yvankoenig/Desktop/AppleUIEvents.py' 'm = AppleMouseEvents()
m.mousesingleclick(pos_x,pos_y)'"
		--> error "/Users/yvankoenig/Desktop/AppleUIEvents.py: line 3:  : command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 4: import: command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 5: import: command not found
from: can't read /var/mail/Quartz.CoreGraphics
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 7:  : command not found
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 8: syntax error near unexpected token `('
/Users/yvankoenig/Desktop/AppleUIEvents.py: line 8: `class AppleMouseEvents():'" number 2

At this time, I’m just trying to get a script behaving well.
The location of AppleUIEvents.py isn’t a problem because I’m not the final user.
For my own needs, I have ASObjC Runner.

KOENIG Yvan (VALLAURIS, France) lundi 26 août 2013 13:41:50

Hi Yvan,

I’ve been doing some review and think I know how to write the do shell script, but it still might not work on my computer because I can’t find the mouse events in the help docs. I’ll try to do it without testing.

BRB,

Hi Yvan,

First I have a module on the desktop named MyClassTest.py:

I couldn’t get the do shell script to work with the module path so changed the current working directory:

set cmd to quoted form of "import os
os.chdir('/Users/kelhome/Desktop')
from MyClassTest import MyClass
m = MyClass()
print m.h(), m.b()"
do shell script "python -c " & cmd

→ “hello bye”

Not sure, but I think the ‘do shell script’ default current working directory is the root level of the hard disk.

In my script above, you need to change the path to the module, the class name, and function names.

Hope it works for you this way.

gl,
kel

Model: MBP
AppleScript: AS 2.2.4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)

Hi Yvan,

Good news. It works here in Mountain Lion at least! I ran this with AppleUIEvents.py on the desktop:

set cmd to quoted form of "import os
os.chdir('/Users/kelhome/Desktop')
import AppleUIEvents
mouseEvents = AppleUIEvents.AppleMouseEvents()
mouseEvents.mousemove(700, 500)"
do shell script "python -c " & cmd

Edited: note that maybe you should store the old current directory and reset it later in the script. Use:

oldDir = os.getcwd()

Then later,

os.chdir(oldDir)

gl,
kel

Thanks

I will try that soon.

At this time I’m busy on other tasks. Who said that when we are retired we have time to rest ?

KOENIG Yvan (VALLAURIS, France) mardi 27 août 2013 09:51:33

I put everything needed in this script:

set cmd to quoted form of "import os
oldDir = os.getcwd()
os.chdir('/Users/kelhome/Desktop')
import AppleUIEvents
os.chdir(oldDir)
mouseEvents = AppleUIEvents.AppleMouseEvents()
mouseEvents.mousemove(1000, 860)
print os.getcwd()		# just checking"
do shell script "python -c " & cmd

(* AppleUIEvents.py text
## The following is from TonyT
## http://hints.macworld.com/article.php?story=2008051406323031
 
import sys
import time
from Quartz.CoreGraphics import *    # imports all of the top-level symbols in the module
 
class AppleMouseEvents():
    """
     with thanks to:
     TonyT http://hints.macworld.com/article.php?story=2008051406323031
     
     example:
    m = AppleMouseEvents()
    pos = m.currentPos()
    m.mousedrag(pos.x,pos.y+float('30'))
    
    """
    def __init__(self):
        self.relative = True
        
    def mouseEvent(self,type, posx, posy):
        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, theEvent)
    def mousemove(self,posx,posy):
        self.mouseEvent(kCGEventMouseMoved, posx,posy);
    def mouseclickdn(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDown, posx,posy);
    def mouseclickup(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseUp, posx,posy);
    def mousedrag(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDragged, posx,posy);
    
    def mouserclick(self,posx,posy):
        self.mouseEvent(kCGEventRightMouseDown, posx,posy);
        self.mouseEvent(kCGEventRightMouseUp, posx,posy);
    
    def mousesingleclick(self,posx,posy):
        self.mouseclickdn(posx,posy)
        self.mouseclickup(posx,posy)
 
    def mousedblclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def mousetrplclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def currentPos(self):
        ourEvent = CGEventCreate(None);	
        return CGEventGetLocation(ourEvent);	# Save current mouse position    
 
class AppleKeyboardEvents():
    def __init__(self):
        self.relative = True
 
class AppleWindowEvents():
    def __init__(self):
        self.relative = True
*)

Don’t know if it works in Python 3

gl,
kel

Model: MBP
AppleScript: AS 2.2.4
Browser: Safari 536.28.10
Operating System: Mac OS X (10.8)

Thanks

Here is the code which run flawlessly under 10.8.4

activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
	keystroke "p" using {command down}
	(*
Wait for the availability of the sheet *)
	repeat
		if exists sheet 1 of window 1 then exit repeat
	end repeat
	--name of menu buttons of sheet 1 of window 1
	--> {"PDF"} but I don't know if it's spelled this way worldwide
	set {{xMenuButton, yMenuButton}, {wMenuButton, hMenuButton}} to {position, size} of first menu button of sheet 1 of window 1
end tell
set {pos_x, pos_y} to {(xMenuButton - 2 + wMenuButton) as text, (yMenuButton + (hMenuButton div 2)) as text}

set p2d to quoted form of POSIX path of (path to desktop)

set cmd to quoted form of ("import os
oldDir = os.getcwd()
os.chdir(" & p2d & ")
import AppleUIEvents
os.chdir(oldDir)
mouseEvents = AppleUIEvents.AppleMouseEvents()
mouseEvents.mousesingleclick(" & pos_x & ", " & pos_y & ")")
do shell script "python -c " & cmd

(* AppleUIEvents.py text
## The following is from TonyT
## http://hints.macworld.com/article.php?story=2008051406323031

import sys
import time
from Quartz.CoreGraphics import *    # imports all of the top-level symbols in the module
 
class AppleMouseEvents():
    """
     with thanks to:
     TonyT http://hints.macworld.com/article.php?story=2008051406323031
     
     example:
    m = AppleMouseEvents()
    pos = m.currentPos()
    m.mousedrag(pos.x,pos.y+float('30'))
    
    """
    def __init__(self):
        self.relative = True
        
    def mouseEvent(self,type, posx, posy):
        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, theEvent)
    def mousemove(self,posx,posy):
        self.mouseEvent(kCGEventMouseMoved, posx,posy);
    def mouseclickdn(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDown, posx,posy);
    def mouseclickup(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseUp, posx,posy);
    def mousedrag(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDragged, posx,posy);
    
    def mouserclick(self,posx,posy):
        self.mouseEvent(kCGEventRightMouseDown, posx,posy);
        self.mouseEvent(kCGEventRightMouseUp, posx,posy);
    
    def mousesingleclick(self,posx,posy):
        self.mouseclickdn(posx,posy)
        self.mouseclickup(posx,posy)
 
    def mousedblclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def mousetrplclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def currentPos(self):
        ourEvent = CGEventCreate(None);	
        return CGEventGetLocation(ourEvent);	# Save current mouse position    
 
class AppleKeyboardEvents():
    def __init__(self):
        self.relative = True
 
class AppleWindowEvents():
    def __init__(self):
        self.relative = True
*)

I was puzzled during several minutes until I understood that there was an illegal character in the third line of the AppleUIEvents.py file.
It was the character $C2.
I removed it and bingo, it works.

Now, I will ask the original asker for a screenshot of his print dialog.
This way I will be able to get the offsets of the menuButton and the offset of the PDF menu item in the AXUnknown object embedding them under 10.4.

KOENIG Yvan (VALLAURIS, France) mardi 27 août 2013 11:09:22

Hi Yvan,

FYI, I didn’t find any c2 character in my text whatever that means.

Edited: it looks like a french character maybe: Â

gl,
kel

In fact there were two characters :$C2 then $A0.
They were in the very first file which I copied from a web page.

I’m sure that it’s not a visible one because it didn’t appear when the code python was open in TextEdit.

I will check if TextEdit kept an old version to check one more time the file contents.

I’m a bit bored becaused I tried to solve the asker’s problem in a hurry and I learnt this afternoon that he will not be able to use it before 2013/11/01.

KOENIG Yvan (VALLAURIS, France) mardi 27 août 2013 21:30:11

Hi Yvan,

Strange but I didn’t find any character id 160 in my text also. Don’t know why you would get those characters. I was thinking about eliminating the the leading white space in the module and replacing with tabs. Python uses the leading white space and I think it’s 4 spaces for tab in this text downloaded from your link to the article. Need to write a program to replace 4 leading spaces with tabs. Wish I knew why you’re getting those characters.

gl,
kel

Hello

I returned to the web page where I got the code :

https://gist.github.com/willwade/5330566

The offending character is not embedded.

So, It was certainly introduced accidentally on my machine, maybe with a script targetting wrongly the file open in TextEdit.

KOENIG Yvan (VALLAURIS, France) mardi 27 août 2013 22:50:13

Hi Yvan,

Thanks you for checking that out. Strange how you got those characters though.

Good day,
kel

Hello

I was able to borrow an old PowerMac G4 running 10.4.11.
I checked that Python is available on it.

When I type Python in the Terminal I get :

Python 2.3.5 (#1, Jan 12 2009, 14:43:55)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1819)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

So I assume that it’s available.

I ran this script which is really dedicated to 10.4.11 because the Print Dialog has a different structure with more recent systems.


tell application "Finder" to set PrintLocal to localized string "N167"

tell application "AppleWorks 6" to activate
tell application "System Events" to tell process "AppleWorks 6"
	set frontmost to true
	keystroke "p" using {command down}
	repeat
		try
			if title of window 1 is PrintLocal then exit repeat
		end try
	end repeat
	tell window 1
		
		tell (second UI element whose role is "AXUnknown")
			set {{xGroup, yGroup}, {wGroup, hGroup}} to {position, size}
		end tell
	end tell -- window 1
end tell
set droiteMenuButton to 110
set {pos_x, pos_y} to {(xGroup + droiteMenuButton) as text, (yGroup + (hGroup div 2)) as text}

set p2u to quoted form of POSIX path of (path to utilities folder)

set cmd to quoted form of ("import os
oldDir = os.getcwd()
os.chdir(" & p2u & ")
import AppleUIEvents
os.chdir(oldDir)
mouseEvents = AppleUIEvents.AppleMouseEvents()
mouseEvents.mousesingleclick(" & pos_x & ", " & pos_y & ")")
do shell script "python -c " & cmd

(* AppleUIEvents.py text
## The following is from TonyT
## http://hints.macworld.com/article.php?story=2008051406323031

import sys
import time
from Quartz.CoreGraphics import *    # imports all of the top-level symbols in the module
 
class AppleMouseEvents():
    """
     with thanks to:
     TonyT http://hints.macworld.com/article.php?story=2008051406323031
     
     example:
    m = AppleMouseEvents()
    pos = m.currentPos()
    m.mousedrag(pos.x,pos.y+float('30'))
    
    """
    def __init__(self):
        self.relative = True
        
    def mouseEvent(self,type, posx, posy):
        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, theEvent)
    def mousemove(self,posx,posy):
        self.mouseEvent(kCGEventMouseMoved, posx,posy);
    def mouseclickdn(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDown, posx,posy);
    def mouseclickup(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseUp, posx,posy);
    def mousedrag(self,posx,posy):
        self.mouseEvent(kCGEventLeftMouseDragged, posx,posy);
    
    def mouserclick(self,posx,posy):
        self.mouseEvent(kCGEventRightMouseDown, posx,posy);
        self.mouseEvent(kCGEventRightMouseUp, posx,posy);
    
    def mousesingleclick(self,posx,posy):
        self.mouseclickdn(posx,posy)
        self.mouseclickup(posx,posy)
 
    def mousedblclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def mousetrplclick(self,posx,posy):
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
        self.mousesingleclick(posx,posy)
    
    def currentPos(self):
        ourEvent = CGEventCreate(None);	
        return CGEventGetLocation(ourEvent);	# Save current mouse position    
 
class AppleKeyboardEvents():
    def __init__(self):
        self.relative = True
 
class AppleWindowEvents():
    def __init__(self):
        self.relative = True
*)

Alas, I get this log report :

tell application “Finder”
localized string “N167”
“Imprimer”
end tell
tell application “AppleWorks 6”
activate
end tell
tell application “System Events”
set frontmost of process “AppleWorks 6” to true
keystroke “p” using {command down}
get title of window 1 of process “AppleWorks 6”
“Barre de boutons”
get title of window 1 of process “AppleWorks 6”
“Imprimer”
get position of UI element 2 of window 1 of process “AppleWorks 6” whose role = “AXUnknown”
{671, 400}
get size of UI element 2 of window 1 of process “AppleWorks 6” whose role = “AXUnknown”
{573, 61}
end tell
tell current application
path to utilities folder
→ alias “Macintosh HD:Applications:Utilities:”
do shell script “python -c ‘import os
oldDir = os.getcwd()
os.chdir(’\‘’/Applications/Utilities/‘\’‘)
import AppleUIEvents
os.chdir(oldDir)
mouseEvents = AppleUIEvents.AppleMouseEvents()
mouseEvents.mousesingleclick(781, 430)’”
“Traceback (most recent call last):
File "", line 4, in ?
ImportError: No module named AppleUIEvents”

I set the error message in bold.
I moved the Python Script to the folder /Applications/Utilities so that it doesn’t pollute the asker’s Desktop without requiring him to create a new folder.
The behavior doesn’t change according to the location.

I know that I ask for a lot of work but I will be glad if somebody was able to debug that.

I repeat that it’s not to fit my own needs. My own AppleWorks files are converted since several years.

KOENIG Yvan (VALLAURIS, France) dimanche 1 septembre 2013 16:16:36

Hi Yvan,

I wrote a test python module in TextEdit and tried to save it to the Utilities folder. I had an error that was something like can’t save untitled as HW.py to Utilities: you don’t have permission. So, I’m wondering if maybe there’s a permission thing with the utilities folder. The Utilities folder works differently here in 10.8. So that might be causing the permission problem.

What happens if you place the AppleUIEvents module in the users home folder?

It’s hard to test things because my g4 doesn’t work and I can’t remember trying to save to the Applications folder.

Edited: wonder where HASS is? The problem probably would be solved by now.

Edited: HHAS.

Later,
kel

Hi Yvan,

I don’t think this will do anything, but can you try running this on the g4:

set cmd to quoted form of "import os
os.chdir('/Applications/Utilities')
print os.getcwd()"
do shell script "python -c " & cmd

What is the result

Writing in Applications folder under 10.4 isn’t a problem. I checked that with a piece of code creating a subfolder in Utilities and an other one writing a text file.

I don’t understand what is HHAS

KOENIG Yvan (VALLAURIS, France) lundi 2 septembre 2013 14:28:10