path to home folder script not point to home folder

I am building a script for Pages which will including setting the path to where a file is saved.

But in this script setting a variable to path to home folder, for example:

set pPath to POSIX path of (path to home folder)

does not go to the home folder, instead it goes to

In new (blank) script running the above gives:

If relevant: temporary Pages files are stored in an iCloud folder, and scripts also stored in an iCloud folder.

Any help would be appreciated to understand why within the Pages script the path to home folder

Relevant parts of the Pages script are (I have not reproduced all of it, as most of it is about formatting paragraphs, so not all the declared variables are shown, and as quoted here will not run without errors):

tell application "Keyboard Maestro Engine"
	
	set AS_Date_prescribed_information_provided to getvariable "Date_prescribed_information_provided"
	set AS_client_name to getvariable "client_name"
	set AS_email_address to getvariable "email_address"
	set AS_details to getvariable "details"
	set AS_number_of_documents to getvariable "number_of_documents"
	set AS_document_details to getvariable "document_details"
	set AS_amount_of_notary_charge to getvariable "amount_of_notary_charge"
	set AS_what_will_third_parties_be_providing to getvariable "what_will_third_parties_be_providing"
	set AS_Charge_to_be_made_by_third_parties to getvariable "Charge_to_be_made_by_third_parties"
	set AS_final_charge to getvariable "final_charge"
end tell

set PrescribedInformation to "Prescribed information"

set date_of_info to "Date"

set name_and_email_address_of_client to "Name of client and email address"

set vname to "Name: "
set email to "Email: "


set my_details to "My Details"
[... part of script deleted ...]

tell application "Pages"
	activate
	
	set thisDocument to ¬
		make new document with properties {document template:template "Notary - basic template"}
	
	tell thisDocument
		
		set body text to PrescribedInformation & return & return & ¬
			date_of_info & return & AS_Date_prescribed_information_provided & return & return & ¬
			name_and_email_address_of_client & return & ¬
			vname & AS_client_name & return & ¬
			email & AS_email_address & return & return & ¬
			my_details & return & return & ¬
			services_that_I_provide & return & return & ¬
			description_services & AS_details & return & ¬
			number_of_documents & return & ¬
			document_details & return & ¬
			my_charge & return & ¬
			what_3rd_are_providing & return & ¬
			third_party_charge & return & ¬
			total_charge & return & return & ¬
			TandCs_and_about_charges_payments
		[... part of script deleted ...]
		end tell
	end tell
	
end tell

tell application "Pages"
	activate
	
	-- as name of client contains first and middle names as well as last name
	-- need to extract just last name which will always be last word in 
	-- variable AS_client_name
	
	set last_name to word -1 of AS_client_name
	
	-- make up filename from last name of client and prescribed information and date
	
	set prescribed_information_filename_without_extension to last_name & " - prescribed information - " & theDate
	
	-- to save files need to add extension to file name for pages and pdf
	set prescribed_information_filename_with_pages_extension to prescribed_information_filename_without_extension & ".pages"
	
	set prescribed_information_filename_with_pdf_extension to prescribed_information_filename_without_extension & ".pdf"
	
	-- setting the path to where filenames are saved
	
	-- set filepath to "Macintosh HD:Users:victor:vwdata:notary:2021:2021-prospective work:" as string
	-- set filepath to "T7:Users:victor:vwdata:notary:2021:2021-prospective work:" as string
	
	-- set filepath to "/Users/victor/vwdata/notary/2021/2021-prospective work/" as string
	
set filepath to (POSIX path of (path to home folder)) & "vwdata/notary/2021/2021-prospective work/"
	
	-- setting a variables for file path and pages file and file path and pdf file
	set pages_filepath_and_filename to filepath & prescribed_information_filename_with_pages_extension
	
	set PDF_filepath_and_filename to filepath & prescribed_information_filename_with_pdf_extension
	
	-- save the document as pages file and export as pdf
	
	
	--	save thisDocument in file pages_filepath_and_filename
	--	export front document to file PDF_filepath_and_filename as PDF
end tell

Model: iMac
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 12

The path to command is a part of Standard Additions and often will not work correctly in another application’s tell statement. Also, many apps do not support the POSIX path property, and that may be the case with Pages. It’s probably best just to set that path outside the Pages tell statement.

Actually, it’s more of a Pages issue. Apparently, Pages has its own ‘home folder’.

tell application "Pages" to path to home folder
--> alias "MacHD:Users:username:Library:Containers:com.apple.iWork.Pages:Data:"

You can move those lines as peavine suggests but you can also use ‘tell me’ —‘me’ as in the ‘script’— inside the tell block and it will return the hoped for reference:

tell application "Pages"

	tell me to path to home folder
	--> alias "MacHD:Users:username:"
	
	tell me to POSIX path of (path to home folder)
	--> "/Users/username/"
	
end tell

It seems like the other special folders references function normally (e.g. downloads folder). And it seems to function consistently regardless of whether you include use scripting additions in your script.

FWIW, other apps seem to do the same thing.

tell application "Safari"
	set aVariable to (path to home folder) --> alias "Macintosh HD:Users:Robert:Library:Containers:com.apple.Safari:Data:"
end tell

tell application "Mail"
	set aVariable to (path to home folder) --> alias "Macintosh HD:Users:Robert:Library:Containers:com.apple.mail:Data:"
end tell

Interesting, my first reaction was that it was an Apple apps thing. I’d already imagined similar for the other iwork apps.

On a whim, I tried some other apps.

It’s the same for another apple app, TextEdit, but also non-Apple apps such as Tweetbot and OmniOutliner.

However, not the same for the browsers Firefox Nightly or iCab. I checked Acorn, BBEdit and DevonThink as well and they show the standard user home, as do apple’s Script Editor or Terminal.

I wonder if it’s related to storing documents on icloud. Oh well, I guess it’s mostly a curiosity but one more applescript quirk to know.

It’s a sandbox issue.

The home folder of sandboxed apps is the Data folder of the application container