2012-07-23 178 views
1

Im使用系統事件來控制沒有applescript庫的程序。 我爲此使用系統事件來控制它。Applescript - 系統事件 - 爲「打開文件」彈出窗口設置默認位置

我已經得到了程序打開一個彈出窗口爲它打開文件界面,我想將它默認到某個位置。這可能嗎。 到目前爲止,我有:

tell application "App Name" 
activate 
end tell 
tell application "System Events" 
tell process "App Name" 
    tell menu bar 1 
     tell menu bar item "File" 
      tell menu "File" 
       tell menu item "Import" 
        tell menu "Import" 
         click menu item "XML..." 
         delay 4 

        end tell 
       end tell 
      end tell 
     end tell 
    end tell 
end tell 
end tell 

的彈出窗口默認爲它自己的上次訪問的位置。我想它默認爲一個給定的文件路徑類似/Users/userabc/Documents/abcd.XML

感謝

回答

2

如果你有一個位置的「POSIX路徑」,對話框打開,可以請執行下列操作。請注意,位置可以是文件夾或文件路徑。如果它是文件路徑,那麼該文件將被選中,然後您只需「擊鍵返回」即可關閉對話框並打開該文件。祝你好運。

set theLocation to path to home folder 
set posixLocation to POSIX path of theLocation 

tell application "System Events" 
    keystroke "g" using {command down, shift down} 
    delay 0.5 
    keystroke posixLocation 
    delay 0.5 
    keystroke return 
end tell 
+0

尼斯,作品一種享受!我是否也可以提示輸入默認的文件名以及文件路徑? – user1540142 2012-07-23 11:09:51

0

此方法的唯一問題是自動更正開始填充文本框中的蘋果腳本類型並將所有東西都擰緊。解決方法是從applescript複製/粘貼。

0

keystroke命令不適用於插入無法用當前輸入源插入的字符。對於一些輸入源,它根本不起作用。

您還可以設置文本字段的值:

tell application "System Events" to tell (process 1 where frontmost is true) 
    keystroke "g" using {shift down, command down} 
    tell window 1 
     tell sheet 1 
      set value of text field 1 to "/usr/share/dict/connectives" 
      click button 1 
     end tell 
     click button "Open" 
    end tell 
end tell