alphanumeric
alphanumeric

Reputation: 19369

How to change Current Working directory using AppleScript

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).

Upvotes: 2

Views: 3894

Answers (2)

adayzdone
adayzdone

Reputation: 11238

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

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89559

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).

Upvotes: 1

Related Questions