2017-02-06 7 views
0

Предполагаемый дублированный вопрос объясняет, как УДАЛИТЬ ФАЙЛ, но мне нужно СОЗДАЙТЕ ОДНУ (ИЛИ БОЛЬШЕ) НЕКСЕСТИТЕЛЬНЫЕ ДИРЕКТОРЫ совершенно другую задачу!Как создать путь к каталогу в Applescript?

В продолжение к моей предыдущей (решается) вопрос Can Applescript be used to tell whether or not a directory (path) exists?

теперь мне нужно знать, как создавать каталоги в пути, который уже не существует?

+0

Возможный дубликат [AppleScript путь набора каталогов в Finder] (http://stackoverflow.com/questions/16124232/applescript-set-directory-path-in-finder) –

+0

Возможный дубликат [Applescript для создания новой папки] (http://stackoverflow.com/questions/4493335/applescript-to-make-new-folder) –

ответ

0

Самый простой способ - использовать оболочку, mkdir -p создает папку только в том случае, если она не существует.

do shell script "mkdir -p ~/Desktop/TestFolder" 

Однако есть один нюанс: если есть пробел в пути вам нужно заменить все пространство с двумя обратными слэшами, так как обычный quoted of не расширяет тильды.

do shell script "mkdir -p ~/Desktop/Test\\ Folder" 

Альтернативно

set thePath to "~/Desktop/Test Folder ABC" 
if thePath starts with "~" then 
    set quotedPath to text 1 thru 2 of thePath & quoted form of (text 3 thru -1 of thePath) 
else 
    set quotedPath to quoted form of thePath 
end if 

do shell script "mkdir -p " & quotedPath 
-1

Добавить POSIX file перед путь, чтобы получить файл объекта:

tell application "Finder" 
    set f to POSIX file "/Users/username/Documents/new.mp3" 
    if exists f then delete f 
end tell 

system attribute "HOME" заменяется /Users/username:

set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3") 
tell application "Finder" to if exists f then delete f 

Или использовать предварительно OS формат X путь:

tell application "Finder" 
    set f to "Macintosh HD:Users:username:Documents:new.mp3" 
    -- set f to (path to documents folder as text) & "new.mp3" 
    if exists f then delete f 
end tell 

Bron: AppleScript set directory path in Finder