這節省BBEdit
的text document 1
爲fileName
(擴展.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:」是剪貼板中文本的示例。
感謝您的答覆。不幸的是,我不想保存在桌面上,我需要保存在一個文件夾中,因爲這是每天發送的電子郵件,我希望所有日常文件存儲在正確的文件夾中。在你的例子「設置filePath到((路徑到桌面文件夾)作爲文本)&fileName」我怎麼能設置文件路徑到我需要的文件夾?謝謝! – Gordon
請參閱附加。希望能幫助到你。 – 2015-04-07 02:23:34
@ Zero,我稍微優化了你的stringForDate處理程序,因爲我喜歡它,所以我覺得比在註釋中更好,因爲它在這裏丟失了回車/換行符。 – McUsr