2

What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).

alphanumeric
  • 13,847
  • 39
  • 164
  • 305
  • [**Executing AppleScript Scripts as Shell Commands**](https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptx/concepts/work_with_as.html#//apple_ref/doc/uid/TP40001568-1148121) – Jonathon Reinhart May 20 '14 at 05:53

2 Answers2

1

You might be thinking of doing something like this:

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

I'm basing my answer off the answers on this page and this related question (or this one).

Community
  • 1
  • 1
Michael Dautermann
  • 86,557
  • 17
  • 155
  • 196
  • Thanks for the suggestion! I am using AppleScript to start (to run/to launch) a third-party application (such as Chrome or Firefox). There is a reason why I have to use AppleScript. When application starts it sets its "home directory" to its default app folder such as `/Applications/Firefox.app/Contents`. So any File-Open, File-Save opens in a default application folder. I would like to override this by changing current working directory to a directory of choice before an application starts. I'm afraid `tell application "Finder"` can't be used in such situation. Other suggestions? – alphanumeric May 20 '14 at 06:19
1

If you are looking to use with an applications save dialog...

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
adayzdone
  • 10,652
  • 2
  • 17
  • 35