How to Open a Specific Word Document in a Folder Hierarchy?

I have a folder called '0 current report'. Within that folder is another folder called 'jobname 1234' where jobname could be any word(s) and 1234 could be any 0-9 digits.

Within 'jobname 1234' will be a word document called '1234 jobname R01'.

I made attached macro to open the word doc but it does not seem to work. Any suggestions why?

Open current report.kmmacros (5.6 KB)

1 Like

Hey Ross,

This seems very convoluted.

Your root folder will always be:

/<path-to>/0 current report/

And there will always be only one folder inside?

And inside that folder there will always be one file?

-Chris

Hi Chris yes I thought there must be an easier way.

Root folder will always be 0 current report.

There will always be only one folder inside 0 current report called ‘jobname 1234’ (where jobname will be one or more words and 1234 will be any 4 digit number).

There will be a variable number of files and folders inside ‘jobname 1234’ including several word documents. But only one word document will have the name form of ‘1234 jobname R01’ (where jobname will be one or more words and 1234 will be any 4 digit number).

Will that Word document be in the root of the JobName 1234 folder?

Or can it be buried somewhere in the hierarchy?

-Chris

Hey Ross,

I'm assuming the root for now.

If I'm correct then this task is fairly simple, and I'll start with AppleScript.

Adjust the sourceFolderPath for your system, and run this from Apple's Script Editor.app or better yet Script Debugger if you have it.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/30 21:34
# dMod: 2021/12/30 21:34 
# Appl: Finder, System Events, Microsoft Word
# Task: Open a Word Document Buried in a Folder Hierarchy.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @System_Events
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

## USER SETTING ##

set sourceFolderPath to "~/Library/Mobile Documents/com~apple~CloudDocs/untitled folder/xx/0 current report/"

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

set sourceFolderPath to ((current application's NSString's stringWithString:sourceFolderPath)'s stringByExpandingTildeInPath) as text
set sourceFolderAlias to alias POSIX file sourceFolderPath

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}}

tell application "Finder"
   set folderList to folders of sourceFolderAlias as alias list
   set jobFolderAlias to item 1 of folderList
   set jobFolderName to name of jobFolderAlias
end tell

set jobNumber to first word of jobFolderName
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set jobName to rest of words of jobFolderName as text
set AppleScript's text item delimiters to oldTIDS
set jobName to jobName & space & jobNumber

tell application "System Events"
   set fileList to files of jobFolderAlias whose name contains jobName
   if length of fileList = 1 then
      open item 1 of fileList
   else
      error
   end if
end tell

set AppleScript's text item delimiters to oldTIDS

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

This task should be fairly simple to do with the shell and with Keyboard Maestro native actions as well, but I'll hold off until I know the answer to the root question.

-Chris

Yes — Word document will be in the root of the JobName 1234 folder

Okay, that script should open it by using the name of the job folder.

All you should have to do to get it working is provide the path to the /0 current report/ folder at the top of the script.

-Chris

Tried it inside the 'Execute AppleScript' action in Keyboard Maestro but throws an error. Should it work inside 'Execute AppleScript' action?

What error?

Did you try running it from Apple's Script Editor.app?

Did you change the path in this variable to reflect your system?

set sourceFolderPath to "~/Library/Mobile Documents/com~apple~CloudDocs/untitled folder/xx/0 current report/"

Of course. It works fine for me.

Attached is screenshot of the error. There is also an error if I try to run it from Script Editor.app.

To get the path name, I ⌥ clicked on the folder '0 current report' then pressed 'copy as pathname'. I then pasted this path into the script so it reflected my system.

Screenshot 2022-01-03 at 13.36.02

What error?

Screenshot of error. The code (-2700) is same as error in Keyboard Maestro.

Do you know what error is and how I might fix it please?

-2700 is an unknown error, so I have no idea how to fix it.