I’m new to Applescript. I have a simple script to open a text file and save it as a Numbers file. It Works!! Remember I’m new. What I would like to do, in the simplest way, is to rename the file to include the date. For example I want to rename
“/Users/Joe/Desktop/Data.numbers” to
“/Users/Joe/Desktop/21Jan26_Data.numbers”
This is my simple script to open a txt file with numbers, add a password, and save as a numbers file. This is what I want to do and it works. However I want to rename the saved file to include the date.
set csvFile to POSIX file “/Users/Joe/Desktop/Data.txt”
set saveFile to POSIX file “/Users/Joe/Desktop/Data.numbers”
tell application “Numbers”
set doc to open csvFile
set password “newpassword” to first document
save doc in saveFile
close doc
end tell
Now… I’ve searched and found several scripts to rename the file, but so far I’ve gotten none of them to work, maybe because I don’t know the syntax enough to update it for my purposes.
to date_format(old_date) – Old_date is text, not a date.
set {year:y, month:m, day:d} to date old_date
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & “-” & text 5 thru 6 & “-” & text 7 thru 8
end date_format
set theDate to (current date)
set dateFormatted to date_format(date string of (theDate))
set newfilename to dateFormatted & “_Data.numbers”
set csvFile to POSIX file “/Users/Joe/Desktop/Data.txt”
set saveFile to POSIX file “/Users/Joe/Desktop/" & newfilename
tell application “Numbers”
set doc to open csvFile
set password “password” to first document
save doc in saveFile
close doc
end tell
newfilename gives me the updated filename
but I get an error in the save doc line now… maybe something with using “POSIX”?
set csvFile to POSIX file "/Users/Joe/Desktop/Data.txt" as text
set saveFile to (POSIX file "/Users/Joe/Desktop/" as text) & newfilename
posix file is an object (technically, ‘file’ is the object).
Since your objective there is to create a string, you have to coerce the posix file reference to a string. If you don’t do that, you actually end up with a list.