Applescript: Am I able to save a text file using a variable for the filename and file path?

老子叫甜甜 提交于 2020-01-24 00:25:19

问题


I have created a script to find/replace within the document to create an SQL insert statement, but so far I have been unable to create a statement that allows me to save the results using a saved variable (the date) and extension (.sql) to a different folder.

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

This results in the following error:

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

Any help would be appreciated.


回答1:


This saves BBEdit's text document 1 as fileName (with extension .sql)
to the desktop folder:


Version that uses the do shell script command to create the datestamp:

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


Version that uses AppleScript's date to create the datestamp:

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


If one wants to check if the file is already there, insert this after the filePath is set up:

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


Addition
Here a little help to put the path you want into the clipboard. Execute following code in a script and select the folder you want to store the files in:
set rootFolder to (choose folder with prompt "Pick a folder…") as string
set the clipboard to rootFolder

Now that path is in the clipboard and ready to get pasted in.
Example: DiskName:Users:shortusername:Desktop:rootFolder:

Now replace

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

with

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

Of course, "DiskName:Users:shortusername:Desktop:rootFolder:" is an example for the text in your clipboard.




回答2:


A slightly optimized version of Zero's stringForDate handler

 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


来源:https://stackoverflow.com/questions/29480474/applescript-am-i-able-to-save-a-text-file-using-a-variable-for-the-filename-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!