applescript to duplicate open numbers file

AppleScript is nothing like any other language I have used.. cant figure out what should be so simple.


I am just trying to make a script that would create a local back up of an open document. ( I will reopen the original iCloud version afterwards, but cant get past the saveas)


I tried to use "SAVE <document> IN <Path>" but couldn't figure it out, switched to "DUPLICATE" and am still stuck...


I failed so many times on SAVE, I tried DUPLICATE thinking might be better.

you can see I have tried multiple different things, cant figure out why it keeps failing with different errors that I don't understand. (I commented the error first then the line that throws that error)

These same kinds of things came up with the ability to ?"Save <document> in <path>"



tell application "Numbers"
	set filePath to file of front document
	set the_name to name of front document
	
	set theNewFilePath to "Macintosh HD:Users:MyName:Documents:backups:numbers:"
    --cant make the <filepath> into type location specifier... its literally saying use the 
    -- front document and filepath came from another script 
    -- that makes it for me that i found online and it seems to be the riught format from others  
 --duplicate front document to theNewFilePath

    --Cant get Document whose name is... but it got the name from the front document?
 --duplicate (the document whose name is the_name) to theNewFilePath
	
    --Cant make <filename> into type specifier ... what the heck is this specifier type?
 --duplicate the_name to theNewFilePath
end tell


Any help would be greatly appreciated

Jason


iMac 27″ 5K

Posted on Apr 16, 2023 11:38 AM

Reply
Question marked as Best reply

Posted on Apr 16, 2023 2:43 PM

Turns out after days of working on and off... after posting the above. I finally got it to work... not sure what change I made that made the difference... I took out the "file of" on the document line and added "as string" after the file path (not sure why a string isn't defined with them quotes)....


tell application "Numbers"
	activate
	--create timestamp
	set todaysDate to (current date)
	set DayIs to text -2 thru -1 of ("0" & (day of todaysDate as string))
	set MonthIs to text -2 thru -1 of ("0" & (month of todaysDate as number))
	set YearIs to (year of todaysDate as string)
	set TimeStamp to DayIs & space & MonthIs & space & YearIs

	--get references
	set thisDocument to front document
	set the_name to name of front document

	--create filename and save
	--Only using this in numbers, so the extension will always be 8
	set NewName to text 1 thru ((length of the_name) - 8) of the_name
	set NewName to NewName & "-- " & TimeStamp & ".numbers"
	set theNewFilePath to "Macintosh HD:Users:Myname:Documents:backups:numbers:" & (NewName) as string

	save thisDocument in file theNewFilePath
end tell



Similar questions

3 replies
Question marked as Best reply

Apr 16, 2023 2:43 PM in response to jaxjason

Turns out after days of working on and off... after posting the above. I finally got it to work... not sure what change I made that made the difference... I took out the "file of" on the document line and added "as string" after the file path (not sure why a string isn't defined with them quotes)....


tell application "Numbers"
	activate
	--create timestamp
	set todaysDate to (current date)
	set DayIs to text -2 thru -1 of ("0" & (day of todaysDate as string))
	set MonthIs to text -2 thru -1 of ("0" & (month of todaysDate as number))
	set YearIs to (year of todaysDate as string)
	set TimeStamp to DayIs & space & MonthIs & space & YearIs

	--get references
	set thisDocument to front document
	set the_name to name of front document

	--create filename and save
	--Only using this in numbers, so the extension will always be 8
	set NewName to text 1 thru ((length of the_name) - 8) of the_name
	set NewName to NewName & "-- " & TimeStamp & ".numbers"
	set theNewFilePath to "Macintosh HD:Users:Myname:Documents:backups:numbers:" & (NewName) as string

	save thisDocument in file theNewFilePath
end tell



Apr 18, 2023 8:57 AM in response to jaxjason

Just for fun here's what ChatGPT suggested when I prompted:


I have an open Numbers document on the Mac. Write an AppleScript that will save a copy of it in a folder called "Backups" and add today's date to its name.


In about half a minute it gave this, followed by an explanation.


tell application "Numbers"
	-- Get the name of the document
	set docName to name of front document
	
	-- Get today's date in the format YYYY-MM-DD
	set today to current date
	set yearNum to year of today
	set monthNum to (text -2 thru -1 of ("00" & ((month of today) as integer)))
	set dayNum to (text -2 thru -1 of ("00" & (day of today)))
	set formattedDate to yearNum & "-" & monthNum & "-" & dayNum
	
	-- Get the path to the backup folder
	set backupPath to POSIX path of (path to documents folder) & "Backups/Numbers/"
	
	-- Create the backup folder if it doesn't already exist
	do shell script "mkdir -p " & quoted form of backupPath
	
	-- Save a copy of the document with today's date added to the name
	set backupName to (text 1 thru -7 of docName) & " " & formattedDate & ".numbers"
	set backupPathWithName to backupPath & backupName
	save front document in backupPathWithName
	
	-- Display a dialog box to confirm that the document was backed up
	display dialog "Document backed up to " & backupPathWithName buttons {"OK"} default button "OK"
end tell



Pretty close. Had an extra ) after integer. But it looks as if it can save a lot of time!


SG

applescript to duplicate open numbers file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.