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.
Interesting… THANKS
Ok, I added that and savefile now appears to have the proper path/file string… But now “Numbers” will not open the file…
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” as text
set saveFile to (POSIX file “/Users/Joe/Desktop/” as text) & newfilename
tell application “Numbers”
set doc to open csvFile
set password “password” to first document
save doc in saveFile
close doc
end tell
Is it simpler to just move the file and rename it in the process?
tell application “Finder”
activate
set srcFile to POSIX file “/Users/Joe/Desktop/Data.numbers”
set destfile to (POSIX file “/Users/Joe/Desktop/” as text) & newfilename
move srcFile to destfile
end tell
tell application “Finder”
set docName to POSIX file “/Users/Home/Desktop/Data.numbers”
set fileToBeNamed to duplicate file docName to desktop with replacing
set name of fileToBeNamed to newfilename
end tell
So I successfully renamed the file… Now
How can I delete the original file and then Move the new file to a different folder?
No, it’s not easier. It’s not difficult but saving with the name you want is easiest.
Hmm… my version of Numbers does not have the ability to set a password (except when exporting — which doesn’t save as a regular Numbers document). Documents don’t have a password property.
If you can copy the relevant text from the Numbers dictionary, I can try and figure it out. Otherwise, someone with a new version would need to weigh in.
Just reading your subsequent post… you would use the Finder or System Events to move a file and delete a file.
Since you’re just working with a single file, it is simpler to use the Finder.
tell application "Finder"
move file csvFile to the trash
move file saveFile to (path to documents folder)
end tell
FWIW, your solution in post 13 is a little odd.
Post your final script in its entirety and I’ll see if I can tweak it.
Ok here we go. This successfully opens the “Data.txt” file in “Numbers” and saves the file as a “Numbers” file with a password. And then within the “Finder” application it successfully renames the file to include a leading date!!! It then deletes the original “Numbers” file without the date. At this point all is great!! It fails when i try to move the file to my icloud folder (or any folder). I’m sure its syntax related…
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 & “_Data2.numbers”
set csvFile to POSIX file “/Users/Joe/Desktop/Data.txt”
set saveFile to POSIX file “/Users/Joe/Desktop/Data2.numbers”
tell application “Numbers”
–set doc to open csvFile
set doc to open file csvFile
set password “password” to first document
save doc in saveFile
close doc
end tell
tell application “Finder”
set docName to POSIX file “/Users/Joe/Desktop/Data2.numbers”
set fileToBeNamed to duplicate file docName to desktop with replacing
set name of fileToBeNamed to newfilename – Sucessfully renames file with leading date
delete POSIX file “/Users/Joe/Desktop/Data2.numbers”
move file newfilename to "/Users/Joe/Library/Mobile Documents/com~apple~CloudDocs/"