Jump to content

Reveal current open Microsoft Word Document (2011 version) in Finder with HotKey


Recommended Posts

Hello,

I have found a workflow that enables me to open my home folder in finder with a HotKey here: http://www.alfredforum.com/topic/855-how-to-open-a-folder-path-with-a-hotkey/

 

However, I want to be able to open whatever Microsoft Word document I am currently in, in Finder. Is there any keyword that opens the current path name that I can put into the text field in the Hotkey Settings?

 

Thanks!

 

Link to comment

I got this to work by changing the Run NSAppleScript to the less foolproof but working:

 

on alfred_script(q)

tell application "Microsoft Word" to set documentName to path of active document as alias
tell application "Finder" to open the container of documentName
end alfred_script
 
the other code that I deleted from Shawn's version was checking if Word was active but in my testing it never returned True, so it never actually ran the two tell statements above.
Link to comment

It might be system dependent (I didn't really check that as much). I know that I got it to work on my machine.

 

I got the actual string "Microsoft Word" by running an applescript from the script editor:

delay 2
tell application "System Events"
set frontApp to the name of the first application process whose frontmost is true
return frontApp
end tell

For me, that returned "Microsoft Word"; if you run that same script, then it might be that the string returned is slightly different. Well, in that two seconds, I had to switch over to Word, but it worked then.

 

You can get rid of this check and still have it be reliable by making the hotkey contextual to work only within Microsoft Word. To do this, double-click on the "hotkey" and press the "Related Apps" tab. Then add Microsoft Word.

 
Another possibility is that it is opening the folder but not putting focus on it. Is that true?
Edited by Shawn Rice
Link to comment

Use this as the applescript:

on alfred_script(q)
tell application "System Events"
	set frontApp to the name of the first application process whose frontmost is true
	if (frontApp is "Microsoft Word") then
		set continueProcess to true
	else
		set continueProcess to false
	end if
end tell

if continueProcess then
	tell application "Microsoft Word" to set documentName to path of active document as alias
	tell application "Finder" 
		open the container of documentName
    		activate
		select documentName
	end tell
end if
end alfred_script

There's probably a more efficient way to do this, but this way works.

Link to comment

One more quick request, I am realizing this would be really helpful to be able to do in Coda 2 as well. I tried this another if statement like this but it isn't doing anything with Coda. I put an "else if" statement in with Coda 2 and it still worked in Word, however when I replaced the bottom portion with Coda 2 it didn't work, not sure how I would leave the bottom portion to work with both Word and Coda 2 either.  Thoughts? This is what I have right now just to see if I can get Coda 2 doing the exact same thing (open in finder and highlight the file in the directory). Thanks again!

on alfred_script(q)
tell application "System Events"
	set frontApp to the name of the first application process whose frontmost is true
	if (frontApp is "Microsoft Word") then
		set continueProcess to true
	else if (frontApp is "Coda 2") then
		set continueProcess to true
	else
		set continueProcess to false
	end if
end tell

if continueProcess then
	tell application "Coda 2" to set documentName to path of active document as alias
	tell application "Finder" 
		open the container of documentName
    		activate
		select documentName
	end tell
end if
end alfred_script 
Link to comment

It's not going to work the same way with Coda 2 as it will with MS Word, primarily because "path of active document" is part of Word's AS Dictionary. I don't have Coda installed, so I can't really look up what they have or have not built.

 

But, you can do this: open up "Script Editor," and then "File->Open Dictionary..." and see if Coda 2 has an AppleScript dictionary. If it does, it might have some properties that you could plug in to the script rather than the "path of active document."

Link to comment

I tried it out and Coda does have an Applescript dictionary.  I tried it with the keywords from the ones listed in the Word one, but "active document" doesn't exist, here is a few of the words that might be in the same field but maybe they need to be presented differently, thoughts?

 

local path: the local path of the site

path: the path of the browser item

file: Its location on the disk, if it has one.

 

Also does "documentName" stay the same?

 

Shawn, do you have a paypal email address, I would like to give you a little something for helping me out. You can email me your paypal email address to tim@timmorrisdesign.com if you would like. 

Edited by Tim Morris
Link to comment

documentName can stay the same because it's a variable rather than an AS property.

 

It looks like local path refers to site, and path refers to the path of a browser item -- neither of which are the current document that you're editing. So, try something like:

tell application "Coda 2" to set documentName to file of document 1 as alias

That may or may not work.

 

To debug it a bit, try just opening Script Editor and popping in that single line of code and see what happens (make sure Coda is open at the time).

Link to comment

Thanks! I added another if/else if statement below to get it to work correctly with your last snippet.  

 

My final code that worked is:

on alfred_script(q)
tell application "System Events"
	set frontApp to the name of the first application process whose frontmost is true
	if (frontApp is "Microsoft Word") then
		set continueProcess to true
	else if (frontApp is "Coda 2") then
		set continueProcess to true
	else
		set continueProcess to false
	end if
end tell

if continueProcess then
	if (frontApp is "Microsoft Word") then
		tell application "Microsoft Word" to set documentName to path of active document as alias
	else if (frontApp is "Coda 2") then
		tell application "Coda 2" to set documentName to file of document 1 as alias
	end if

	tell application "Finder" 
		open the container of documentName
    		activate
		select documentName
	end tell
end if
end alfred_script
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...