Applescript:我能用档案文件和文件路径的变量保存文本文件吗?

时间:2015-04-06 22:06:48

标签: applescript bbedit

我已经在文档中创建了一个脚本来查找/替换以创建一个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出错:无法获取文件\”文件路径:\“。”编号为-1728的文件“Macintosh HD:Users:Filepath:”

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

这会将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

当然,&#34; DiskName:用户:shortusername:桌面:rootFolder:&#34;是剪贴板中文本的示例。

答案 1 :(得分: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