2012-02-24 68 views
4

我一直在尋找試圖找出如何執行此腳本的留言板。實質上,目標是能夠在文件夾內運行此腳本,並且如果某些文件夾不存在,則會創建這些文件夾。如果它們已經存在,則什麼都不會發生。這是我迄今拼湊在一起的東西:AppleScript:如果不存在新的文件夾結構

property archivesFolder : "Archives" 

property imagesFolder : "Images" 

property proofreadFolder : "Proofreading" 

property proofFolder : "Proofs" 

property sourceFolder : "Source" 

try 

tell application "Finder" to set theLocation to (folder of the front window as alias) 

end try 

tell application "Finder" 

    if (exists folder archivesFolder) then 
     (* do nothing *) 
    else 
     make new folder at theLocation with properties {name:archivesFolder} 
    end if 

    if (exists folder imagesFolder) then 
     (* do nothing *) 
    else 
     make new folder at theLocation with properties {name:imagesFolder} 
    end if 

    if (exists folder proofreadFolder) then 
     (* do nothing *) 
    else 
     make new folder at theLocation with properties {name:proofreadFolder} 
    end if 

    if (exists folder proofFolder) then 
     (* do nothing *) 
    else 
     make new folder at theLocation with properties {name:proofFolder} 
    end if 

    if (exists folder sourceFolder) then 
     (* do nothing *) 
    else 
     make new folder at theLocation with properties {name:sourceFolder} 
    end if 

end tell 

我在做什麼錯? (原諒我的n00b代碼格式,在工作中,無法弄清楚如何創建代碼塊)另外,這可能不僅僅是在前窗上,而是在剛剛選擇的文件夾上?任何幫助都會很棒。

回答

3

我建議兩個選項(運行腳本):

選項1:以代碼(假設它確實你打算什麼),並將其保存爲一個應用程序(使用腳本編輯器)。然後,只需將該應用程序拖到窗口工具欄(您需要使工具欄可見)。爲此,請在拖動時按住命令鍵。

選項2:使用巴特勒:http://manytricks.com/butler/ (有一個免費版本,我不知道你的OSX版本)。

它允許您爲applescript腳本定義全系統快捷鍵。

創建一個智能項目(applescript);將代碼粘貼在那裏,並在腳本的名稱添加快捷鍵:例如:create folder here ⇧⌥⌘N

編輯:

根據您的意見,我明白你的問題,我可以告訴你,你人失蹤路徑(當前文件夾,你的情況theLocation

所以,在每if (exists folder archivesFolder) then情況下,你需要添加of theLocation這樣的:if not (exists folder archivesFolder of theLocation) then

最後,知道日如果文件夾存在,你不會做任何事情,只要測試假的情況。

我測試此代碼,我在這裏張貼:

property archivesFolder : "Archives" 

property imagesFolder : "Images" 

property proofreadFolder : "Proofreading" 

property proofFolder : "Proofs" 

property sourceFolder : "Source" 

try 

    tell application "Finder" to set theLocation to (folder of the front window as alias) 

end try 

tell application "Finder" 

    if not (exists folder archivesFolder of theLocation) then 
     make new folder at theLocation with properties {name:archivesFolder} 
    end if 

    if not (exists folder imagesFolder of theLocation) then 
     make new folder at theLocation with properties {name:imagesFolder} 
    end if 

    if not (exists folder proofreadFolder of theLocation) then 
     make new folder at theLocation with properties {name:proofreadFolder} 
    end if 

    if not (exists folder proofFolder of theLocation) then 
     make new folder at theLocation with properties {name:proofFolder} 
    end if 

    if not (exists folder sourceFolder of theLocation) then 
     make new folder at theLocation with properties {name:sourceFolder} 
    end if 

end tell 
+0

我實際上已將它設置爲選項1,並且如果文件夾中沒有任何內容,它工作正常。但是,如果我刪除這些文件夾中的一個,它會破壞代碼,它什麼也不做。我希望它能夠在不是所有文件夾都存在的情況下工作,但有些可能已經存在(這取決於誰設置了原始文件夾)。 – user1231499 2012-02-24 19:50:29

+0

@ user1231499在這裏你走!如果這爲你工作,請批准答案。 – jackJoe 2012-02-24 20:11:44

+0

就像魔術一樣,它很有效。我認爲每種情況都可能存在某種路徑,但我認爲我只是嘗試了一些東西,比如在「位置」處添加「並停止工作」。非常感謝,這將是偉大的! 實際上,不會有這樣的設置方式,如果你只是簡單地選擇文件夾就會運行,或者只有當文件夾位於打開窗口時才真正可行? – user1231499 2012-02-24 20:54:45

4

您還可以使用的shell腳本mkdir,因爲選項來創建將文件夾已經不存在中間錯誤的文件夾。

# define a list of folders - items will need to be quoted if they contain spaces, etc. 
property theFolders : {"Archives", "Images", "ProofReading", "Proofs", "Source"} -- can also nest, e.g. "Inside/One" 

try 
    tell application "Finder" to set targetFolder to (target of the front window) as alias 
on error -- no window 
    set targetFolder to (choose folder) 
end try 

# build a parameter string from the folder list 
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space} 
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID} 

do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders 
0

試試這個:

tell application "Finder" 
set theLocation to (target of front window) as string 
set folderNames to {"archivesFolder", "imagesFolder", "proofreadFolder", "proofFolder", "sourceFolder"} 
repeat with theFolder in folderNames 
    try 
     make new folder at theLocation with properties {name:"" & theFolder & ""} 
    end try 
    end repeat 
end tell 
0

這是一個腳本,我做了排序基於服務的日期一堆醫療檔案到子文件夾(在腳本解釋「文本5至圖14」 ;這些文件是根據模式命名的,因此腳本可以從文件名中提取服務的日期)。而不是檢查文件夾是否已經存在,我只是把make文件夾指令放在一個try塊中;如果該文件夾已經存在,則'嘗試'失敗,但腳本在該文件夾已存在的假設下繼續。使用「文本」項而不是「字符」將提取的字符串作爲單個字符串返回,而不是作爲必須轉換回字符串的字符數組。

tell application "Finder" 
set theDirectory to "Internal Disk:Users:steven:Documents:Vondalee:Medical" 
set theFiles to every file in folder theDirectory whose name extension is "pdf" 
repeat with eachFile in theFiles 
    set theFileName to the name of eachFile 
    set theFolder to text 5 thru 14 of theFileName 
    try 
     make new folder at theDirectory with properties {name:theFolder} 
    end try 
    move eachFile to folder theFolder of folder theDirectory 
end repeat 
end tell