Saving document from Pages - permission error

Hi, I wrote a script that makes a new document from template in Pages using info from Filemaker. The problem is that when I want to save the document or export to a .pdf document it gives me an error “You don’t have enough permissions” (in spanish). I’ve searched everywhere and tested a lot of different solutions but I couldn’t solve it.

Here are the important chunks of code:



set carpeta_caso to choose folder with prompt "Choose folder:" -- here a folder, not file, should be chosen
set carpeta_caso_path to POSIX path of (carpeta_caso)
tell application "Pages"
	set informe to make new document with properties {document template:template nombre_template, name:nombre_informe}
	--> Here comes the code to complete the Pages document -- no problem with this
	set file_name to "Informe " & caso_ID & ".pages"
	set file_path to carpeta_caso_path & file_name
	save informe in POSIX file file_path
end tell


I am using MacOS 10.12.2 and Pages 6.05

Thanks in advance for your answers!

Model: MacBook Pro
Browser: Safari 602.3.12
Operating System: Mac OS X (10.10)

Hi,

it’s much easier to use an HFS path by coercing the result of choose folder to text

set carpeta_caso_path to (choose folder with prompt "Choose folder:") as text -- here a folder, not file, should be chosen
tell application "Pages"
	set informe to make new document with properties {document template:template nombre_template, name:nombre_informe}
	--> Here comes the code to complete the Pages document -- no problem with this
	set file_name to "Informe " & caso_ID & ".pages"
	set file_path to carpeta_caso_path & file_name
	save informe in file_path
end tell

Thanks for your advise. I changed it but I still have the same problem. I also tried:


save informe in file file_path

instead of just


save informe in file_path

but it doesn’t change anything. I think I have tried that before, and then changed to the POSIX path because of somebody suggesting that, something related with sandboxing. Because it continued the same, I left the POSIX path, but both give me the same error. :frowning:

You are facing a problem described in several threads.
I kept the class of objects used in your original code.
The important instruction is commented as “THE TIP”

set carpeta_caso to path to desktop
set carpeta_caso_path to POSIX path of (carpeta_caso)
set caso_ID to 123

set file_name to "Informe " & caso_ID & ".pages"
set file_path to carpeta_caso_path & file_name
set posixFile to POSIX file file_path
close access (open for access posixFile) # THE TIP
tell application "Pages"
	activate
	set informe to make new document
	--> Here comes the code to complete the Pages document -- no problem with this
	
	save informe in posixFile
end tell

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 18 janvier 2017 18:51:04

Thank you very much Yvan!! It worked as expected! Now I will try to understand what that mean so I can learn and not just copy your code. I was struggling with this problem for several days.

Thank you again!

open for access posixFile
creates the defined file
close access immediately close the created file
Once the file exists, Pages is able to save the document in this file object.

We may do the same with the Unix command : touch but triggering it thru do shell script requires about 30 times the one required by the old fashioned instruction.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 18 janvier 2017 21:02:44

Thanks again Yvan!

What is funny is that in AppleScript User Guide I saw no information about the fact that the open for access command creates a file if it doesn’t exist.
The standard use is :

set fileDesc to open for access file "path:to:the:file"
-- work upon the file
close access fileDesc

As here we do nothing with the file I shortened that into

close access (open for access file "path:to:the:file")

Under 10.12.2/3, without the given instruction,
Excel 2016 can access ~/Library/Application Scripts/com.microsoft.Excel/
Word 2016 can access ~/Library/Application Scripts/com.microsoft.Word/
Mail can access ~/Downloads/
Pages can access /Library/Containers/com.apple.iWork.Pages/Data/Documents/
Numbers can access /Library/Containers/com.apple.iWork.Numbers/Data/Documents/
Keynote can access /Library/Containers/com.apple.iWork.Keynote/Data/Documents/

With it they can access every folder which is not write protected.

I would not be surprised to learn that the list of apps with this new behavior is longer.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 19 janvier 2017 13:30:28

Good news

With the current version of beta 10.12.4, the tip
close access (open for access posixFile)
is no longer needed (but it doesn’t hurt).

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 13:54:38

Hi Yvan,

I’m still stuggling to make this work with MS Word 2016.

Here’s what I got:


set myWordFile to choose file with prompt "Choose a word document"
set myWordFilePath to myWordFile as text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set myFolder to (text 1 thru text item -2 of myWordFilePath) & ":"
set myName to text item -1 of myWordFilePath
set AppleScript's text item delimiters to {"."}
if (count of text items of myName) > 1 then
	set myName to (text 1 thru text item -2 of myName)
end if
set AppleScript's text item delimiters to oldDelims
set newDocPath to (myFolder & myName & "_fixed.docx")
set myNewName to (myName & "_fixed.docx")

display dialog "Le script prend du temps à rouler, attendez la boîte de dialogue vous disant que c'est fini." buttons "OK" default button "OK" giving up after 5
tell application "Microsoft Word"
	launch
	--activate
	open myWordFile
	set myFind to find object of text object of active document
	clear formatting myFind
	tell myFind
		
		execute find find text "^~" replace with "-" replace replace all
	end tell
	close access (open for access file "newDocPath")
	save as active document file name newDocPath
	close active document
	
end tell

I get an error saying the path is empty or something. How do I make ajustement to my file, then save as with another file name in the same folder?

Thanks for any help, or if anybody else can help, that’d be great!

You made a huge error.


set myWordFile to choose file with prompt "Choose a word document"
set myWordFilePath to myWordFile as text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set myFolder to (text 1 thru text item -2 of myWordFilePath) & ":"
set myName to text item -1 of myWordFilePath
set AppleScript's text item delimiters to {"."}
if (count of text items of myName) > 1 then
	set myName to (text 1 thru text item -2 of myName)
end if
set AppleScript's text item delimiters to oldDelims
set myNewName to (myName & "_fixed.docx") # It's cleaner to build the new fileName first
set newDocPath to (myFolder & myNewName) # then use it to build the new pathname

display dialog "Le script prend du temps à rouler, attendez la boîte de dialogue vous disant que c'est fini." buttons "OK" default button "OK" giving up after 5
tell application "Microsoft Word"
	launch
	--activate
	open myWordFile
	set myFind to find object of text object of active document
	clear formatting myFind
	tell myFind
		
		execute find find text "^~" replace with "-" replace replace all
	end tell
	--close access (open for access file "newDocPath") # the huge error. You try to open a file whose path is the string "newDocPath"
	close access (open for access file newDocPath) # Here you  open - close a file with the correct pathname
	save as active document file name newDocPath
	close active document
	
end tell

Please, read carefully the added comments.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) lundi 6 mars 2017 16:52:06

Duh! thanks man, I don’t know why I put this between quotes.

You don’t know anything about scripting InDesign by any chance?

I have a word document, which I place in ID, then run a script that cleans it up a bit. I’m having trouble getting to all tables in the document. This is a snippet of the script to get all tables. With this, I get all the first level of tables, but if there is a table within a table, it’s not affected. So how to go through all levels of tables in a document is my question.
Just in case you work with InDesign:-)


		repeat with k from (count of every story) to 1 by -1
			repeat with j from (count of table of story k) to 1 by -1
				set mytable to table j of story k

Thanks for the feedback.
I don’t know inDesign Applescript support.
In fact I don’t know Word Applescript support too but the error was really easy to find :confused: :wink:

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) lundi 6 mars 2017 19:44:21