14

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 Answers 4

12

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.

7
  • The try part is just great. Very flexible and a lot less hand work of getting all the different apps to output the correct path (just stopped writing IF conditions as soon as I tested this). Apropos, "if" I can get this to work with nvALT then I have even more than I desired.
    – patrick
    Jul 12, 2013 at 14:00
  • You could use something like tell application "System Events" to tell (process 1 where frontmost is true) to if name is "nvALT" then ((system attribute "HOME") & "/Notes/" & value of text field 1 of group 1 of tool bar 1 of window 1 & ".txt").
    – Lri
    Jul 13, 2013 at 1:20
  • 1
    This is nice. Thanks. But when I run the script it removes the text at the beginning of the path so /users/username/path/to/file becomes sername/path/to/file. Any idea why?
    – drstevok
    Jun 3, 2014 at 15:27
  • @drstevok I edited the answer. The AXDocument URLs started with file:///localhost/ in 10.8 but they start with just file:/// in 10.9.
    – Lri
    Jun 4, 2014 at 4:36
  • +1, for the clever bash URL-decoding trick alone; there's a small bug, though: there are only 2 initial / instances in 10.8 file URLs: file://localhost/...; to look it at differently: 10.9 file URLs simply drop the localhost token (resulting in a 3rd consecutive /, which, however, is part of the local path); thus, it should be ${x#localhost} instead of ${x/#\\/localhost}. Note that this solution doesn't work with Microsoft Word (2011), sadly: path of document 1 does return an object there, but only if explicitly inside a "Microsoft Word" block.
    – mklement0
    Jun 4, 2014 at 7:27
5

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.

7
  • Thanks for correcting me on the plural of process… I hadn't thought of that. I'm still learning the basics (yesterday I was working on a list AppleScript and getting the curly braces here made me wonder). // In addition I now have made the two parts separates like you suggested. Still I have no luck. // I copied your script and tried it on my text editors but there's still the -1728 problem.
    – patrick
    Jul 12, 2013 at 12:37
  • I updated the code... I forgot "name of first application process" so please try again. Jul 12, 2013 at 12:39
  • I updated my description explain better what I'm after. Sadly trying "name of first" just outputs a variant of the error: "error "AppleScript Editor got an error: Can’t get file of document 1." number -1728 from file of document 1"
    – patrick
    Jul 12, 2013 at 12:46
  • 1
    If the front application is TextEdit then you'll need... "get path of front document". Other applications may need different code. Unfortunately there is no uniform code which will work for every application. Jul 12, 2013 at 12:47
  • Right. That's it. I guess I can work around that with an IF statement.
    – patrick
    Jul 12, 2013 at 13:10
5

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
1
  • Thanks for contributing, but this only gets the app, but not the active/frontmost document.
    – patrick
    Aug 26, 2014 at 7:54
0

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.