Finding the File Path for the Front Document in the Front Application

Ah, a bit more messing about in AppleScript and this did the job:

POSIX path of (choose file)

Next step is to find the file path of the file I’m currently working on and then choose file if that’s null…


Hey Mike,

Okay, first problem solved.

The proper format for an alias in AppleScript is (note the quotes):

alias "Macintosh SSD:Users:mikekentdavies:Desktop:Blank MS 9 staves.tif"

But — Keyboard Maestro does NOT understand aliases — it understands POSIX Paths.

Keyboard Maestro munges AppleScript-Objects when it returns them as plain-text, so for instance you cannot return a list-object from AppleScript to Keyboard Maestro and then use it without any other processing.

This italicized portion of the quote is what my script above is designed to do.

To alter it to choose a file if the current working file is NOT found is simple:

# Auth: Christopher Stone
# dCre: 2017/05/22 15:10
# dMod: 2017/05/29 14:16
# Appl: System Events
# Task: Attempt to acquire the POSIX Path of the file associated with the front window of the front app.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @System_Events, @Acquire, @POSIX_Path, @Path, @File, @Associated, @Front, @Window, @Front_App

use framework "Foundation"
use scripting additions

   tell application "System Events"
      set frontProcess to first process whose frontmost is true
      set procName to name of frontProcess
      tell frontProcess
         tell front window
            tell attribute "AXDocument"
               set fileUrlOfFrontWindow to its value
            end tell
         end tell
      end tell
   end tell
on error
   set fileUrlOfFrontWindow to missing value
end try

if fileUrlOfFrontWindow ≠ missing value and fileUrlOfFrontWindow ≠ "file:///Irrelevent" then
   set posixPath to (current application's class "NSURL"'s URLWithString:fileUrlOfFrontWindow)'s |path|() as text
   return posixPath
   set theFile to POSIX path of (choose file)
end if



Yay! It’s working really well now.
I added a dialogue after the last else:

display dialog "please choose a file"

Thanks so much for all your help :smile:

Hey Mike,

The choose file command is fairly versatile. It allows for a prompt and has other goodies as well.

set dialogPrompt to "••••• PLEASE CHOOSE A FILE •••••"
set defaultLocation to path to desktop
set seeInvisibles to false
set multipleSelectionsAllowed to false
set showingPackageContents to false

set pickedFile to choose file with prompt dialogPrompt ¬
   of type {"pdf"} ¬
   default location defaultLocation ¬
   invisibles seeInvisibles ¬
   multiple selections allowed multipleSelectionsAllowed ¬
   showing package contents showingPackageContents


Ah, yep, thanks for that - saves on a dialogue box!
I was wondering about restricting which files I could attach, but decided against it - I may look again later…

Hi Chris,

In " Numbering Files added to Folders" Peter suggests "...takes the path to a file, either the currently selected file or the file just moved in to the folder by Keyboard Maestro. Either way, the file should already be in the target folder."

Seems like this macro should give me the path of the file I've selected in a Finder window. Guessingly, I selected a file on the Desktop in Finder and fired this macro and despite the "If All Conditions Met Execute Action" action "If all of the following are true:" reports (currently true). I get the "No file was associated with the front window!" error message in the window that opens.

Has something changed in the two years since you wrote this? Did I guess wrong and am using this incorrectly?


I'm not Chris (obviously :slightly_smiling_face:) but I think I can answer this nonetheless; I'm afraid you did guess wrong. The macros and scripts in this thread are for obtaining the file path for the frontmost document in apps other than the Finder, and can't be used to get the current Finder selection. Fortunately, there's only one KM action needed to get the path of the file currently selected in Finder, For Each:


Just select that collection, and the variable specified in the "For each" field will contain the path to the selected file once the macro is run.


It is not work for me, it showed "No file was associated with the front window!" when i open word file with office.

Hey Liang,

It works fine for me with Word from Office 2016 – IF the front document has already been saved.

If the front document is not saved then it behaves as you describe above.


Thanks, now it's work fine!

Doesn't work with the documents synchronized by OneDrive.

You said otherwise here:

So, did you get it to work or not?

It's still not work for the office document synchronized by onedrive. And these file cannot reveal by right-click the title bar.

So. You have a document open in what app?

Saved and synchronized to OneDrive?

And the macro doesn't work?

Do you have a local OneDrive folder and a OneDrive app? Or is Office reaching out on the net by itself?

I don't have MSO-365 to test with...

The document was open with I have a local OneDrive folder and a OneDrive app.

Only the documents linked by Excel, PPT, and Word app in the onedrive, which cannot be revealed in the Finder.

Right-click the title bar, nothing happend.


Open a suitable test document in Word, and try running this in the Apple Script

See if you get an appropriate result:

tell application "Microsoft Word"
   tell active document
      set docPathPosix to its posix full name
   end tell
end tell

return docPathPosix

I get the right file path way.

The Finding the File Path for the Front Document in the Front Application macro need to be updated?

tell application "TextEdit"
   tell active document
      set docPathPosix to its posix full name
   end tell
end tell

return docPathPosix

Returns "Expected end of line but found class name."

Running Script Editor 2.11 in macOS 12.3


"Microsoft Word" ⇄ "TextEdit"

Each application provides its own osascript interface – they're not interchangeable.

1 Like

You might try this kind of thing:

tell application "TextEdit"
    tell front document
    end tell
end tell


tell application "TextEdit"
    path of front document
end tell
1 Like