2015-04-06 53 views
0

我已經創建了一個腳本在文檔中查找/替換以創建一個SQL插入語句,但到目前爲止,我一直無法創建一條語句,允許我使用保存的變量(日期)和擴展名( .sql)到不同的文件夾。Applescript:我可以使用文件名和文件路徑的變量保存文本文件嗎?

tell (current date) to set {_year, _month, _day} to {year, it's month, day} 
set _day to text -2 thru -1 of ("00" & _day) -- add leading zeros if needed 
set _month to text -2 thru -1 of ("00" & (_month as integer)) -- add leading zeros if needed 
set _year to text -4 thru -1 of ("00" & (_year as integer)) 
set _date to _year & _month & _day 

save text document 1 to file "Filepath:" & fileName without saving as stationery 

這將導致以下錯誤:

error "BBEdit got an error: Can’t get file \"Filepath:\"." number -1728 from file "Macintosh HD:Users:Filepath:"

任何幫助,將不勝感激。

回答

0

這節省BBEdittext document 1fileName(擴展.sql
desktop folder:使用該do shell script命令創建郵戳


版本:

set fileName to (do shell script "/bin/date +%Y-%m-%d") & ".sql" 
set filePath to ((path to desktop folder) as text) & fileName 

tell application "BBEdit" 
    if exists text document 1 then 
     save text document 1 to file filePath 
    else 
     beep 
    end if 
end tell 


版本使用AppleScript的date創建日期戳:

set fileName to my stringForDate("") & ".sql" 
set filePath to ((path to desktop folder) as text) & fileName 

tell application "BBEdit" 
    if exists text document 1 then 
     save text document 1 to file filePath 
    else 
     beep 
    end if 
end tell 

on stringForDate(aDate) 
    if aDate is "" then set aDate to (the current date) 
    try 
     set dYear to year of (aDate) as number 
     set dMonth to month of (aDate) as number 
     set dDay to day of (aDate) as number 
     if dMonth < 10 then set dMonth to "0" & dMonth 
     if dDay < 10 then set dDay to "0" & dDay 
     return ((dYear & "-" & dMonth & "-" & dDay) as string) 
    on error 
     return "-ERROR" 
    end try 
end stringForDate 


如果想檢查文件是否已經存在,則filePath後插入此設置:

tell application "Finder" 
    if exists file filePath then 
     beep 
     display dialog "Overwrite existing file?" buttons {"Overwrite", "Cancel"} default button 2 
     if the button returned of the result is "Cancel" then 
      return 
     end if 
    end if 
end tell 


加成
這裏一點幫助把你想要進入剪貼板的路徑。在腳本執行下面的代碼,然後選擇文件夾,你想將文件存儲在:

set rootFolder to (choose folder with prompt "Pick a folder…") as string 
set the clipboard to rootFolder 

現在,路徑在剪貼板並準備好粘貼在
例:DiskName:Users:shortusername:Desktop:rootFolder:

現在更換

set filePath to ((path to desktop folder) as text) & fileName 

set filePath to "DiskName:Users:shortusername:Desktop:rootFolder:" & fileName 

當然,「DiskName:Users:shortusername:Desktop:rootFolder:」是剪貼板中文本的示例。

+0

感謝您的答覆。不幸的是,我不想保存在桌面上,我需要保存在一個文件夾中,因爲這是每天發送的電子郵件,我希望所有日常文件存儲在正確的文件夾中。在你的例子「設置filePath到((路徑到桌面文件夾)作爲文本)&fileName」我怎麼能設置文件路徑到我需要的文件夾?謝謝! – Gordon

+0

請參閱附加。希望能幫助到你。 – 2015-04-07 02:23:34

+0

@ Zero,我稍微優化了你的stringForDate處理程序,因爲我喜歡它,所以我覺得比在註釋中更好,因爲它在這裏丟失了回車/換行符。 – McUsr

0

稍微優化Zero的stringForDate處理器的版本

on stringForDate(aDate) 
    if aDate is "" then set aDate to (the current date) 
    if class of aDate is not date then return null 
    set {year:dYear, month:dMonth, day:dDay} to aDate 
    set dMonth to dMonth as integer 
    if dMonth < 10 then set dMonth to "0" & dMonth 
    if dDay < 10 then set dDay to "0" & dDay 
    return ((dYear & "-" & dMonth & "-" & dDay) as string) 
end stringForDate 
相關問題