2013-05-16 2 views
2

나는이 작업을 수행 유래에 애플 스크립트 here의 조각을 발견OS X를 : 파이썬

OS X의 파이썬에서 프로그래밍 방식으로 창을 이동하려고 해요에서 창을 이동,하지만 난 그것을 할 싶습니다 파이썬 또는 다른 "진짜"스크립팅 언어.

이것은 작동하지 않는 제 Python 스크립트입니다. 나는 그들 각각의 아래에 print 명령의 출력을 썼다.

#!/usr/bin/python 

from Foundation import * 
from ScriptingBridge import * 

app = SBApplication.applicationWithBundleIdentifier_("com.apple.SystemEvents") 

finderProc = app.processes().objectWithName_("Finder") 

print finderProc 
# <SystemEventsProcess @0x74b641f0: SystemEventsProcess "Finder" of application "System Events" (29683)> 

finderWin = finderProc.windows()[0] 

print finderWin 
# <SystemEventsWindow @0x74b670e0: SystemEventsWindow 0 of SystemEventsProcess "Finder" of application "System Events" (29683)> 

print finderWin.name() 
# Macintosh HD 

finderWin.setBounds_([[20,20],[100,100]]) 
# no visible result 

finderWin.setPosition_([20,20]) 

마지막 명령 (setPosition_는) 다음과 같은 예외와 충돌합니다.

Traceback (most recent call last): 
    File "/Users/mw/Projekte/Python/winlist.py", line 17, in <module> 
    finderWin.setPosition_([20,20]) 
AttributeError: 'SystemEventsWindow' object has no attribute 'setPosition_' 

어떻게 setBounds 명령을 사용할 수 있습니까?

+0

이것은 작동하는 AppleScript입니다 :''tell application "System Events"; 응용 프로그램 프로세스 "Finder"의 첫 번째 창 위치를 {20, 20}으로 설정 ; end tell ''' –

+0

파이썬 코드로하고 싶습니까? osascript와 do와 os.system 호출을 사용하지 않는 이유는 무엇입니까? – maranas

+0

하나의 특정 창을 사용하고 싶지 않고 사용자가 창을 선택할 수있는 목록을 표시하기 때문에. 파이썬에서 직접 할 수 있었다면 아마 더 쉬울 것입니다. –

답변

2

파이썬에서 OS X의 접근성 API와 상호 작용하려면 atomac을 시도하십시오. 시스템 이벤트는 다양한 시스템 API를 둘러싼 AppleScriptable 래퍼 일 뿐이지 만 PyObjC 및 다른 Python 라이브러리는 이미 AS/SB 말도 안 해석 할 필요없이 시스템 API에 대한 광범위한 액세스 권한을 제공합니다.

-

p.s 당신은 다른 대부분의 접근성 기능을 사용할 수 없습니다, 시스템 환경 설정 '접근성 창에서'보조 장치 '옵션을 활성화해야 할 수도 있습니다.

+0

마지막으로 액세스 가능성 API를 사용하여 가져 왔지만 기본 Objective C로 전환하고 Apple 제공 예제 ([UIElementInspector] (https://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/)를 수정했습니다. Intro.html)). –

+0

atomac은 현재 python3에서 작동하지 않습니다 : https://github.com/pyatom/pyatom/issues/141 –

3

시스템 이벤트를 통해이를 수행 할 필요는 없습니다 (작동하지 않을 수도 있습니다). 대신 Finder 앱에서 직접 수행하십시오.

from ScriptingBridge import * 

app = SBApplication.applicationWithBundleIdentifier_("com.apple.Finder") 
finderWin = app.windows()[0] 
finderWin.setBounds_([[100,100],[100,100]]) 
finderWin.setPosition_([20,20]) 

Foundation 가져 오기가 필요하지 않습니다.

+0

스크립트를 지원하는 대상 응용 프로그램에 의존하기 때문에이 작업을 수행 할 수 없습니다. 모든 응용 프로그램에서 작동해야합니다. –

관련 문제