0

我创建了一个脚本来在文档中查找/替换以创建 SQL 插入语句,但到目前为止,我无法创建允许我使用保存的变量(日期)和扩展名(.sql)保存结果的语句) 到另一个文件夹。

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

这会导致以下错误:

错误“BBEdit 出现错误:无法获取文件 \"Filepath:\"。” 来自文件“Macintosh HD:用户:文件路径:”的编号 -1728

任何帮助,将不胜感激。

4

2 回答 2

0

这将BBEdit's保存text 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


使用 AppleScriptdate创建日期戳的版本:

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:”是剪贴板中文本的示例。

于 2015-04-06T23:35:46.927 回答
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
于 2015-04-07T08:31:39.560 回答