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: