Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of frontmost app with AppleScript and use it to get the filepath

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

like image 367
patrick Avatar asked Jul 12 '13 10:07

patrick


Video Answer


1 Answers

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.

like image 120
regulus6633 Avatar answered Jan 04 '23 09:01

regulus6633