How to get just the file path of a currently opened file in app

I have a file opened in an app (say PDFpen). For example: ~/Documents/test.pdf.

Is it possible to just get the file path (without the filename)?

I can get the path (with filename) of the file with AppleScript, for example:

tell application "PDFpen"
	
	return path of front document
	
end tell

and I can get the just the filename (with extension), for example

tell application "PDFpen"
	
	return name of front document
	
end tell

But cannot workout how can get just the path without the filename.

Any help would be gratefully received.

tell application "PDFpen"
	set POSIXPath to path of front document
end tell

--set POSIXPath to "~/Documents/test.pdf"
tell application "System Events"
	set theName to name of disk item POSIXPath
	set theContainer to path of (container of disk item POSIXPath)
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 janvier 2021 12:13:40

You may also use:


tell application "PDFpen"
	set POSIXPath to path of front document
end tell

-- set POSIXPath to "~/Documents/test.pdf"

set aList to my decoupe(POSIXPath, "/")
set theName to item -1 of aList
set theContainer to my recolle(items 1 thru -2 of aList, "/")
#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as string
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 janvier 2021 18:02:45

Yan,

Thank you very much for the providing the two ways of doing this. I am very grateful.

I asked for help on this earlier this year. It works fine with PDFpenPro.

But when I try to use it with another application I get the following error, for example with Pages:

The script:

tell application "Pages"
	set POSIXPath to path of front document
end tell

The document in Pages is saved as “test.pages” on to the Desktop.

macOS 11.6, Pages 11.1, Script Editor 2.11

Any help would be gratefully received.

The Pages.app doesn’t provide path property. But you can get Posix path of front document using file property of document and Posix path command of Standard Additions.


tell application "Pages"
	set POSIXPath to POSIX path of (get file of front document)
end tell

KniazidisR,

Thank you very much for response and script.

One question, is the only way to get just the file path for an app which does not provide a path property is to use the code that Yvan Koenig provided?

I don’t know if I understood your question correctly, but I think the answer is obvious: the script from @Yvan Koenig is only for applications that have a path property to access their document file.

There is no single way for applications that do not have this property, since everything depends on the implementation of the application by the developer. Although, so far I have come across only those applications that use one of two: file and path. You can put in try block: first attempt with path property, on error - second attempt with file property, on nested error - return the error description.


set appName to "Pages"

tell application appName
	try
		return path of front document
	on error
		try
			return POSIX path of (get file of front document)
		on error errorMessage
			return errorMessage
		end try
	end try
end tell