2009-06-25 51 views
1

我有一個文件夾充滿了圖像,我需要創建一個帶有applescript的所有圖像名稱的文本文件。有沒有什麼方法可以用Applescript讀取所有文件名中約有10k的文件名,然後將其輸出到文本文件中?任何幫助將是偉大的!謝謝閱讀。Mac用戶需要一些與applescript文件夾的幫助。 SFW

+4

安全工作?我想這是... – 2009-06-25 23:21:02

回答

5

爲什麼不從終端做起呢。

LS> pix.txt

1

下面的AppleScript將寫入文件的名稱的文件夾中的一個文本文件:

property theFolder : "File:Path:To:theFolder:" 

tell application "Finder" 

    -- Create text file on desktop to write filenames to 
    make new file at desktop with properties {name:"theFile.txt"} 
    set theFile to the result as alias 
    set openFile to open for access theFile with write permission 

    -- Read file names and write to text file 
    set theFiles to every item of folder theFolder 
    repeat with i in theFiles 
     set fileName to name of i 
     write fileName & " 
" to openFile starting at eof 
    end repeat 

    close access openFile 

end tell 
1

你不需要打開它之前創建一個文件訪問。你可以做

set theFile to (theFolder & "thefile.txt")as string 
set openFile to open for access theFile with write permission 

當然,如果文件存在,它會覆蓋它。你可以使用

set thefile to choose file name with prompt "name the output file" 

「選擇文件名」返回的路徑,而無需創建一個文件,並詢問用戶是否要覆蓋該文件是否存在。

您還可以使用「迴歸」放線打破像這樣,它使代碼有點整潔:

write fileName & return to openFile 

當然,如果你想的簡單,更優雅的方式這樣做,命令就是你需要的地方。

ls>thefile.txt 

在這個例子中「>」從LS的輸出(列表目錄)命令寫入到文件中。您可以從一個AppleScript

set thePosixDrectory to posix path of file thedirectory of app "Finder" 
set theposixResults to posix path of file theresultfile of app "Finder" 
do shell script ("ls \"" & thePosixDrectory & "\">\"" & theposixResults & "\"")as string 

中運行這個POSIX路徑的東西是把AppleScript的風格directory:paths:to your:files到UNIX風格/directory/paths/to\ your/files

需要注意的是,實際上被運行shell腳本看起來像:

ls "/some/directory/path/">"/some/file/path.txt" 

的報價都沒有停止空格或其他字符時髦從混亂的shell腳本。要停止在蘋果腳本中將引號讀爲引號,反斜槓被用來「逃避」它們。您還可以使用單引號,爲更可讀的代碼改爲:

做shell腳本( 「LS '」 & thePosixDrectory & 「 '>'」 & theposixResults & 「'」)作爲字符串

將出現在殼狀

ls '/some/directory/path/'>'/some/file/path.txt' 

HTH