How do I copy a filename without its extension and have it inserted into a pop up file dialogue box?
Here's the background. For the past 10 years I have used a shortcut CMD + P,P to quickly print to PDF. Almost 100% of its use is whilst using MS Word. Since upgrading to Ventura when I print to PDF, the filename which has always defaulted to the file name of whichever document I'm PDF'ing, now appears as 'untitled.pdf'
I have been unable to fix this, find any help from MS, or any other easy workaround. Yes I know I can manually retype the filename in, but it is a manual step that hasn't been needed for as long as I have been PDF'ing files and adds up to a fair bit of time each day.
So now I'm thinking a few KM additions can copy the filename of the active document in Word and replace the 'untitled.pdf' with 'filename.pdf' in the Save As... pop up dialogue box.
But... the only copy filename action I can locate in KM asks for the actual file location of which isn't possible until I create the file. Either that or it would require navigating to that folder and selecting the current file which probably is slower than typing in the filename manually.
Any pointers would be appreciated.
Cheers,
Russell.
Plus brute-force actions emulating user interaction.
AppleScript
Get the name of the front window of the front app.[1]
Plus brute-force actions emulating user interaction.
Directly export from Word to PDF – bypassing dialogs.[2]
My preference
-Chris
Get Name … AppleScript[1]
tell application ((path to frontmost application) as text) to set frontAppName to its name
tell application "System Events"
tell application process frontAppName
set frontWindowName to name of front window
end tell
end tell
Directly Export … AppleScript[2]
--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2023/01/02 22:42
# dMod: 2023/01/02 22:42
# Appl: Microsoft Word 16.16.27
# Task: Save Active Document as a PDF with Same Name in Same Directory.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Save, @Active, @Document, @PDF
--------------------------------------------------------
try
tell application id "com.microsoft.Word"
set activeDoc to active document
tell activeDoc
set docName to its name
set docPath to its path
end tell
set newPath to docPath & ":" & docName
if my folderExists(newPath) is false then
error "Parent Folder Does Not Exist!"
end if
if newPath ends with ".docx" then
set newPath to text 1 thru -6 of newPath
end if
set newPath to newPath & ".pdf"
save as activeDoc file name newPath file format format PDF
end tell
on error errMsg number errNum
set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
if errNum ≠ -128 then
try
beep
tell application (path to frontmost application as text) ¬
to set ddButton to button returned of ¬
(display dialog errMsg with title ¬
"ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to errMsg
end try
end if
end try
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on folderExists(folderPathHFS)
tell application "System Events"
return exists of disk item folderPathHFS
end tell
end folderExists
--------------------------------------------------------
Wow! Chris
That's a lot to digest and thanks heaps for the scripts. I think my brain would have had a hernia trying to figure that out.
Keep you posted.
Regards,
Russell
Apologies if this takes the wind out of your sail but just as I figured out how to implement your script and test run it, the latest update of MS word has restored the filename retention.
However, I offer my thanks yet again for assisting with something that I'd have no hope of figuring out myself. AND... I have the script copied neatly away for the next time MS messes with Word!
Chris
You may be delighted to hear that Microsoft has again updated Office and I'm back to untitled documents when PDF'ing. So I have set up your code to run in KM as execute Applescript and hey presto, better than ever!
Thank you once again for your help.
Cheers,
Russell.