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

Hey Folks,

Sometimes you need the POSIX Path of a document that's open in an application.

Sometimes that's available via the app's native sdef (AppleScript Dictionary), but unfortunately not all apps are natively AppleScriptable.

AppleScript, System Events, and UI-Scripting to the rescue – the macro below will grab the POSIX Path of the frontmost document open in most applications.

-Chris


Acquire POSIX Path of File Associated with Front Window of Front Application.kmmacros (7.7 KB)

3 Likes

See this topic and Tom’s solution for other techniques.

Finding the file path for an open document in Audacity

-ccs

Hi there,
this looks to be exactly what I’m after, but it gives me this error message:

osascript: OpenScripting.framework - scripting addition "/Users/mikekentdavies/Library/ScriptingAdditions/List & Record Tools.osax" cannot be used with the current OS because it has no OSAXHandlers entry in its Info.plist.
/Users/mikekentdavies/Library/Containers/com.apple.mail/Data/Library/Mail Downloads/ADD4F879-F188-4A8E-9CA5-71BAABBA50A9/Hit the Road Jack - FULL Big Band - Myagkov.pdf

I’m on Mac OS 10.12.5

Does this make any sense to anyone?

Cheers!
Mike

Hey Mike,

Are you actually using the List & Record Tools.osax?

If not then uninstall it.

If you need it then make sure you have the most recent version.

Also — you can turn off error messages in the gear menu of the Execute an AppleScript action.

-Chris

Many thanks!
I’ve found and uninstalled it. Now getting what looks like a file path, e.g:

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

Unfortunately, it’s not working (in a variable) when I try to get file attribute, or when I add it to the new mail step to create an attachment.
Do I need to coerce it into a different format?

Many thanks!
Mike

KM file Actions require a POSIX path.
In the script that is returning the above alias, you need to add something like:

set filePath to POSIX path of fileAlias
return filePath

where “fileAlias” is the file path being returned.

If you need more help, please post the script you are using.

I've used "choose file" as a quick B plan (see below).
Next step is to use Chris' script and then go to the B plan if the result is null.

Thanks so much!
I've tried a few combinations to try to get it to work, but no luck so far. I'm assuming that I've got to convert the file path in AppleScript which is what I've been trying mainly.
The "choose file" does work; giving an AppleScript alias.
I don't /think/ that the full file path step works, but it's feasible I'm just using the result wrong somehow..

Best wishes,
Mike

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…

Thanks!

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

try
   
   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
else
   set theFile to POSIX path of (choose file)
end if

------------------------------------------------------------------------------

-Chris

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

-Chris

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…
Cheers!

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?

Thanks!

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.