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.
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
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?
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..
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
------------------------------------------------------------------------------
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…
Cheers!
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 ) 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.