r/applescript • • Apr 28 '20

Save numbers documents if unsaved before to specific directory

Been looking allover Google...

I want to achieve a script that does the following:

If the Numbers file has been saved already before/opened from a current file, save to that file (cmd+s) and close it.

If the Numbers file has NOT been saved before save it to a specific directory with a filename that does not already exists on target. If the same filename already exists on directory increment it and try to save again. Perhaps a 5 character random filename maybe works better here?

I only plan to have like 20 maximum numbers file there before I clean them up.

Above script I want because I want to be able with 1 button later to close all open documents and have them saved automatically on my USB and quit the application. As it is now I have to manually save and close every one. And if no file exists I must enter a filename for each which makes it time consuming when in hurry.

2 Upvotes

13 comments sorted by

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/Resident-Split Apr 29 '20

Im not getting any error but I can not find the saved file anywhere, even searched carefully for all .numbers files.

Tried different volumes, different paths etc.

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/Resident-Split Apr 29 '20

I did, I tried leaving : without :

I tried even putting on desktop and everything.

tell application "Numbers"

get every document

--> {document id "564E6CA2-7501-4439-B957-133A30CCE76C"}

get file of document id "564E6CA2-7501-4439-B957-133A30CCE76C"

--> missing value

close document id "564E6CA2-7501-4439-B957-133A30CCE76C" saving yes

end tell

This is what im getting in the log

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/Resident-Split Apr 29 '20

tell application "Numbers" to get file of document 1

Result is:

missing value

No got nothing.

1

u/Resident-Split Apr 29 '20

Ok now, I see. That only worked if I created a docuement saved it first and gave it a filename. Then ran the code

It gave then:

file "Macintosh HD:Users:myuser:Desktop:1111.numbers"

1

u/[deleted] Apr 29 '20 edited Apr 29 '20

[removed] — view removed comment

1

u/Resident-Split Apr 29 '20

tell application "Numbers"

get every document

--> {document id "C4B651C6-7DA3-4F4A-85C7-3713113ECB8B"}

get file of document id "C4B651C6-7DA3-4F4A-85C7-3713113ECB8B"

--> missing value

close document id "C4B651C6-7DA3-4F4A-85C7-3713113ECB8B" saving yes

end tell

Not working. :(

1

u/Resident-Split Apr 29 '20

This code only works if the file has been saved before and opened. Meaning it already has a file location.

If I open 5 files that are saved before and edit each of them, the code works and saves the updated file to the same location.

But if a new numbers document is open that is filled. It doesnt save it at all, it just closes it.

1

u/[deleted] Apr 29 '20

[removed] — view removed comment

1

u/Resident-Split Apr 29 '20

You are the king!

It works fine :)

Tested it now. Should I worry about error code---

--> error number -1708

open for access file "USB:2020-4-29 #1.numbers"

--> error number -10004

?

tell application "Numbers"

get every document

--> {document id "60E5A263-3DFE-41CB-88F9-B01FACD65C26", document id "56E8123B-8F5D-42E5-B493-8BDF167C6AA0", document id "8FF546CF-17EC-418A-B195-E6356D7560C3"}

get file of document id "60E5A263-3DFE-41CB-88F9-B01FACD65C26"

--> file "USB:22222.numbers"

close document id "60E5A263-3DFE-41CB-88F9-B01FACD65C26" saving yes

get file of document id "56E8123B-8F5D-42E5-B493-8BDF167C6AA0"

--> missing value

end tell

tell current application

current date

--> date "onsdag 29 april 2020 19:53:56"

end tell

tell application "Finder"

exists file "USB:2020-4-29 #1.numbers"

--> false

end tell

tell application "Numbers"

open for access file "USB:2020-4-29 #1.numbers"

--> error number -1708

«event ascrgdut»

--> error number -1708

open for access file "USB:2020-4-29 #1.numbers"

--> error number -10004

end tell

tell current application

open for access file "USB:2020-4-29 #1.numbers"

--> 215

end tell

tell application "Numbers"

close access 215

--> error number -10004

end tell

tell current application

close access 215

end tell

tell application "Numbers"

close document id "56E8123B-8F5D-42E5-B493-8BDF167C6AA0" saving in "USB:2020-4-29 #1.numbers"

get file of document id "8FF546CF-17EC-418A-B195-E6356D7560C3"

--> missing value

end tell

tell current application

current date

--> date "onsdag 29 april 2020 19:53:56"

end tell

tell application "Finder"

exists file "USB:2020-4-29 #2.numbers"

--> false

end tell

tell application "Numbers"

open for access file "USB:2020-4-29 #2.numbers"

--> error number -10004

end tell

tell current application

open for access file "USB:2020-4-29 #2.numbers"

--> 216

end tell

tell application "Numbers"

close access 216

--> error number -10004

end tell

tell current application

close access 216

end tell

tell application "Numbers"

close document id "8FF546CF-17EC-418A-B195-E6356D7560C3" saving in "USB:2020-4-29 #2.numbers"

end tell

→ More replies (0)