2014-05-20 102 views
1

使用AppleScript更改當前工作目錄(cwd)最簡單的方法是什麼?這將等同於操作系統的cd或Python的os.chdir。如果一個目標目錄不存在,會很快創建(但這是非常可選的)。如何使用AppleScript更改當前工作目錄

+0

[**執行的AppleScript腳本,Shell命令**](https://developer.apple.com/library/mac /documentation/applescript/conceptual/applescriptx/concepts/work_with_as.html#//apple_ref/doc/uid/TP40001568-1148121) –

回答

1

如果您正在尋找與應用程序使用保存對話框...

set filePath to "path/to/my/file" 

tell application "System Events" 
    -- Once save dialog is open 
    keystroke "g" using {shift down, command down} 
    delay 1 
    set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath 
end tell 
1

你可能會想這樣做這樣的事情的:

tell application "Finder" 
# ^^^ or whatever application you want to control up there 
    # to get to a file... 
    set filePath to POSIX file "/Users/username/Documents/new.mp3" 

    # to get to a folder... 
    set testFolder to POSIX path "/Users/username/test" 
    if (exists (folder testFolder)) then 
     say "folder exists" 
    else 
     make new folder at testFolder 
    endif 
end tell 

我立足我的答案關閉答案on this pagethis related question(或this one)。

+0

感謝您的建議!我正在使用AppleScript來啓動(運行/啓動)第三方應用程序(如Chrome或Firefox)。我必須使用AppleScript是有原因的。當應用程序啓動時,它將其「主目錄」設置爲其默認應用程序文件夾,如「/ Applications/Firefox.app/Contents」。因此,任何文件打開,文件保存都會在默認應用程序文件夾中打開。我想通過在應用程序啓動之前將當前工作目錄更改爲所選目錄來覆蓋此內容。我害怕'告訴應用程序「Finder」'不能用於這種情況。其他建議? – alphanumeric

相關問題