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

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:

48%20PM

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.

2 Likes

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.

-Chris

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 word.app. 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.

image

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

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.
"https://stu-my.sharepoint.com/personal/xxx/test.docx"

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
        path
    end tell
end tell

or

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

First one worked. TY

1 Like

I'm not going to special-case it for Microsoft Office 365.

That macro uses AppleScript UI-Scripting to discover the document attached to the front window in the front application, and there are a few apps it won't work with.

That said – others are welcome to attend to their own special needs.

1 Like

Thanks, tt's only not work with the documents stored in the OneDrive.

Works great for some apps.

"No file was associated with the front window!" for Logic Pro and Ableton Live.

As it says on the box in the first thread – neither macOS nor application developers allow for any universal methods, alas.

1 Like

Hi @noisneil - for Logic there is the inbuilt menu item "Show in Finder" so, maybe you could add this in to the Macro with an If Then Action?

2 Likes