2014-05-20 53 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

相关问题