Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using script editor to generate path for Apple Pages document

Vikingosx has suggested that a script can be used to generate the path to an Apple Pages document (and I suspect, a Numbers document, as well), which can then be placed in the footer of that document. I will appreciate if Vikingosx or someone else will be willing to provide the script which can do that.


Thanks.

Bill

MacBook Pro 13″, macOS 11.4

Posted on Jul 8, 2021 1:11 PM

Reply
Question marked as Best answer

WOW! Thank you, VikingOSX! You have saved me countless hours of work. I trust others will see your response and likewise be a be able to benefit from your help.


All the best,


Bill

Posted on Jul 8, 2021 7:39 PM

5 replies

Jul 8, 2021 3:13 PM in response to vermontbill

The best way to approach this is with AppleScript, though bearing in mind that Page's AppleScript dictionary does not expose header or footer content. The solution that I have used involves Pages placeholder text, where one creates a unique string in the header or footer location where the file path is desired, and Pages can replace that placeholder text with the file path.


Let's say that I place some (arbitrarily named) boilerplate text in the lower-left footer segment:


[filepath]


I then select all of that text including the brackets, and visit the Format menu > Advanced > Define as Placeholder Text. So now, it looks like this:



and then I run the following AppleScript to replace that placeholder text with a tilde path (for privacy reasons) of the currently opened Pages document (which was previously saved):



AppleScript to be copy/pasted into Script Editor and run on an opened, previously saved Pages document:


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString

set phtext to text returned of (display dialog "Enter entire placeholder string (e.g. [filename]):" default answer "")
set theTags to {} as list

tell application "Pages"
	launch
	if not (exists front document) then
		display alert "Pages document must be open for this script to function properly."
		if it is running then quit
		return
	end if
	set thePath to POSIX path of (front document's file as alias) as text
	set thePath to my tilde_path(thePath)
	
	tell front document
		set theTags to the tag of every placeholder text whose tag contains phtext
		repeat with i from 1 to count of theTags
			set thisTag to item i of theTags
			set (every placeholder text whose tag is thisTag) to thePath as text
		end repeat
	end tell
end tell
return

on tilde_path(afile)
	-- conceal user name in path as ~/Desktop/foo.pages
	return ((NSString's stringWithString:afile)'s stringByAbbreviatingWithTildeInPath()) as text
end tilde_path






Question marked as Helpful

Jul 8, 2021 3:42 PM in response to VikingOSX

Apple's Numbers application allows you to assign placeholder text to selected content in text boxes, but not within table cells. However, one has no AppleScript dictionary support for placeholder text in Numbers as one does in Pages, so we have to replace the text in another manner.



AppleScript to be copy/pasted into Script Editor and run on an opened, previously saved Numbers spreadsheet:


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString

set phtext to text returned of (display dialog "Enter entire placeholder string (e.g. [filename]):" default answer "")
set theTags to {} as list

tell application "Numbers"
	launch
	if not (exists front document) then
		display alert "Numbers document must be open for this script to function properly."
		if it is running then quit
		return
	end if
	set thePath to POSIX path of (front document's file as alias) as text
	set thePath to my tilde_path(thePath)
	
	tell front document's first sheet
		# get all of the text boxes that have the placeholder text in them
		set theTags to every text item whose object text contains phtext
		repeat with placeholder in theTags
			set placeholder's object text to thePath
		end repeat
	end tell
end tell
return

on tilde_path(afile)
	-- conceal user name in path as ~/Desktop/foo.pages
	return ((NSString's stringWithString:afile)'s stringByAbbreviatingWithTildeInPath()) as text
end tilde_path


Tested with macOS 11.4, Numbers v11.1


Using script editor to generate path for Apple Pages document

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