我正在使用 AppleScript 在 GUI 上自動化一個程序。
我使用腳本在 GUI 中單擊“添加源檔案夾”按鈕(類似于在任何應用程式中打開檔案),這會提示一個集成的 Finder 視窗(此處為影像)。
在實踐中,一旦提示的查找器視窗打開,我希望從別名物件theFolderToProcess(之前在腳本中設定)自動設定路徑。
我無法設定其路徑,因為我無處可theFolderToProcess在應用程式內提示查找器視窗中設定別名。那么如何讓腳本導航到別名的路徑呢?
這是代碼:
set theFolderToProcess to choose folder with prompt "Step 1: Select the folder to convert:"
tell application "System Events"
tell application process "MyApp"
tell its window "MyApp"
activate
click button "Add Source Folder"
set uiElems to UI elements
end tell
end tell
end tell
使用UI Elements,我知道提示視窗sheet 1 of window "MyApp" of application process "MyApp" of application "System Events"在哪里sheet 1。
注意:在提示視窗中設定路徑不起作用。
uj5u.com熱心網友回復:
在“打開”對話框中,您應該能夠使用 Finder 的Go to Folder…功能,或command-shift-G(或您作業系統上的任何功能)。然后使用keystroke輸入您想要的位置。位置參考應遵循以下格式,而不是使用您的別名。跟進key code以接受您的位置。我發現短延遲有助于鍵盤腳本撰寫,但可以根據需要進行編輯。
set posixFolder to posix path of theFolderToProcess
--> "~/Desktop/exports/"
delay 0.1
key code 5 using {command down, shift down} -- Go to folder…
delay 0.1
keystroke posixFolder -- folder must exist
--> keystroke "~/Desktop/exports/" -- folder must exist
delay 0.1
key code 76 -- type Return (Go button)
這里有一個完整的例子,但這應該足夠了。
uj5u.com熱心網友回復:
不知道您的“應用程式”,因此以下示例使用 TextEdit。如果您使用其他方式成功打開 OPEN 對話框(單擊按鈕“添加源檔案夾”...),則將我腳本的相應代碼行替換為您的代碼行。我的腳本也展示了如何設定自動延遲。以及如何點擊最后的“開始”按鈕。
set processName to "TexEdit"
set sourceFolder to POSIX path of (path to pictures folder)
tell application processName to activate -- bring to front
tell application "System Events"
-- this is instead of your 'click button "Add Source Folder"'
-- it opens OPEN dialog window
keystroke "o" using command down
-- wait
repeat until window "Open" of process processName exists
delay 0.02
end repeat
-- open go to path sheet
keystroke "g" using {command down, shift down}
repeat until sheet 1 of window "Open" of process processName exists
delay 0.02
end repeat
-- keystroke full posix path
keystroke sourceFolder
-- go
click button "Go" of sheet 1 of window "Open" of process processName
end tell
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/317133.html
下一篇:在PyQt5中取消QThread
