click in a script with 10.4

I ran a script embedding the same code with two syntax :

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

set p2u to quoted form of POSIX path of (path to utilities folder)
set cmd to quoted form of ("import os
os.chdir(" & p2u & ")
print os.getcwd()")
do shell script "python -c " & cmd

The log report is :

tell current application
do shell script “python -c ‘import os
os.chdir(’\‘’/Applications/Utilities’\‘’)
print os.getcwd()'”
→ “/Applications/Utilities”
path to utilities folder
→ alias “Macintosh HD:Applications:Utilities:”
do shell script “python -c ‘import os
os.chdir(’\‘’/Applications/Utilities/‘\’‘)
print os.getcwd()’”
→ “/Applications/Utilities”
end tell

I tried also this edited version :


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)")")
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
*)

which is supposed to load the python file and it fails exactly like the full one :

“Traceback (most recent call last):
File "", line 4, in ?
ImportError: No module named AppleUIEvents”

Seeing that, I thought : I’m a fool, I gave a wrong name to the file so I used my good old script returning the entire pathname of the submitted file icon and I got :

Macintosh HD:Applications:Utilities:AppleUIEvents.pyc

I made an ultimate test with this subscript :


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)")
do shell script "python -c " & cmd

It behaves flawlessly under 10.8.4 but fails with the infamous error message under 10.4.11 :

“Traceback (most recent call last):
File "", line 4, in ?
ImportError: No module named AppleUIEvents”

It’s clear that it’s always the instruction “import AppleUIEvents” which fails.

As I wrote yesterdays, I get the same behaviour when the pyc file is stored on the Desktop.
It’s to be able to test the two locations that I choose to define the UNIX path of the folder in a variable so I’m sure that I don’t fail to edit one instruction using it.

KOENIG Yvan (VALLAURIS, France) lundi 2 septembre 2013 15:03:15

PS : It’s funny to see how quickly system’s new features are quickly integrated by the brain.
When testing on the G4, I was always trying to navigate in the script as I does with the trackpad connected to my iMac. :rolleyes:

Hi Yvan,

Instead of changing the current working directory, let’s try changing the modules search path. Try this:

do shell script "python -c '
import sys
sys.path.append(\"/Applications/Utilities\")
import AppleUIElements'
"

where AppleUIElements.py is in the Utilities folder. Also, trash the .pyc file first and a new one will be created after you import the AppleUIElements module.

Edited: note that changing the search path is only temporary here.

Edited: HHAS is the Python guru who used to post here a lot.

Good Luck,
kel

Thanks kel1

As my file is named AppleUIEvents.py,

I tried to run your late suggestion:

do shell script "python -c '
import sys
sys.path.append(\"/Applications/Utilities\")
import AppleUIEvents'
"

(*
-- log report
tell current application
	do shell script "python -c '
import sys
sys.path.append(\"/Applications/Utilities\")
import AppleUIEvents'
"
		"Traceback (most recent call last):
  File \"<string>\", line 4, in ?
  File \"/Applications/Utilities/AppleUIEvents.py\", line 8
    class AppleMouseEvents():
                           ^
SyntaxError: invalid syntax"

*)

I ran also a version defining the folder path in a variable and got :

set p2u to quoted form of POSIX path of (path to utilities folder)
set cmd to quoted form of ("
import sys
sys.path.append(" & p2u & ")
import AppleUIEvents
")
do shell script "python -c " & cmd

(*
log report
tell current application
	path to utilities folder
		alias "Pour_jouer:Applications:Utilities:"
	do shell script "python -c '
import sys
sys.path.append('\\''/Applications/Utilities/'\\'')
import AppleUIEvents
'"
		"Traceback (most recent call last):
  File \"<string>\", line 4, in ?
  File \"/Applications/Utilities/AppleUIEvents.py\", line 8
    class AppleMouseEvents():
                           ^
SyntaxError: invalid syntax"
*)

KOENIG Yvan (VALLAURIS, France) mardi 3 septembre 2013 10:56:26

Hi Yvan,

At least we have the script trying to import the module now.

I’ve seen in some old posts where they define it without the parenthesis. like:

class AppleMouseEvents:

I wonder if they changed things. I’ll do some experimenting. Hope Python2.4 has the other modules.

Edited: in the line before the class definition, try deleting the space. There’s one space.

Edited: Hi Yvan, I’m don’t know what this does, but read it on the internet. Try:

gl,
kel

Hi Yvan,

Just a long shot, but could there be carriage returns instead of linefeeds in the Python module?

gl,
kel

Hello Kel1

I will be without the G4 for few days.

The script behave flawlessly under 10.8.4 but fails under 10.4.11.
So, I assumes that :
(1) the problem is not a linebreak one
(2) it’s not really a problem with the syntax.

I’m afraid that the required tool ” Quartz.CoreGraphics ” is not available.

KOENIG Yvan (VALLAURIS, France) mercredi 4 septembre 2013 09:14:28

The CGEventCreateMouseEvent function was introduced in 10.4, according to the docs (which are usually reliable about this sort of thing). However, it’s not part of Quartz.CoreGraphics – it’s in ApplicationServices.CoreGraphics. It’s entirely possible that CoreGraphics was moved frameworks at some stage. In fact, I suspect was.

Whether this explains things, I don’t know…

Thanks Shane

I will test that when the PowerMac G4 will be back.

KOENIG Yvan (VALLAURIS, France) mercredi 4 septembre 2013 12:01:58

Hi Yvan,

Did you go to (Terminal > python > help > modules) to find out that Quartz.CoreGraphics is not there? I was just wondering if you know how to check out the modules.

gl,
kel

I got a message out of this thread which does the trick calling explicitely Quartz.CoreGraphics and got the error :

“Traceback (most recent call last):
File "", line 2, in ?
ImportError: No module named Quartz.CoreGraphics”

I’m trying to get help for an other scheme : a mouseClick command written in C.

If StefanK read this thread maybe he may be helpful.

KOENIG Yvan (VALLAURIS, France) mercredi 4 septembre 2013 19:34:24

Hi Yvan,

In Terminal type python to enter interactive mode (>>>). Then type help(). Then type modules. This gives you a listing of most modules. When you get the G4 back, type ApplicationServices.CoreGraphics to find out if there is such a module. As Shane said it does sound like they changed the name.

gl,
kel