12
votes

What I try to do:

When I'm in one of my text editors (TextEdit, Byword, FoldingText) I want this AppleScript to display the file path.

I figured asking for the frontmost window app get's me the apps name nice and easily and then I can ask for the POSIX path in the next step.

The Problem:

The script is already 99% there, but I'm missing something. When I try to use the variable of activeApp it doesn't work and I get this error:

Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728

Here's the script:

tell application "System Events"
     set activeApp to name of application processes whose frontmost is true

     --This doesn't work either:
     --do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'"

     tell application activeApp
         set myPath to POSIX path of (get file of front document)
     end tell
     display dialog myPath
end tell

If I exchange activeApp with "TextEdit" everything works. Help would be appreciated.

Maybe there's something in here that helps: Get process name from application name and vice versa, using Applescript

4

4 Answers

10
votes

Either get the path property of a document or use System Events to get value of attribute "AXDocument":

try
    tell application (path to frontmost application as text)
        (path of document 1) as text
    end tell
on error
    try
        tell application "System Events" to tell (process 1 where frontmost is true)
            value of attribute "AXDocument" of window 1
        end tell
        do shell script "x=" & quoted form of result & "
        x=${x/#file:\\/\\/}
        x=${x/#localhost} # 10.8 and earlier
        printf ${x//%/\\\\x}"
    end try
end try

The first method didn't work with Preview, TextMate 2, Sublime Text, or iChm, and the second method didn't work with Acorn. The second method requires access for assistive devices to be enabled.

4
votes

I've been using this snippet for a while, seems to work for all Cocoa apps (not sure about X11):

set front_app to (path to frontmost application as Unicode text)

tell application front_app
    -- Your code here
end tell
3
votes

You are asking for...

set activeApp to name of application processes whose frontmost is true

Notice "processes", that's plural meaning you can get several processes in response so applescript gives you a list of names. Even though only one application is returned it's still in list format. Also see that your error contains {"TextEdit"}. The brackets around the name mean it's a list, so the error is showing you the problem.

You can't pass a list of names to the next line of code. As such you have a couple of choices. 1) you can ask for only 1 process instead of all processes. That will return a string instead of a list. Try this code...

set activeApp to name of first application process whose frontmost is true

2) you can work with the list by using "item 1 of the list". Try this code...

set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps

Finally, you shouldn't be telling system events to tell the application. Separate those 2 tell blocks of code. Here's how I would write your code.

tell application "System Events"
    set activeApp to name of first application process whose frontmost is true
end tell

try
    tell application activeApp
        set myPath to POSIX path of (get file of front document)
    end tell

    tell me
        activate
        display dialog myPath
    end tell
on error theError number errorNumber
    tell me
        activate
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
    end tell
end try

I can't promise the "get file of front document" code will work. That depends on the application. Not all applications will understand that request. That's why I used a try block. In any case though you can be certain you are addressing the proper application. Good luck.

0
votes

None of this seems to work with a compiled AppleScript saved as an application and placed on the Dock. Whenever you run the application, IT is the frontmost, not the application that is showing its front window. That application becomes inactive as my Applescript runs. How do I write an Applescript application that isn't active when it runs?

I may have found a solution to the problem listed above. Just tell the user to reactivate the desired application, and give them time.

tell application "Finder"
   activate
   say "Click front window of your application"
   delay 5
   set myapp to get name of first application process whose frontmost is true 
-- etc.
-- your code
end tell